On this page, you will see some if else exercise and their solution.
Write a program (WAP) that will print “Hello World”.
Sample Input | Sample Output |
---|---|
Hello World |
#include<stdio.h>
int main(){
printf("Hello World\n");
return 0;
}
Hello World
Write a program (WAP) that will use newline/tab and print the following segment:
Sample Input | Sample Output |
---|---|
Hello World. This is my first program. C is fun. |
#include<stdio.h>
int main(){
printf("Hello World.\n This is my first program.\t C is fun.");
return 0;
}
Hello World.
This is my first program. C is fun.
Write a program (WAP) that will print the following segment:
Sample Input | Sample Output |
---|---|
The question is - “How to write a \comment/ in C programming language?” |
#include<stdio.h>
int main(){
printf("The question is - \“How to write a \\comment/ in C programming language?\”");
return 0;
}
3
three
Write a program (WAP) will check whether a triangle is valid or not, when the three angles (angle value
should be such that, 0 < value < 180) of the triangle are entered through the keyboard.
[N:B: A triangle is valid if the sum of all the three angles is equal to 180 degrees.]
Sample Input | Sample Output |
---|---|
90 45 45 | Yes |
30 110 40 | Yes |
160 20 30 | No |
#include<stdio.h>
int main(){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(a + b + c == 180){
printf("Yes\n");
}
else{
printf("No\n");
}
return 0;
}
100 20 60
Yes
Write a program (WAP) that will read from the console a random positive nonzero number and determine if it is a power of 2.
Sample Input | Sample Output |
---|---|
1 | Yes |
512 | Yes |
1022 | No |
#include<stdio.h>
int main(){
int number;
scanf("%d", &number);
if (number > 0 && (number & (number - 1)) == 0) {
printf("Yes\n");
} else {
printf("No\n");
}
return 0;
}
1
Yes
Write a program (WAP) that will read from the console a random number and check if it is a nonzero
positive number. If the check is yes, it will determine if the number is a power of 2.
If the check fails the program will check for two more cases. If the number is zero, the
program will print “Zero is not a valid input”. Else it will print “Negative input is not valid”.
Sample Input | Sample Output |
---|---|
0 | Zero is not a valid input |
1 | Yes |
512 | Yes |
1022 | No |
-512 | Negative input is not valid |
#include<stdio.h>
int main(){
int number;
scanf("%d", &number);
if (number > 0) {
if ((number & (number - 1)) == 0) {
printf("Yes\n");
} else {
printf("No\n");
}
}
else if (number == 0) {
printf("Zero is not a valid input\n");
}
else {
printf("Negative input is not valid\n");
}
return 0;
}
0
Zero is not a valid input
Write a program (WAP) m that will take two numbers X & Y as inputs and decide whether X is greater than/less than/equal to Y.
Sample input (X,Y) | Sample output |
---|---|
5 -10 | 5 is greater than -10 |
5 10 | 5 is less than 10 |
5 5 | 5 is equal to 5 |
#include<stdio.h>
int main(){
int X, Y;
scanf("%d%d",&X,&Y);
if (X > Y) {
printf("%d is greater than %d\n", X, Y);
}
else if (X < Y) {
printf("%d is less than %d\n", X, Y);
}
else {
printf("%d is equal to %d\n", X, Y);
}
return 0;
}
5 5
5 is equal to 5
Write a program (WAP) that will decide whether a year is leap year or not.
Yes, if ( Year % 4 == 0 && year % 100 != 0 ) || ( Year % 400 ==0 )
Sample Input | Sample Output |
---|---|
2000 | Yes |
2004 | Yes |
2014 | No |
#include<stdio.h>
int main(){
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
printf("Yes\n");
}
else {
printf("No\n");
}
return 0;
}
2000
Yes