KEMBAR78
C Programs On Conditional Statements | PDF | Triangle | Integer (Computer Science)
0% found this document useful (0 votes)
14 views25 pages

C Programs On Conditional Statements

Uploaded by

hdgdjehss
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views25 pages

C Programs On Conditional Statements

Uploaded by

hdgdjehss
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

C Programs on Conditional Statements

//1.Write a program to input two numbers and find maximum


between two.
#include<stdio.h>
int main()
{
int a,b;
printf("Enter first number:");
scanf("%d",&a);
printf("Enter second number:");
scanf("%d",&b);
if(a>b)
{
printf("a=%d is maximum",a);
}
else
{
printf("b=%d is maximum",b);
}
return 0;
}

//2.Write a program to input three numbers and find maximum


between three.
#include<stdio.h>
int main()
{
int a,b,c;
printf("Enter first number(a):");
scanf("%d",&a);
printf("Enter second number(b):");
scanf("%d",&b);
printf("Enter third number(c):");
scanf("%d",&c);
if(a>b)
{
if(a>c)
printf("a=%d is maximum",a);
else
printf("c=%d is maximum",c);
}
else
{
if(b>c)
printf("b=%d is maximum",b);
else
printf("c=%d is maximum",c);
}
return 0;
}

//3.Write a program to input any number and check whether it is even


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

//4.Write a program to input any number and check whether it is negative,


positive or zero.
#include <stdio.h>
int main()
{
int n;
printf("Enter a number: ");
scanf("%d",&n);
if(n>0)
printf("%d is a positive number.",n);
else if(n==0)
printf("%d is a zero.",n);
else
printf("%d is a negative number.",n);
return 0;
}

//5.Write a program to input any number and check whether it is divisible


by 5 and 11 or not.
#include <stdio.h>
int main()
{
int n;
printf("Enter a number: ");
scanf("%d",&n);
if((n%5==0)&&(n%11==0))
printf("%d is divisible by both 5 and 11",n);
else
printf("%d is not divisible by both 5 and 11",n);
return 0;
}

//6.Write a program to input any year & check whether it is leap year
or not.
#include <stdio.h>
int main()
{
int year;
printf("Enter year number: ");
scanf("%d",&year);
if(year%100==0)
{
if(year%400==0)
printf("%d is leap year \n",year);
else
printf("%d is not leap year \n",year);
}
else if(year%4==0)
printf("%d is leap year \n",year);
else
printf("%d is not leap year \n",year);
return 0;
}

//7.Write a program to input three numbers and find smallest number.


#include<stdio.h>
int main()
{
int a,b,c;
printf("Enter first number(a):");
scanf("%d",&a);
printf("Enter second number(b):");
scanf("%d",&b);
printf("Enter third number(c):");
scanf("%d",&c);
if(a<b)
{
if(a<c)
printf("a=%d is smallest number \n",a);
else
printf("c=%d is smallest number \n",c);
}
else
{
if(b<c)
printf("b=%d is smallest number \n",b);
else
printf("c=%d is smallest number \n",c);
}
return 0;
}

//8.Write a program to input angles of a triangle and check whether


triangle is valid or not.
#include<stdio.h>
int main()
{
int a,b,c,sum;
printf("Enter first angle of a traingle(a):");
scanf("%d",&a);
printf("Enter second angle of a traingle(b):");
scanf("%d",&b);
printf("Enter third angle of a traingle(c):");
scanf("%d",&c);
sum=a+b+c;
if(sum==180 && a>0 && b>0 && c>0)
printf("Triangle is valid \n");
else
printf("Triangle is not valid \n");
return 0;
}

//9.Write a program to input all sides of a triangle and check whether


triangle is valid or not.
//Hint:A triangle is valid if sum of two sides is greater than the side
#include<stdio.h>
int main()
{
int a,b,c;
printf("Enter first side of a traingle(a):");
scanf("%d",&a);
printf("Enter second side of a traingle(b):");
scanf("%d",&b);
printf("Enter third side of a traingle(c):");
scanf("%d",&c);
if((a+b)>c && (b+c)>a && (c+a)>b)
printf("Triangle is valid \n");
else
printf("Triangle is not valid \n");
return 0;
}
//10.Write a program to input all sides of a triangle and check whether
triangle is Equilateral, Isosceles or Scalene triangle.
#include<stdio.h>
int main()
{
int a,b,c;
printf("Enter first side of a traingle(a):");
scanf("%d",&a);
printf("Enter second side of a traingle(b):");
scanf("%d",&b);
printf("Enter third side of a traingle(c):");
scanf("%d",&c);
if(a==b && b==c)
printf("Triangle is Equilateral \n");
else if(a==b || b==c || c==a)
printf("Triangle is Isosceles\n");
else
printf("Triangle is Scalene \n");
return 0;
}

//11.Write a program to input cost price and selling price of a product and
calculate profit or loss.
#include<stdio.h>
int main()
{
int cp,sp,profit,loss;
printf("Enter cost price of a product:");
scanf("%d",&cp);
printf("Enter selling price of a product:");
scanf("%d",&sp);
if(sp>cp)
{
profit=sp-cp;
printf("Profit: %d Rs \n",profit);
}
else if(cp>sp)
{
loss=cp-sp;
printf("Loss: %d Rs \n",loss);
}
else
{
printf("No profit & No loss \n");
}
return 0;
}

//12.Write a program that reads any three integers and print the second
smallest among them.
#include<stdio.h>
int main()
{
int a,b,c;
printf("Enter first integer(a):");
scanf("%d",&a);
printf("Enter second integer(b):");
scanf("%d",&b);
printf("Enter third integer(c):");
scanf("%d",&c);
if(a<=b && a<=c)
{
if(b>=c)
printf("c= %d is second smallest integer \n",c);
else
printf("b= %d is second smallest integer \n",b);
}
else if(b<=c && b<=a)
{
if(c>=a)
printf("a= %d is second smallest integer \n",a);
else
printf("c= %d is second smallest integer \n",c);
}
else
{
if(a>=b)
printf("b= %d is second smallest integer \n",b);
else
printf("a= %d is second smallest integer \n",a);
}
return 0;
}

//13.Write a program to input any character and check whether it is


alphabet or not
#include<stdio.h>
void main()
{
char c;
printf("Enter a character:");
scanf("%c",&c);
if((c>='A' && c<='Z')||(c>='a' && c<='z'))
printf("%c is a alphabet \n",c);
else
printf("%c is not a alphabet \n",c);
}

//14.Write a program to input any alphabet and check whether it is vowel


or consonant.
#include<stdio.h>
void main()
{
char c;
printf("Enter a character:");
scanf("%c",&c);
if((c>='A' && c<='Z')||(c>='a' && c<='z'))
{
if(c=='A' || c=='E' || c=='I' || c=='O' || c=='U' || c=='a' || c=='e' ||c=='i' || c=='o' ||
c=='u')
printf("%c is vowel \n",c);
else
printf("%c is consonant \n",c);
}
else
printf("Enter alphabets only \n");
}

//15.Write a program to input any character and check whether it is


alphabet, digit or special character.
#include<stdio.h>
void main()
{
char c;
printf("Enter a character:");
scanf("%c",&c);
if((c>='A' && c<='Z')||(c>='a' && c<='z'))
printf("%c is a alphabet \n",c);
else if(c>='0' && c<='9')
printf("%c is a digit \n",c);
else
printf("%c is a special character \n",c);
}

//16.Write a program to check whether a character is Uppercase or


Lowercase alphabet.
#include<stdio.h>
void main()
{
char c;
printf("Enter a character:");
scanf("%c",&c);
if((c>='A' && c<='Z')||(c>='a' && c<='z'))
{
if(c>='A' && c<='Z')
printf("%c is Uppercase alphabet \n",c);
else
printf("%c is Lowercase alphabet \n",c);
}
else
printf("Enter alphabets only \n");
}

//17.Write a program to prompt for a score between 0.0 and 1.0. If the
score is out ofrange, print an error message. If the score is between 0.0 and
1.0, print a grade using the following instructions:(>= 0.9 A; >= 0.8 B; >=
0.7 C; >= 0.6 D; < 0.6 F)
#include <stdio.h>
int main()
{
float s;
printf("Enter score: ");
scanf("%f",&s);
if(s>0.0 && s<1.0)
{
if(s>=0.9)
printf("A \n");
else if(s>=0.8)
printf("B \n");
else if(s>=0.7)
printf("C \n");
else if(s>=0.6)
printf("D \n");
else
printf("F \n");
}
else
printf("Enter valid score \n");
return 0;
}

//18.Write a program to input marks of five subjects Physics, Chemistry,


Biology, Mathematics and Computer. Calculate percentage and grade
according to following:
/*Percentage >= 90% : Grade A
Percentage >= 80% : Grade B
Percentage >= 70% : Grade C
Percentage >= 60% : Grade D
Percentage >= 40% : Grade E
Percentage < 40% : Grade F*/
#include <stdio.h>
void main()
{
float phy,chem,bio,mat,com,sum,p;
printf("Each subject exam is conducted for 100 marks \n");
printf("Enter obtained marks in Physics:");
scanf("%f",&phy);
printf("Enter obtained marks in Chemistry:");
scanf("%f",&chem);
printf("Enter obtained marks in Biology:");
scanf("%f",&bio);
printf("Enter obtained marks in Maths:");
scanf("%f",&mat);
printf("Enter obtained marks in Computer:");
scanf("%f",&com);
sum=phy+chem+bio+mat+com;
p=(sum/500)*100;
printf("Percentage: %f \n",p);
if(p>=90)
printf("Grade A \n");
else if(p>=80)
printf("Grade B \n");
else if(p>=70)
printf("Grade C \n");
else if(p>=60)
printf("Grade D \n");
else if(p>=40)
printf("Grade E \n");
else
printf("Grade F \n");
}
//19.Write a program to input basic salary of an employee and calculate its
Gross salary according to following:
/*Basic Salary <= 10000 : HRA = 20%, DA = 80%
Basic Salary <= 20000 : HRA = 25%, DA = 90%
Basic Salary > 20000 : HRA = 30%, DA = 95%*/
#include <stdio.h>
void main()
{
float bs,hra,da,gs;
printf("Enter basic salary of an employee:");
scanf("%f",&bs);
if(bs<=10000)
{
hra=bs*0.2;
da=bs*0.8;
}
else if(bs<=20000)
{
hra=bs*0.25;
da=bs*0.9;
}
else
{
hra=bs*0.3;
da=bs*0.95;
}
gs=bs+hra+da;
printf("Gross Salary: %f \n",gs);
}

//20.Write a program to check the person is major or not based on their


age?
//(If the age is greater than 18 then we call him/her major)
#include <stdio.h>
void main()
{
int age;
printf("Enter age of a person:");
scanf("%d",&age);
if(age>18)
printf("Person is major \n");
else
printf("Person is not major \n");
}

//21.Give two integer values from the keyboard or console, if 2 values are
different then print their sum. If 2 values are same then print double their
sum
/*Example:
(1, 2)-->3
(4, 8)-->12
(4, 4)-->16
(5, 5)-->20*/
#include<stdio.h>
int main()
{
int a,b,sum;
printf("Enter first number:");
scanf("%d",&a);
printf("Enter second number:");
scanf("%d",&b);
if(a!=b)
sum=a+b;
else
sum=(a+b)*2;
printf("%d \n",sum);
return 0;
}

//22.Take an integer from the keyboard or console and print the difference
with 15. If the entered number is greater than or equal to 15 then print
double the difference.
/*Example:
3 15-->12
8 15-->7
15 15-->0
17 15-->4*/
#include <stdio.h>
int main()
{
int n,difference;
printf("Enter an integer: ");
scanf("%d",&n);
if(n>15)
difference=(n-15)*2;
else
difference=15-n;
printf("%d \n",difference);
return 0;
}

//23.Take 3 integers from the keyboard or console and print their sum If
any of the number is teen (example 13, 14,15, 16, 17, 18, 19) then that value
counts as 0.
/*Example:
(1, 2, 3)-->6
(2, 14, 11)-->13
(7, 15, 5)-->12
(5, 3, 19)-->8*/
#include <stdio.h>
int main()
{
int a,b,c,sum;
printf("Enter first integer(a):");
scanf("%d",&a);
printf("Enter second integer(b):");
scanf("%d",&b);
printf("Enter third integer(c):");
scanf("%d",&c);
if(a==13 || a==14 || a==15 || a==16 || a==17 || a==18 || a==19)
a=0;
else if (b==13 || b==14 || b==15 || b==16 || b==17 || b==18 || b==19)
b=0;
else if(c==13 || c==14 || c==15 || c==16 || c==17 || c==18 || c==19)
c=0;
else
sum=a+b+c;
sum=a+b+c;
printf("%d \n",sum);
return 0;
}

//24.Write a program to display the last digit of a number and check it is


divisible by 3 or not
//(Hint : any number % 10 will return the last digit)
#include <stdio.h>
int main()
{
int n,ld;
printf("Enter a number:");
scanf("%d",&n);
ld=n%10;
if(ld%3==0)
printf("Last digit of a number is %d. It is divisible by 3 \n",ld);
else
printf("Last digit of a number is %d. It is not divisible by 3 \n",ld);
return 0;
}
//25.If a five-digit number is input through the keyboard, write a program
to calculate the sum of its digits.
//(Hint: Use the modulus operator ‘%’)
#include <stdio.h>
void main()
{
int n,a,b,c,d,e,sum;
printf("Enter five-digit number only:");
scanf("%d",&n);
if(n>=10000 && n<100000)
{
a=n%10;
b=(n/10)%10;
c=(n/100)%10;
d=(n/1000)%10;
e=(n/10000)%10;
sum=a+b+c+d+e;
printf("Sum of digits in the number %d is %d \n",n,sum);
}
else
printf("Enter 5-digit number only \n");
}

//26.If a five-digit number is input through the keyboard, write a program


to reverse the number.
#include <stdio.h>
void main()
{
int n,a,b,c,d,e,rev;
printf("Enter five-digit number only:");
scanf("%d",&n);
if(n>=10000 && n<100000)
{
a=n%10;
b=(n/10)%10;
c=(n/100)%10;
d=(n/1000)%10;
e=(n/10000)%10;
rev=(a*10000)+(b*1000)+(c*100)+(d*10)+e;
printf("Reversed number: %d \n",rev);
}
else
printf("Enter 5-digit number only \n");
}

//27.If a four-digit number is input through the keyboard, write a program


to obtain the sum of the first and last digit of this number.
#include <stdio.h>
void main()
{
int n,f,l,sum;
printf("Enter four-digit number only:");
scanf("%d",&n);
if(n>=1000 && n<10000)
{
l=n%10;
f=(n/1000)%10;
sum=f+l;
printf("Sum of first & last digit in the number %d is: %d\n",n,sum);
}
else
printf("Enter 4-digit number only \n");
}

//28.Enter three points (x1, y1), (x2, y2) and (x3, y3), write a program to
check if all the three points fall on one straight line.
#include <stdio.h>
void main()
{
int x1,y1,x2,y2,x3,y3,m1,m2;
printf("Enter (x1,y1):");
scanf("%d %d",&x1,&y1);
printf("Enter (x2,y2):");
scanf("%d %d",&x2,&y2);
printf("Enter (x3,y3):");
scanf("%d %d",&x3,&y3);
if(x1!=x2 && x2!=x3)
{
m1=(y2-y1)/(x2-x1);
m2=(y3-y2)/(x3-x2);
if(m1==m2)
printf("All three points fall on same straight line \n");
else
printf("All three points not fall on same straight line \n");
}
else
printf("x1,x2,x3 values must be different \n");
}

//29.Enter a point (x, y), write a program to find out if it lies on the x-axis,
y-axis or at the origin.
#include <stdio.h>
void main()
{
int x,y;
printf("Enter (x,y):");
scanf("%d %d",&x,&y);
if(x==00 && y==0)
printf("The point (%d,%d) lies at origin.\n",x,y);
else if(x==0)
printf("The point (%d,%d) lies on the y-axis.\n",x,y);
else if(y==0)
printf("The point (%d,%d) lies on the x-axis.\n",x,y);
else
printf("The point (%d,%d) does not lie on either x-axis or y-axis.\n",x,y);
}
//30.Find the absolute value of a number entered through the keyboard.
#include <stdio.h>
void main()
{
int n;
printf("Enter a number:");
scanf("%d",&n);
if(n<0)
printf("Absolute value of %d is %d \n",n,-n);
else
printf("Absolute value of %d is %d \n",n,n);
}

You might also like