Assignment of Subjective questions
1. Write a C program to input character from user and check whether character is
uppercase or lowercase alphabet using if else.
Example
Input
Input character: C
Output
'C' is uppercase alphabet
Solution -
#include <stdio.h>
int main()
{
char ch;
/* Input character from user */
printf("Enter any character: ");
scanf("%c", &ch);
if(ch >= 'A' && ch <= 'Z')
{
printf("'%c' is uppercase alphabet.", ch);
}
else if(ch >= 'a' && ch <= 'z')
{
printf("'%c' is lowercase alphabet.", ch);
}
else
{
printf("'%c' is not an alphabet.", ch);
}
return 0;
}
2. Write a C program to input week number(1-7) and print the corresponding day
of week name using if else.
Example
Input
Input week number: 1
Output
Monday
Solution -
#include <stdio.h>
int main()
{
int week;
/* Input week number from user */
printf("Enter week number (1-7): ");
scanf("%d", &week);
if(week == 1)
{
printf("Monday");
}
else if(week == 2)
{
printf("Tuesday");
}
else if(week == 3)
{
printf("Wednesday");
}
else if(week == 4)
{
printf("Thursday");
}
else if(week == 5)
{
printf("Friday");
}
else if(week == 6)
{
printf("Saturday");
}
else if(week == 7)
{
printf("Sunday");
}
else
{
printf("Invalid Input! Please enter week number between 1-7.");
}
return 0;
}
3. Write a C program to enter month number between(1-12) and print number of
days in month using if else.
Example
Input
Enter month number: 1
Output
It contains 31 days.
Solution -
#include <stdio.h>
int main()
{
int month;
/* Input month number from user */
printf("Enter month number (1-12): ");
scanf("%d", &month);
if(month == 1)
{
printf("31 days");
}
else if(month == 2)
{
printf("28 or 29 days");
}
else if(month == 3)
{
printf("31 days");
}
else if(month == 4)
{
printf("30 days");
}
else if(month == 5)
{
printf("31 days");
}
else if(month == 6)
{
printf("30 days");
}
else if(month == 7)
{
printf("31 days");
}
else if(month == 8)
{
printf("31 days");
}
else if(month == 9)
{
printf("30 days");
}
else if(month == 10)
{
printf("31 days");
}
else if(month == 11)
{
printf("30 days");
}
else if(month == 12)
{
printf("31 days");
}
else
{
printf("Invalid input! Please enter month number between (1-12).");
}
return 0;
}
4. Write a C program to check whether a triangle is valid or not if angles are
given using if else.
Example
Input
Input first angle: 60
Input second angle: 30
Input third angle: 90
Output
The triangle is valid
Solution -
#include <stdio.h>
int main()
{
int angle1, angle2, angle3, sum;
/* Input all three angles of triangle */
printf("Enter three angles of triangle: \n");
scanf("%d%d%d", &angle1, &angle2, &angle3);
/* Calculate sum of angles */
sum = angle1 + angle2 + angle3;
/*
* If sum of angles is 180 and
* angle1, angle2, angle3 is not 0 then
* triangle is valid.
*/
if(sum == 180 && angle1 > 0 && angle2 > 0 && angle3 > 0)
{
printf("Triangle is valid.");
}
else
{
printf("Triangle is not valid.");
}
return 0;
}
5. Write a C program to input sides of a triangle and check whether a triangle is
equilateral, scalene or isosceles triangle using if else.
Example
Input
Input first side: 30
Input second side: 30
Input third side: 30
Output
Triangle is equilateral triangle
Solution -
#include <stdio.h>
int main()
{
int side1, side2, side3;
/* Input sides of a triangle */
printf("Enter three sides of triangle: ");
scanf("%d%d%d", &side1, &side2, &side3);
if(side1==side2 && side2==side3)
{
/* If all sides are equal */
printf("Equilateral triangle.");
}
else if(side1==side2 || side1==side3 || side2==side3)
{
/* If any two sides are equal */
printf("Isosceles triangle.");
}
else
{
/* If none sides are equal */
printf("Scalene triangle.");
}
return 0;
}
6. Write a C program to check whether a number is divisible by 5 and 11 or not
using if else.
Example
Input
Input number: 55
Output
Number is divisible by 5 and 11
Solution -
#include <stdio.h>
int main()
{
int num;
/* Input number from user */
printf("Enter any number: ");
scanf("%d", &num);
/*
* If num modulo division 5 is 0
* and num modulo division 11 is 0 then
* the number is divisible by 5 and 11 both
*/
if((num % 5 == 0) && (num % 11 == 0))
{
printf("Number is divisible by 5 and 11");
}
else
{
printf("Number is not divisible by 5 and 11");
}
return 0;
}
7. Write a C program to check leap year using if else.
Example
Input
Input year: 2004
Output
2004 is leap year.
Solution -
#include <stdio.h>
int main()
{
int year;
/* Input year from user */
printf("Enter year : ");
scanf("%d", &year);
/*
* If year is exactly divisible by 4 and year is not divisible by 100
* or year is exactly divisible by 400 then
* the year is leap year.
* Else year is normal year
*/
if(((year % 4 == 0) && (year % 100 !=0)) || (year % 400==0))
{
printf("LEAP YEAR");
}
else
{
printf("COMMON YEAR");
}
return 0;
}
8. Write a C program to input a character from user and check whether the given
character is alphabet or not using if else.
Example
Input
Input character: a
Output
'a' is alphabet
Solution -
#include <stdio.h>
int main()
{
char ch;
/* Input a character from user */
printf("Enter any character: ");
scanf("%c", &ch);
if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
printf("Character is an ALPHABET.");
}
else
{
printf("Character is NOT ALPHABET.");
}
return 0;
}
9. Write a C program to check whether an alphabet is vowel or consonant using if
else.
Example
Input
Input character: a
Output
'a' is vowel
Solution -
int main()
{
char ch;
/* Input character from user */
printf("Enter any character: ");
scanf("%c", &ch);
/* Condition for vowel */
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' ||
ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U')
{
printf("'%c' is Vowel.", ch);
}
else if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
/* Condition for consonant */
printf("'%c' is Consonant.", ch);
}
else
{
/*
* If it is neither vowel nor consonant
* then it is not an alphabet.
*/
printf("'%c' is not an alphabet.", ch);
}
return 0;
}
10. Write a C program to input a character from user and check whether given
character is alphabet, digit or special character using if else.
Example
Input
Input any character: 3
Output
3 is digit
Solution -
#include <stdio.h>
int main()
{
char ch;
/* Input character from user */
printf("Enter any character: ");
scanf("%c", &ch);
/* Alphabet check */
if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
printf("'%c' is alphabet.", ch);
}
else if(ch >= '0' && ch <= '9')
{
printf("'%c' is digit.", ch);
}
else
{
printf("'%c' is special character.", ch);
}
return 0;
}