C programming on if else statement:
1.//wap to check a number odd or even
#include<stdio.h>
main()
{
int n;
printf("enter a number");
scanf("%d",&n);
if(n%2==0)
printf("even");
else
printf("odd");
}
2.//wap to check a number negative,positive or zero
#include<stdio.h>
main()
{
int n;
printf("enter a number");
scanf("%d",&n);
if(n>0)
printf("positive");
else if(n<0)
printf("negative");
else
printf("zero");
}
3./*wap to accept a number form user and check it is single digit,double digit, more than double digit number*/
#include<stdio.h>
main()
{
int n;
printf("enter a number");
scanf("%d",&n);
if(n>=0 && n<=9)
printf("single digit number");
else if(n>=10 && n<=99)
printf("double digits number");
else
printf("more than 2 digits number");
}
4./*wap to accept 2 numbers from user and print the greater number*/
#include<stdio.h>
main()
{
int a,b;
printf("enter 2 numbers");
scanf("%d%d",&a,&b);
if(a>b)
printf("%d is greater",a);
else if(b>a)
printf("%d is greater",b);
else
printf("both are equal");
}
5./*wap to accept 3 numbers from user and print the maximum number*/
#include<stdio.h>
main()
{
int a,b,c;
printf("enter 3 numbers");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
printf("%d is max",a);
else if(b>a && b>c)
printf("%d is max",b);
else if(c>a && c>b)
printf("%d is max",c);
else
printf("invalid condition");
}
6./*wap to check a number is buzz number or not*/
#include<stdio.h>
main()
{
int n;
printf("enter a number");
scanf("%d",&n);
if(n%7==0 || n%10==7)
printf("buzz no");
else
printf("not buzz number");
}
7./*wap to check a year leapyer or not*/
#include<stdio.h>
main()
{
int y;
printf("enter a year");
scanf("%d",&y);
if((y%4==0 && y%100!=0)||(y%400==0))
printf("leapyear");
else
printf("not leapyear");
}
Or
/*wap to check a year leapyer or not*/
#include<stdio.h>
main()
{
int y;
printf("enter a year");
scanf("%d",&y);
if(y%4==0 && y%100!=0)
printf("leapyear");
else if(y%400==0)
printf("century leapyear");
else
printf("not leapyear");
}
8./*Write a program to accept 3 sides of a triangle and check triangle formation possible or not if possible then check it is equilateral triangle isosceles triangle and scalene triangle*/
#include<stdio.h>
main()
{
int a,b,c;
printf("enter sides of a triangle\n");
scanf("%d%d%d",&a,&b,&c);
if(a+b>c && b+c>a && c+a>b)
{
printf("triangle formation possible\n");
if(a==b && b==c)
printf("eq....");
else if(a==b||b==c||c==a)
printf("iso....");
else if(a!=b && b!=c && c!=a)
printf("sc.....");
}
else
{
printf("triangle fromation not possible");
}
}
9./*Write a program to accept three angles of a triangle and check triangle formation possible or not if possible then check right angle triangle acute angle triangle or obtuse angle triangle*/
#include<stdio.h>
main()
{
int a,b,c;
printf("enter angles of a triangle\n");
scanf("%d%d%d",&a,&b,&c);
if(a+b+c==180)
{
printf("triangle formation possible\n");
if(a==90 || b==90 ||c==90)
printf("right.....");
else if(a<90 && b<90 && c<90)
printf("ac....");
else if(a>90 || b>90 || c>90)
printf("obtu.....");
}
else
{
printf("triangle fromation not possible");
}
}
10./*Write a program to accept the price of a mobile phone calculate and print profit and profit percentage or loss and loss percentage*/
#include<stdio.h>
main()
{
int cp,sp;
float p,pp,l,lp;
printf("enter cp and sp\n");
scanf("%d%d",&cp,&sp);
if(sp>cp)
{
p=sp-cp;
pp=p/cp*100.0;
printf("profit=%.2f profit percentage=%.2f",p,pp);
}
else if(cp>sp)
{
l=cp-sp;
lp=l/cp*100.0;
printf("loss=%.2f loss percentage=%.2f",l,lp);
}
else
printf("no loss no profit");
}
11./*wap to accept price of a tv and calculate the discount and actual price which has been given below
price discount(in%)
--------------------------------------
upto 5000 2.5%
5001-10000 3.5%
10001-15000 4.5%
more than 15000 5.25% */
#include<stdio.h>
main()
{
int p;
float d,ac;
printf("enter the price from user");
scanf("%d",&p);
if(p<=5000)
d=p*2.5/100.0;
else if(p>5000 && p<=10000)
d=p*3.5/100.0;
else if(p>10000 && p<=15000)
d=p*4.5/100.0;
else
d=p*5.25/100.0;
ac=p-d;
printf("discount will be %.2f\nactual price will be=%.2f",d,ac);
}
12./*wap to accept price of a tv and calculate the discount and actual price which has been given below
price discount(in%)
--------------------------------------
upto 5000 2.5%
5001-10000 3.5%
10001-15000 4.5%
more than 15000 5.25% */
#include<stdio.h>
main()
{
int p;
float d,ac;
printf("enter the price from user");
scanf("%d",&p);
if(p<=5000)
d=p*2.5/100.0;
else if(p>5000 && p<=10000)
d=p*3.5/100.0;
else if(p>10000 && p<=15000)
d=p*4.5/100.0;
else
d=p*5.25/100.0;
ac=p-d;
printf("discount will be %.2f\nactual price will be=%.2f",d,ac);
}