C-Programming
related to Input / Output Function
C program to print sum and average of three numbers
#include<stdio.h>main()
{
int a,b,c,sum=0;
float average=0;
printf("Enter three numbers: ");
scanf("%d%d%d",&a,&b,&c);
sum=a+b+c;
average=(float)sum/3;
printf(" Sum of three numbers is %d \n ",sum);
printf(" Average of three numbers is %0.2f ",average);
}
Output
Enter three numbers: 24
6
Sum of three numbers is 12
Average of three numbers is 4
C program to calculate and print simple interest(SI) and net amount(A)
main()
{
int p,t,r,si,a;
printf("Enter principal, time and rate : ");
scanf("%d%d%d",&p,&t,&r);
si=p*t*r/100;
a=si+p;
printf(" Simple interest is %d \n ",si);
printf(" Net amount is %d ",a);
}
Output
Enter principal, time and rate : 10002
12
Simple interest is 240
Net amount is 1240
C program to calculate the sum of two distances and the distance is measured in terms of feet and inches.
main()
{
int feet1,feet2,feet3,newfeet,inch1,inch2,inch3,newinch;
printf(" Enter feet1,feet2,inch1 and inch2 : ");
scanf("%d%d%d%d",&feet1,&feet2,&inch1,&inch2);
feet3=feet1+feet2;
inch3=inch1+inch2;
newfeet=inch3/12+feet3;
newinch=inch3%12;
printf(" Feet=%d Inch=%d ",newfeet,newinch);
}
Output
Enter feet1,feet2,inch1 and inch2 : 23
14
11
Feet=7 Inch=1
C program to enter a number of days and convert it into years, months and days.
#include<stdio.h>main()
{
int days,y,m,d,rd;
printf(" Enter the days: ");
scanf("%d",&days);
y=days/365;
rd=days%365;
m=rd/30;
d=rd%30;
printf(" Year=%d Month=%d Day=%d",y,m,d);
}
Output
Enter the days: 400Year=1 Month=1 Day=5
C program to calculate distance covered
main()
{
float u,t,a,s;
printf(" Enter initial velocity , time and acceleration : ");
scanf("%f%f%f",&u,&t,&a);
s=u*t+0.5*a*t*t;
printf(" Distance covered is %0.2f ",s);
}
Output
Enter initial velocity , time and acceleration : 105
6
Distance covered is 125.00
C program to calculate area and circumference of a circle
#include<stdio.h>main()
{
float r,a,c;
printf(" Enter radius : ");
scanf("%f",&r);
a=3.14*r*r;
c=2*3.14*r;
printf(" Area = %.2f \n Circumference = %.2f ",a,c);
}
Output
Enter radius : 7Area = 153.86
Circumference = 43.96
C program to convert temperature from centigrade into fahrenheit
#include<stdio.h>main()
{
float c,f;
printf(" Enter temperature in centigrade ");
scanf("%f",&c);
f=1.8*c+32;
printf(" Temperature in Fahrenheit = %.2f ",f);
}
Output
Enter temperature in centigrade 36Temperature in Fahrenheit = 96.80
Complete the following programs ans discuss the outputs.
Program 1
float x;int x1=5;
int x2=2;
x=x1/x2;
printf("%f",x);
Program 2
float x;int x1=5;
int x2=2;
x=(float) x1/x2;
printf("%f",x);
Answer-
Completed Program 1
#include<stdio.h>main()
{
float x;
int x1=5;
int x2=2;
x=x1/x2;
printf("%f",x);
}
Completed Program 2
#include<stdio.h>main()
{
float x;
int x1=5;
int x2=2;
x=(float)x1/x2;
printf("%f",x);
}
Output of Program 1
2.000000Conclusion: In this case, implicit type conversion is used which convet the final data(x) into float data type not x1 and x2.
Output of Program 2
2.500000Conclusion: In this case, explicit type conversion is used which convert the x1 and x2 into float before the division.
Complete the following programs ans discuss the outputs.
Program 1
int j;int i=5;
j=++i;
printf(" i=%d\n j=%d",i,j);
Program 2
int j;int i=5;
j=i++;
printf(" i=%d\n j=%d",i,j);
Answer-
Completed Program 1
#include<stdio.h>
main()
{
int j;
int i=5;
j=++i;
printf(" i=%d\n j=%d",i,j);
}
main()
{
int j;
int i=5;
j=i++;
printf(" i=%d\n j=%d",i,j);
}
j=6
Conclusion: (++i) is Prefix increment unary operator first adds 1 to the operand and then the result is assigned to the variable on the left.
j=5
Conclusion: (i++) is Postfix increment unary operator first assigns the value on the left and then increments the operand.
#include<stdio.h>
main()
{
int j;
int i=5;
j=++i;
printf(" i=%d\n j=%d",i,j);
}
Completed Program 2
#include<stdio.h>main()
{
int j;
int i=5;
j=i++;
printf(" i=%d\n j=%d",i,j);
}
Output of Program 1
i=6j=6
Conclusion: (++i) is Prefix increment unary operator first adds 1 to the operand and then the result is assigned to the variable on the left.
Output of Program 2
i=6j=5
Conclusion: (i++) is Postfix increment unary operator first assigns the value on the left and then increments the operand.
0 Comments