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 decide whether a number is positive or negative.

Sample Input Sample Output
100 Positive
-11.11 Negative
0 Positive

Solution:

  
  #include<stdio.h>
  int main(){
    int n;
    scanf("%d",&n);
    if(n < 0) {
      printf("Negative\n");
    }
    else{
      printf("Positive\n"); 
    }
    return 0;
  }
  
  

Output:


  5
  Positive



Exercise 2:

Write a program (WAP) that will decide whether a number is even or odd.

Sample Input Sample Output
2 Even
5 Odd
11 Odd

Solution:


  #include<stdio.h>
  int main(){
    int n;
    scanf("%d",&n);
    if(n % 2 == 0){
        printf("Even");
    }
    else{
        printf("Odd"); 
    }
    return 0;
  }


Output:


  11
  Odd



Exercise 3:

Write a program (WAP) that will take an integer of length one from the terminal and then display the digit in English.

Sample Input Sample Output
1 one
2 two
7 seven

Solution:


  #include<stdio.h>
  int main(){
    int n;
    if(n == 0){
      print("zero\n");
    }
    else if(n == 1){
      print("one\n");
    }
    else if(n == 2){
      print("two\n");
    }
    else if(n == 3){
      print("three\n");
    }
    else if(n == 4){
      print("four\n");
    }
    else if(n == 5){
      print("five\n");
    }
    else if(n == 6){
      print("six\n");
    }
    else if(n == 7){
      print("seven\n");
    }
    else if(n == 8){
      print("eight\n");
    }
    else if(n == 9){
      print("nine\n");
    }
    else{
        printf("wrong input\n");
    }
    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