If Condition Programs Set 1

Question: 1

Write a C program to take input for a number, check and print whether the number is +ve or not?

Sol:

#include <stdio.h>
 int main()
 {
     int a;
     printf("Enter any no ");
     scanf("%d",&a);
     if(a>0)
         printf("number is +ve");
     else
         printf("number is not +ve");
     return 0;
 }

Output:

case 1:

Enter any no 25
number is +ve

Case :2

Enter any no -96
number is not +ve

Question : 2

Write a C program to take input for a number, check and print whether the number is -ve or not?

Sol:

#include <stdio.h>
 int main()
 {
     int a;
     printf("Enter any no ");
     scanf("%d",&a);
     if(a<0)
         printf("number is -ve");
     else
         printf("number is not -ve");
     return 0;
 }

Output:

case 1:
Enter any no 25
number is not -ve
case 2:
Enter any no -96
number is -ve

Question: 3

Write a C program to take input for a number, check and print whether the number is even or not?

Sol:

#include <stdio.h>
 int main()
 {
     int a;
     printf("Enter any no ");
     scanf("%d",&a);
     if(a%2==0)
         printf("number is even");
     else
         printf("number is not even");
     return 0;
 }

Output:

case 1:
Enter any no 26
number is even
case 2:
Enter any no 95
number is not even

Question: 4

Write a C program to take input for a number, check and print whether the number is odd or not?

Sol:

#include <stdio.h>
 int main()
 {
     int a;
     printf("Enter any no ");
     scanf("%d",&a);
     if(a%2==1) //if(a%2!=0)
         printf("number is odd");
     else
         printf("number is not odd");
     return 0;
 }

Output:

case 1:
Enter any no 45
number is odd
case 2:
Enter any no 67
number is odd

Question: 5

Write a C program to take input for a number, check and print whether the number is a multiple of 7 or not?

Sol:

#include <stdio.h>
 int main()
 {
     int a;
     printf("Enter any no ");
     scanf("%d",&a);
     if(a%7==0)
         printf("number is multiple of 7");
     else
         printf("number is not multiple 7");
     return 0;
 }

Output:

case 1:
Enter any no 24
number is not multiple 7
case 2:
Enter any no 77
number is multiple of 7

C Language Programming Tutorial

C Language Tutorial Home     Introduction to C Language     Tokens     If Condition      goto statement and Labelname     Switch Statements     For loop     While Loop     Do while loop     break and continue     Functions     Recursion     Inbuild Functions     Storage Classes     Preprocessor     Arrays     Pointers     Structures and Unions     File Handling     Projects