(XI) C Lab Work - 1 Input Output Functions

C Lab Work - 1 ( Input Output Function)

Input/Output(I/O) Functions

Like other languages, C does not have any built-in input/output statements as part of its syntax. All input/output operations are carried out through printf() and scanf() function. These functions are known as the Standard I/O library because it can take or accept any type of data. Each program that uses a standard input/output function must contain a statement #include<stdio.h> at the beginning. The instruction #include<stdio.h> tells the compiler to search for a file named stdio.h and place its contents at this point in the program. The contents of the header file become part of the source code when it is compiled.
C language consists of some I/O functions like getchar(), putchar(), gets(), puts() which are also defined inside stdio.h header file. The printf() and scanf() functions are known as formatted I/O functions because they can take any type of format of data from the I/O devices. printf() can be used to display some conversion characters along with some unchanged characters. Such conversion characters may include format specifiers.
Syntax:
printf( "control string", arg1, arg2, ......);
Here control string may consist of any simple characters or format conversion specifiers or escape sequences and arg1, arg2,.... are arguments (variables) that represent the individual data item.
Example1:
printf(“Hello”);
Here, the statement consists of simple characters without arguments and will display output ‘Hello'.
Example2:
int a=5;
printf(“%d”,a);
Here, printf() statement contains a format conversion specifier “%d”  and an argument ‘a’ which will display output 5.
Example3:
float a=0.5;
float b=987.8145;
printf(“ \n here are two values %f and %.3f ”, a, b);
Here, printf() statement contains characters "here are .....” , format conversion specifiers “ %f ”, “ %.3f ”, escape sequence ‘\n’ and arguments ‘a’, ‘b’ which will display.
Output: here are two values 0.500000 and 987.814
scanf() can be used to get inputs from the user.
Syntax:
scanf (" control string " , argl, arg2,....);
Here control string may consist of any format conversion specifiers and arg1, arg2,.... are arguments specifying the address of locations where the data is stored. Control string and arguments are separated by commas. For example, if ‘k’ is a variable or argument then '&k' is its address. So, it must be written as &k with scanf function.
Examples: int a;
float b;
char c;
scanf(“%d%f%c”,&a,&b,&c);
Here, scanf() takes three different values: first variable a takes integer decimal like 4,-9,200 etc. , second variable b takes floating point number like 3.14,9.861,-8.99 etc. and the third variable takes a character like 'a','#','5' etc.

Differentiate between printf() and scanf() statements

printf() scanf()
1. It is used to display contents(text/number). 1. It is used to get inputs from the user.
2. It contains any simple characters, format conversion specifiers or escape sequence and arg1, arg2,... arguments(variables) 2. It does not contain simple characters, it contains format conversion specifier and arg1,arg2,..... arguments(variables)
3. Syntax:
printf(“control string”, arg1,arg2,....);
Here, control string may consist of simple string or format conversion specifiers and arg1,arg2,.... are arguments.
3. Syntax:
scanf(“control string”,arg1,arg2);
Here, control strings may consist of format conversion specifiers and arg1,arg2,... are arguments.
4. Example:
main()
{
float a=9861.492492;
printf(“\n %.3f”,a);
}
4. Example:
main()
{
int a;
printf(“Enter a number”);
scanf(“%d”,&a);
}

Other Input/Output Functions

  1. getchar () and putchar():
  2. The getchar() function is used to read (or accept) a single character. It can not take more than one character.
    Syntax:
    variable_name = getchar();
    Here variable_name is a valid C name that has been declared as char type.

    The putchar() function is used to display the character contained in the variable name at the output screen / terminal.
    Syntax:
    putchar(variable_name);
    Where variable_name is a type char containing a character.
    Example:
    #include<stdio.h>
    int main()
    {
    char a;
    printf("Enter a character");
    a=getchar();
    putchar(a);
    }
    In the above example, getchar() function accepts a character which stores in a variable 'a' and putchar() used to display stored character.
  3. gets and puts():
  4. The gets() function is used for a completely different purpose. It reads a string (group of characters) from the standard input and stores it in the given variable.
    It reads a whole line of input until a newline.

    gets (variable);
    Example:
    char name[25];
    It will ask the user for a name and save the name into name.

    The puts() function is used to display text in the monitor which is stored in the variable. But the variable is always string data type.
    Syntax:
    puts (variable);
    or puts("string");
    Example:
    char name[25]={“Alex”};
    puts(name);
    This example will display name “Alex”.
1) Write a program to input three numbers and find sum and average.
Algorithm to print sum and average of three numbers
Step 1: Start
Step 2: Input three numbers as a , b and c
Step 3: Calculate sum=a+b+c and average=sum/3
Step 4: Display sum and avearge
Step 5: Stop
Flowchart to print sum and average of three numbers
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: 2
3
5
Sum of three numbers is 10
Average of three numbers is 3.33
2) Write a program to find area and circumference of circle. (A=3.14xr2,c=2x3.14xr)
Algorithm to calculate area and circumference of a circle
Step 1: Start
Step 2: Input radius as r
Step 3: Use a=3.14*r*r and c=2*3.14*r
Step 4: Display a and c
Step 5: Stop
Flowchart to calculate area and circumference of a circle
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 : 7
Area = 153.86
Circumference = 43.96
3) Write a program to find square and cube root of a number. [use sqrt(),cbrt()].
Algorithm to find square and cube root of a number Step 1: Start Step 2: Enter any number as n Step 3 : Use sqrt for square root and cbrt for cube root Step 4: Display the square root and cube root Step 5: Stop Flowchart to find square and cube root of a number C program to find square root and cube root of a number #include<stdio.h> #include<math.h> int main() { int num,sq,cb; printf("Enter any number : "); scanf("%d",&num); sq=sqrt(num); cb=cbrt(num); printf(" Square Root=%d\n",sq); printf(" Cube Root=%d",cb); return 0; } Output Enter any number : 125 Square Root=11 Cube Root=5
4) Write a program to find simple interest and net amount.[SI=PTR/100,A=SI+P]
Algorithm to calculate and print simple interest(SI) and net amount(A)
Step 1: Start
Step 2: Input p,t,r
Step 3: Use si=p*t*r/100 and a=si+p
Step 4: Display si and a
Step 5: Stop
Flowchart to calculate and print simple interest(SI) and net amount(A)
C program to calculate and print simple interest(SI) and net amount(A)
#include<stdio.h>
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 : 1000
2
12
Simple interest is 240
Net amount is 1240
5) Write a program to convert centigrade temperature into Fahrenheit.[f=1.8c+32]
Algorithm to convert temperature from centigrade into fahrenheit
Step 1: Start
Step 2: Input temperature in centigrade as c
Step 3: Use f=1.8*c+32
Step 4: Display f
Step 5: Stop
Flowchart to convert temperature from centigrade into fahrenheit
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 36
Temperature in Fahrenheit = 96.80
6) Write a program to find the value of S in S=ut+(1/2)*at2
Algorithm to calculate distance covered
Step 1: Start
Step 2: Input initial velocity, time and accelaration as u,t,a
Step 3: Use s=u*t+0.5*a*t*t
Step 4: Display s
Step 5: Stop
Flowchart to calculate distance covered
C program to calculate distance covered
#include<stdio.h>
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 : 10
5
6
Distance covered is 125.00
7) Write a program to find total surface area of a cuboid. [TSA=2(lb+bh+lh)]
Algorithm to find total surface area of a cuboid Step 1: Start Step 2: Input length, breadth and height as l, b and h Step 3: Use a=(lb+bh+lh) Step 4: Print a Step 5 : Stop Flowchart to find total surface area of a cuboid C program to find total surface area of a cuboid #include<stdio.h> int main() { int l,b,h,a; printf("Enter value of l,b and h : "); scanf("%d%d%d",&l,&b,&h); a=2*(l*b+b*h+l*h); printf("Total Surfcace Area =%d",a); return 0; } Output Enter value of l,b and h : 5 3 2 Total Surfcace Area =62
8) Write a program to find sum of two distances measured in kilo meter and meter. For example: Kilometer meter 234 2300 100 300 336 600
Algorithm to find sum of two distances measured in kilo meter and meter Step 1: Start Step 2: Enter km1 , m1 , km2 and m2 value Step 3: Use km3=km1+km2 and m3=m1+m2 Step 4: newkm=km3/1000+km3 and newm=km3%12 Step 5: Print newkm and newm Step 6: Stop Flowchart to find sum of two distances measured in kilo meter and meter C program to find sum of two distances measured in kilo meter and meter #include<stdio.h> int main() { int km1,km2,km3,m1,m2,m3,newkm,newm; printf("Enter km1 , m1 , km2 and m2 value : "); scanf("%d%d%d%d",&km1,&m1,&km2,&m2); km3=km1+km2; m3=m1+m2; newkm=m3/1000+km3; newm=m3%1000; printf("KM=%d Meter=%d",newkm,newm); return 0; } Output Enter km1 , m1 , km2 and m2 value : 234 2300 100 300 KM=336 Meter=600
9) Write C program to find roots of quadratic equation ax2+bx+c=0.
Algorithm to find roots of quadratic equation ax2+bx+c=0 Step 1: Start Step 2: Input value of a,b and c Step 3: Use x1=((-b+sqrt(b*b-4*a*c))/2*a) and x2=((-b-sqrt(b*b-4*a*c))/2*a); Step 4: Print x1 and x2 Step 5: Stop Flowchart to find roots of quadratic equation ax2+bx+c=0 C program to find roots of quadratic equation ax2+bx+c=0 #include<stdio.h> #include<math.h> int main() { int a,b,c; float x1,x2; printf("Enter value of a,b and c : "); scanf("%d%d%d",&a,&b,&c); x1=(float)((-b+sqrt(b*b-4*a*c))/2*a); x2=(float)((-b-sqrt(b*b-4*a*c))/2*a); printf("First root= %0.2f",x1); printf("\n Second root =%0.2f",x2); return 0; } Output Enter value of a,b and c : 1 -8 15 First root= 5.00 Second root =3.00
10) Write C program to find v in v2=u2+2as.
Algorithm to find v in v2=u2+2as Step 1: Start Step 2: Input value of u,a and s Step 3: Use v=sqrt(u*u+2*a*s) Step 4: Print v Step 5: Stop Flowchart to find v in v2=u2+2as C program to find v in v2=u2+2as #include<stdio.h> #include<math.h> int main() { int v,u,a,s; printf("Enter value of u,a and s : "); scanf("%d%d%d",&u,&a,&s); v=sqrt(u*u+2*a*s); printf(" Value of v = %d",v); return 0; } Output Enter value of u,a and s : 2 3 2 Value of v = 4
11) Write C program to convert total number of seconds into hours, minutes and seconds. e.g. for 4850 seconds, total hours =1,minutes=20 and seconds=50
Algorithm to convert total number of seconds into hours, minutes and seconds Step 1: Start Step 2: Input time in seconds (s) Step 3: Use hr =s/3600; s1=s%3600; min = s1/60; sec = s1%60; Step 4: Print hr , min , sec Step 5: Stop Flowchart to convert total number of seconds into hours, minutes and seconds C program to convert total number of seconds into hours, minutes and seconds #include<stdio.h> int main() { int s,hr,min,sec,s1; printf("Enter time in seconds : "); scanf("%d",&s); hr =s/3600; s1=s%3600; min = s1/60; sec = s1%60; printf("Hours=%d Minutes=%d Seconds=%d",hr,min,sec); return 0; } Output Enter time in seconds : 4850 Hours=1 Minutes=20 Seconds=50
12) Write C program to input your name, address and grade. Then print them. Try this with scanf() and gets().
Algorithm to to input your name, address and grade. Then print them Step 1: Start Step 2: Input name, address and grade Step 3: Print name, address and grade Step 4: Stop Flowchart to input your name, address and grade. Then print them C program to input your name, address and grade. Then print them #include<stdio.h> int main() { char name[30],address[30]; int grade; printf("Enter name : "); gets(name); printf("Enter Address : "); gets(address); printf("Enter grade : "); scanf("%d",&grade); printf("Name=%s Address=%s Grade=%d",name,address,grade); return 0; } Output Enter name : Alex Lal Enter Address : Kathmandu Enter grade : 11 Name=Alex Lal Address=Kathmandu Grade=11