C Lab Work : 1
Theory of Input/Ouput Functions
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
- getchar () and putchar(): The getchar() function is used to read (or accept) a single character. It can not take more than one character.
- gets and puts(): 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.
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.
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. Input three numbers and print sum and average using C program.
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
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);
}
3
5
Sum of three numbers is 10
Average of three numbers is 3.33
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: 23
5
Sum of three numbers is 10
Average of three numbers is 3.33
2. Find out the square root of a number using the C program.
Algorithm to find square root of a number
Step 1: Start
Step 2: Input number as n
Step 3: Use sqrt function to find sqaure root and store it in a
Step 4: Display a
Step 5: Stop
#include<math.h>
main()
{
int n,a;
printf(" Enter a number : ");
scanf("%d",&n);
a=sqrt(n);
printf(" Square root of %d is %d ",n,a);
}
Square root of 25 is 5
Step 1: Start
Step 2: Input number as n
Step 3: Use sqrt function to find sqaure root and store it in a
Step 4: Display a
Step 5: Stop
Flowchart to find square root of a number
C program to find square root of a number
#include<stdio.h>#include<math.h>
main()
{
int n,a;
printf(" Enter a number : ");
scanf("%d",&n);
a=sqrt(n);
printf(" Square root of %d is %d ",n,a);
}
Output
Enter a number : 25Square root of 25 is 5
3. Write a program in C to calculate and print simple interest(SI) and net amount(A), given that SI=PTR/100 and 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
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);
}
2
12
Simple interest is 240
Net amount is 1240
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 : 10002
12
Simple interest is 240
Net amount is 1240
4. Write a program in C to calculate distance using s=ut+1/2at2
Algorithm to calculate distance covered
Step 1: StartStep 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 : 105
6
Distance covered is 125.00
5. Write a program in C to calculate area and circumference of a circle.
Algorithm to calculate area and circumference of a circle
Step 1: StartStep 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 : 7Area = 153.86
Circumference = 43.96
6. Write a program in C to convert temperature from centigrade(c) into fahrenheit(f). [Hint: f=1.8c+32]
Algorithm to convert temperature from centigrade into fahrenheit
Step 1: StartStep 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 36Temperature in Fahrenheit = 96.80
7. Write a program in C to calculate the sum of two distances and the distance is measured in terms of feet and inches.
Algorithm to calculate the sum of two distances and the distance is measured in terms of feet and inches
Step 1: StartStep 2: Input feet1,feet2,inch1,inch2
Step 3: Use feet3=feet1+feet2 and inch3=inch1+inch2
Step 4: newfeet=inch3/12+feet3 and newinch=inch3%12
Step 5: Display newfeet and newinch
Step 6: Stop
Flowchart to calculate the sum of two distances and the distance is measured in terms of feet and inches.
C program to calculate the sum of two distances and the distance is measured in terms of feet and inches.
#include<stdio.h>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
8. Write a program in C to enter a number of days and convert it into years, months and days.
Algorithm to enter a number of days and convert it into years, months and days.
Step 1 StartStep 2: Input days
Step 3: Divide days by 365 and store it in y
Step 4: Remaining days(rd) =days%365
Step 5: For m= rd/30 and d=rd%30
Step 6: Display y,m,d
Step 7: Stop
Flowchart to enter a number of days and convert it into years, months and days
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
9. 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.
10. 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);
}
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.