IF CONDITION PROGRAMS SET 3

Question: 11

Write a C program to take input for name and age of a person, check and print whether the person can vote or not?

Sol:

#include <stdio.h>
 int main()
 {
     int age;
     char name[20];
     printf("Enter name ");
     scanf("%s",name);
     printf("Enter age ");
     scanf("%d",&age);
     if(age>=18)
         printf("you can vote");
     else
         printf("you cannot vote");
 return 0;
 }

Output:

case 1:
Enter name amit
Enter age 12
you cannot vote
case 2:
Enter name Sumit
Enter age 19
you can vote

Question: 12

Write a C program to take input for a number if number<=10 calculate and print its square?

Sol:

#include <stdio.h>
 int main()
 {
     int a,b;
     printf("Enter any no ");
     scanf("%d",&a);
     if(a<=10)
         {
             b=a*a;
             printf("square = %d",b);
         }
 return 0;
 }

Output:

case :1
Enter any no 5
square = 25
case 2:
Enter any no 12
(no ouput as condition is false)

Question: 13

Write a C program to take input for a number if number<=10 calculate and print its cube otherwise print its square?

Sol:

#include <stdio.h>
 int main()
 {
     int a,b;
     printf("Enter any no ");
     scanf("%d",&a);
     if(a<=10)
         {
             b=a*a*a;
             printf("cube = %d",b);
         }
         else
         {
             b=a*a;
             printf("square = %d",b);
         }
     return(0);
 }

Output:

case :1
Enter any no 5
cube = 125
case 2:
Enter any no 12
square = 144

Question: 14

Write a C program to take a number check and print whether the number is +ve , -ve or zero?

Sol:

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

Output:

case 1:
Enter any no 25
value is +ve
case 2:
Enter any no -96
value is -ve
case 3:
Enter any no 0
value is zero

Question: 15

Write a C program to take a number check and print whether the number is even , odd or zero?

Sol:

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

Output:

case 1:
Enter any no 25
value is odd
case 2:
Enter any no 36
value is even
case 3:
Enter any no 0
value is zero

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