Ip Exp 11-20
Ip Exp 11-20
Ip Exp 11-20
average in float.
12) Write a C program to find the max and min of four numbers
using if-else.
AIM: Write a C program to find the max and min of four
numbers using if-else.
Program:
#include <stdio.h>
#include<conio.h>
main()
{
int a, b, c, d, y;
printf("Enter four integers (separate them with spaces): ");
scanf("%d %d %d %d", &a, &b, &c, &d);
if (a>b && a>c && a>d){
if (b<c && b<d){
y = b;
}
else if (c<b && c<d){
y = c;
}
else if (d<b && d<c){
y = d;
}
printf("Largest: %d\n", a);
printf("Smallest: %d", y);
}
else if (b>a && b>c && b>d) {
if (a<c && a<d){
y = a;
}
else if(c<a && c<d){
y = c;
}
else if(d<a && d<c){
y = d;
}
printf("Largest: %d\n", b);
printf("Smallest: %d", y);
}
else if (c>a && c>b && c>d)
{
if (a<b && a<d){
y = a;
}
else if(b<a && b<d){
y = b;
}
else if(d<a && d<b){
y = d;
}
printf("Largest: %d\n", c);
printf("Smallest: %d", y);
}
else if (d>a && d>b && d>c) {
if (a<b && a<c){
y = a;
}
else if(b<a && b<c){
y = b;
}
else if(c<a && c<b){
y = c;
}
printf("Largest: %d\n", d);
printf("Smallest: %d", y);
}
return 0;
}
Output:
Enter four integers (separate them with spaces):
21
8
4
52
Largest: 52
Smallest: 4
#include <stdio.h>
#include<conio.h>
int main()
{
int units;
float amount, charge, total_amount;
printf ("Enter total units consumed: ");
scanf ("%d", &units);
Output
Enter total units consumed: 280
Electricity Bill: 1220.00
Program:
# include<stdio.h>
# include<math.h>
int main ()
{
float a, b, c, r1, r2, d;
printf ("Enter the values of a b c: ");
scanf (" %f %f %f", &a, &b, &c);
d= b*b - 4*a*c;
if (d>0)
{
r1 = -b+sqrt (d) / (2*a);
r2 = -b-sqrt (d) / (2*a);
printf ("The real roots = %f %f", r1, r2);
}
else if (d==0)
{
r1 = -b/(2*a);
r2 = -b/(2*a);
printf ("Roots are equal =%f %f", r1, r2);
}
else
printf ("Roots are imaginary");
return 0;
}
Output:
Case 1: Enter the values of a b c:
1
4
3
The real roots = -3.000000 -5.000000
Case 2:
Enter the values of a b c: 1
2
1
Roots are equal =-1.000000 -1.000000
Case 3:
Enter the values of a b c: 1
1
2
Roots are imaginary
switch (ch)
{
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
default:
printf ("Invalid operation\n");
}
printf("The value = %f", result);
return 0;
}
OUTPUT:
Enter first number = 8
Enter second number = 9
Choose operator to perform operations = +
The value = 17.000000
if(year%400 == 0)
{
printf("%d is a leap year.\n", year);
}
else
if(year%100 == 0)
printf("%d is not a leap year.\n", year);
else
if(year%4 == 0 )
printf("%d is a leap year.\n", year);
else
printf("%d is not a leap year.\n", year);
return 0;
}
Output:
Enter a year to check if it is a leap year
2016
2016 a leap year.
if (c == 2)
{
printf ("n is a Prime number");
}
else
{
printf ("n is not a Prime number");
}
return 0;
}
Output:
Enter any number n: 7
n is a Prime number
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x, sum, term;
int i,n;
clrscr ();
printf ("enter the no of terms: \n");
scanf ("%d",&n);
printf ("enter the angle in degrees x: \n");
scanf("%f", &x);
x=(x*3.14)/180;
sum=1;
term=1;
for (i=1; i<=n; i++)
{
term=(term*(-1)*x*x)/((2*i)*(2*i-1));
sum+=term;
}
printf("cos valve of given angle is %f",sum);
getch();
}
Output:
enter the no of terms: 3
enter the angle in degrees x: 30
cos valve of given angle is 0.866158
Output:
Enter the number=151
palindrome number