Subjective Assignment 03 Ifelse Ans
Subjective Assignment 03 Ifelse Ans
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
Solution -
#include <stdio.h>
int main()
{
char ch;
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;
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;
}
Example
Input
Enter month number: 1
Output
It contains 31 days.
Solution -
#include <stdio.h>
int main()
{
int 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;
}
Example
Input
Input first angle: 60
Input second angle: 30
Input third angle: 90
Output
Solution -
#include <stdio.h>
int main()
{
int angle1, angle2, angle3, sum;
/*
* 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;
}
Example
Input
Input first side: 30
Input second side: 30
Input third side: 30
Output
#include <stdio.h>
int main()
{
int side1, side2, side3;
return 0;
}
Example
Input
Input number: 55
Output
Number is divisible by 5 and 11
Solution -
#include <stdio.h>
int main()
{
int 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;
}
Example
Input
Input year: 2004
Output
2004 is leap year.
Solution -
#include <stdio.h>
int main()
{
int 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;
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;
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;
/* 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;
}