If Else Exercise 1

On this page, you will see some if else exercise and their solution.


Exercise 1:

Write a program (WAP) that will print “Hello World”.

Sample Input Sample Output
  Hello World

Solution:


  #include<stdio.h>
  int main(){
    printf("Hello World\n");
    return 0;
  }


Output:


  Hello World



Exercise 2:

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.

Solution:


  #include<stdio.h>
  int main(){
    printf("Hello World.\n This is my first program.\t C is fun.");
  return 0;
  }


Output:


  Hello World.
  This is my first program.    C is fun.



Exercise 3:

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?”

Solution:


  #include<stdio.h>
  int main(){
    printf("The question is - \“How to write a \\comment/ in C programming language?\”");
    return 0;
  }


Output:


  3
  three



Exercise 4:

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

Solution:


  #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;
  }


Output:


  100 20 60
  Yes 



Exercise 5:

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

Solution:


  #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;
  }


Output:


  1
  Yes   



Exercise 6:

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

Solution:


  #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;
  }


Output:


  0
  Zero is not a valid input



Exercise 7:

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

Solution:


  #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;
  }


Output:


  5 5
  5 is equal to 5



Exercise 8:

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

Solution:


  #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;
  }


Output:


  2000
  Yes