Managing Input and Output Operations
- Data can be provided to the program variables in two ways – non-interactive method & interactive input method.
- stdio – standard input-output libraray.
- Two types of i/o functions - Formatted i/o functions & Unformatted i/o functions
- scanf() – formatted i/p function.
- scanf(“control-string”, address_list);
- scanf(“%[^\n]“, message); //go on typing any ASCII printable characters till the new line(\n) character is pressed.
- printf() – formatted o/p function.
- printf(“control-string”, address_list);
- Unformatted input functions – getchar(), gets()
- Unformatted output functions – putchar(), puts()
Program 1
//C program to accept 3 numbers and compute their sum and average
#include<stdio.h>
void main()
{
int num1,num2,num3,sum;
float averag;
printf(“Enter Three Numbers\n”);
scanf(“%d %d %d”,&num1,&num2,&num3);
sum=num1+num2+num3;
averag=sum/3.0;
printf(“Sum = %d\nAverage = %f\n”,sum,averag);
}
Program 2
/*Program to accept the radius of a circle and compute the area and it’s perimeter*/
#include<stdio.h>
void main()
{
float radius,pmeter,PI=3.142,Area;
printf(“Enter the radius\n”);
scanf(“%f”,&radius);
Area=PI*radius*radius;
pmeter=2.0*PI*radius;
printf(“Area of a circle = %7.2f\n”,Area);
printf(“Perimeter = %7.2f\n”,pmeter);
}
Program 3
/*Program to accept the temperature in Farenheit and convert it into Celcius. C=(f-32.0)/1.8*/
#include<stdio.h>
#include<conio.h>
void main()
{
float ct,ft;
clrscr();
printf(“Enter the temperature in Farenheit\n”);
scanf(“%f”,&ft);
ct=(ft-32.0)/1.8;
printf(“Farenheit temperature = %6.2f\n”,ft);
printf(“Celcius temperature = %6.2f\n”,ct);
}
Program 4
/*Program to accept the principle amount, rate of interest and number of years & compute the simple interest*/
#include<stdio.h>
void main()
{
float p,r,si;
int t;
printf(“Enter the values for p,r,t\n”);
scanf(“%f %f %d”,&p,&r,&t);
si=(p*r*t)/100.0;
printf(“P=%f\t R=%f\t T=%d\n”,p,r,t);
printf(“Simple Interest = %f\n”,si);
}
Program 5
/*Program to exchange the values of two variables
1.using a dummy variable, and
2.without using a dummy variable*/
#include<stdio.h>
void main()
{
int x,y,dummy;
printf(“Enter the values of x and y\n”);
scanf(“%d %d”,&x,&y);
printf(“Using a dummy variable\n”);
dummy=x;
x=y;
y=dummy;
printf(“x=%d y=%d\n”,x,y);
printf(“Without using a dummy variable\n”);
x=x+y;
y=x-y;
x=x-y;
printf(“x=%d y=%d\n”,x,y);
}
Related posts:








Leave your response!