CPPM Assignment 1
1.
Write a C program to interchange value of two numbers using third
variable.
#include<Stdio.h>
#include<conio.h>
void
main()
{
int
x,y,temp;
clrscr();
printf("Enter
Integer X : ");
scanf("%d",&x);
printf("Enter
Integer Y : ");
scanf("%d",&y);
printf("Integer
without Swapping are : %d %d",x,y);
temp
=x;
x=y;
y=temp;
printf("\n\nAfter
Swapping the Integers : %d %d",x,y);
getch();
}
Output
Enter
Integer X : 5
Enter
Integer Y: 9
Integer
without Swapping are : 5 9
After
Swapping the Integers : 9 5
2.
Write a C program to convert degree to radians. (Formula:
degree=π/180)
#include<stdio.h>
#include<conio.h>
void
main()
{
float
degree,Convert,Pi;
Pi=3.14;
clrscr();
printf("Enter
The Degree to convert into Radian : ");
scanf("%f",°ree);
Convert=(degree*Pi)/180;
printf("The
Radian Of Entered %f Degree is\n\n %f",degree,Convert);
getch();
}
OUTPUT
Enter
The Degree to convert into Radian : 45
The
Radian Of Entered 45.000000 Degree is 0.785000
3.
Write a C program to input quantity and price of an item and display
total Rs. on screen. Also input discount (%) and display final
payment Rs on screen.
#include<stdio.h>
#include<conio.h>
void
main()
{
int
quan,dis,final_amt,price,total;
clrscr();
printf("Enter
the quantity that has sold :");
scanf("%d",&quan);
printf("Enter
the price of each pieces : ");
scanf("%d",&price);
total=quan*price;
printf("\ntotal
amount :%d",total);
printf("\nEnter
the amount of discount given to customer :");
scanf("%d",dis);
dis=(total*dis)/100;
final_amt=total-dis;
printf("The
customer have to pay total amount of %d",final_amt);
getch();
}
Output
Enter
the quantity that has sold :10
Enter
the price of each pieces : 100
total
amount :1000
Enter
the amount of discount given to customer :20
The
customer have to pay total amount of 800
4.
Write a C program to input a three digit number from user and
calculate sum of first and last digit.
#include<stdio.h>
#include<conio.h>
void
main()
{
int
i,n,sum=0;
clrscr();
printf("Enter
the number to print first and last digit additio : ");
scanf("%d",&n);
if(n>10)
{
sum+=n%10;
}
while(n>=10)
{
n=n/10;
}
sum+=n;
printf("\nSum
of first and last digit is %d",sum);
getch();
}
OutPut
Enter
the number to print first and last digit additio : 786
Sum
of first and last digit is 13
5.
Write a C program to calculate area of trapezium.
#include<stdio.h>
#include<conio.h>
void
main()
{
float
Short_side,long_side,height,area;
clrscr();
printf("Enter
SHORT SIDE of Trapezium : ");
scanf("%f",&Short_side);
printf("Emter
LONG SIDE of Trapezium : ");
scanf("%f",&long_side);
printf("Enter
the HEIGHT of Trapezium : ");
scanf("%f",&height);
area=(0.5)*(Short_side+long_side)*height;
printf("THE
AREA OF TRAPEZIUM IS %f",area);
getch();
}
OUTPUT
Enter
SHORT SIDE of Trapezium : 8
Emter
LONG SIDE of Trapezium : 9
Enter
the HEIGHT of Trapezium : 7
THE
AREA OF TRAPEZIUM IS 59.500000
6.
Write a C program to find largest of three numbers using ternary
operator.
#include<stdio.h>
#include<conio.h>
void
main()
{
int
a,b,c,big;
clrscr();
printf("Enter
Value for A : ");
scanf("%d",&a);
printf("Enter
value for B : ");
scanf("%d",&b);
printf("Enter
the value for C : ");
scanf("%d",&c);
big=a>b?(a>c?a:c):(b>c?b:c);
printf("The
BIGGER(larger) Value of enterd numbers is : %d",big);
getch();
}
OutPut
Enter Value for A : 25
Enter value for B : 20
Enter the value for C : 35
The BIGGER(larger) Value of enterd numbers is : 35
7.
If cost price and selling price of an item are input through the
keyboard, write a C program to determine whether the seller has made
profit or loss and evaluate it.
#include<stdio.h>
#include<conio.h>
void
main()
{
int
cost_price,selling_price,profit,loss;
clrscr();
printf("Enter
the cost of an item :");
scanf("%d",&cost_price);
printf("Enter
the selling price od an item : ");
scanf("%d",&selling_price);
if(cost_price>selling_price)
{
loss=cost_price-selling_price;
printf("The
Shopkeeper has loss of %d",loss);
}
else
{
profit=selling_price-cost_price;
printf("The
shopkeeper having profit of %d",profit);
}
getch();
}
Output
Enter
the cost of an item :5000
Enter
the selling price od an item : 5551
The
shopkeeper having profit of 551
8.
Write a program to read two integer values m and n and to decide and
print whether m is multiple of n.
#include<stdio.h>
#include<conio.h>
void
main ()
{
int
m,n,Div;
clrscr();
printf("Enter
the value of m: ");
scanf("%d",&m);
printf("Enter
the value of n: ");
scanf("%d",&n);
if
(m%n==0)
{
printf("%d
is divisible by %d",m,n);
}
else
{
printf("%d
is not divisible by %n",m,n);
}
getch();
}
Output
Enter the value of m: 5
Enter the value of n: 6
5 is not divisible by 6
9.
Write a program to calculate and print electricity charges for
customer by
accepting
no. of units and customer no.
If
unit <= 200 then 0.50/unit
If
unit <=400 then 100 + 0.65/unit
If
unit <=600 then 230 + 0.80/unit
#include<conio.h>
#include<conio.h>
void
main()
{
int
unit,id_no;
float
charges;
clrscr();
printf("Enter
the Customer ID number : ");
scanf("
%d",&id_no);
printf("Enter
total number unit consumed ny user : ");
scanf("%d",&unit);
if
(unit==0 && unit <=200)
{
charges=(unit*0.50);
printf("Your
ID no. is %d and you have consumed %d units and your charges are
%f",id_no,unit,charges);
}
else
if (unit>200 && unit<=400)
{
charges=100+(unit*0.60);
printf("Your
ID no. is %d and you have consumed %d units and your charges are
%f",id_no,unit,charges);
}
else
if (unit<400 && unit<=600)
{
charges=230+(unit*.80);
printf("Your
ID no. is %d and you have consumed %d units and your charges are
%f",id_no,unit,charges);
}
getch();
}
Output
Enter
the Customer ID number : 128
Enter
total number unit consumed ny user : 350
Your
ID no. is 128 and you have consumed 350 units and your charges are
310.000000
10.
Write a menu driven program that accept three numbers using scanf
statement
and print the following results:
1.
Sum of values
2.
Average of three values
3.
Largest of three values
4.
Smallest of three values
#include<stdio.h>
#include<conio.h>
void
main()
{
int
ch,a,b,c,sum,avg,big,small;
clrscr();
printf("Enter
the Value of A : ");
scanf("%d",&a);
printf("Enter
the value of B : ");
scanf("%d",&b);
printf("Enter
the value of C : ");
scanf("%d",&c);
printf("\n1.Sum
of all three Number\n2.Average of all three number\n3.Bigger of all
three number\n4.Smaller of all three number\n\n Enter your choice.");
scanf("%d",&ch);
switch
(ch)
{
case
1:sum=(a+b+c);
printf("Sum
of all three number is %d",sum);
break;
case
2:avg=(a+b+c)/3;
printf("Avg
of all three number is %d ",avg);
break;
case
3:big=a>b?(a>c?a:c):(b>c?b:c);
printf("Bigger
of all three number is %d",big);
break;
case
4:small=a<b?(a<c?a:c):(b<c?b:c);
printf("Smaller
of three number is %d",small);
break;
}
getch();
}
Output
Enter
the Value of A : 10
Enter
the value of B : 5
Enter
the value of C : 3.
1.Sum
of all three Number
2.Average
of all three number
3.Bigger
of all three number
4.Smaller
of all three number
Enter
your choice.1
Sum of all three number is 18
11.
Write a program using switch case to check whether a character is
vowel or consonant.
#include<stdio.h>
#include<conio.h>
void
main()
{
char
ch;
clrscr();
printf("Enter
the Character to know whether it is vowel or consonant : ");
scanf("%c",&ch);
switch(ch)
{
case
'a':
case
'A':
case
'e':
case
'E':
case
'i':
case
'I':
case
'o':
case
'O':
case
'u':
case
'U':
printf("%c
is a vowel \n",ch);
break;
default:
printf("%c
is consonant.",ch);
break;
}
getch();
}
Output
Enter
the Character to know whether it is vowel or consonant: A
A
is a vowel
12.
Write a program to make a simple calculator using switch statement.
#include<stdio.h>
#include<conio.h>
void
main()
{
int
ch,a,b,sum,sub,mult,div;
clrscr();
printf("Enter
the value of A: ");
scanf("%d",&a);
printf("Enter
the value of B: ");
scanf("%d",&b);
printf("\n
Enter chour choice \n1.Sum\n2.Sub\n3.Mult\n4.Div\n Select you choice
");
scanf("%d",&ch);
switch(ch)
{
case
1:sum=a+b;
printf("Sum
of a and b is : %d",sum);
break;
case
2 :sub=a-b;
printf("Sub
of a and b is : %d",sub);
break;
case
3:mult=a*b;
printf("Mult
od A and b is :%d",mult);
break;
case
4:div=a/b;
printf("Div
of a and b is :%d",div);
break;
}
getch();
}
Output
Enter
the value of A: 10
Enter
the value of B: 5
Enter
chour choice
1.Sum
2.Sub
3.Mult
4.Div
Select
you choice 1
Sum
of a and b is : 15