TRINITY INTERNATIONAL SS & COLLEGE
Lab Report on C programming
(COMPUTER SCIENCE-427)
SUBMITTED BY: SUBMITTED TO:
Sujal Rana Lal Krishna Malik
GRADE: XI ‘MF1'
REGISTRATION NUMBER: 21610
KATHMANDU, NEPAL
2021
TRINITY INTERNATIONAL SS & COLLEGE
Lab sheet #1
(Input/Output Functions)
Lab date:- 2078/3/15
Submission date:- 2078/3/27
Internal signature External Signature
Objectives
The objective of a program using input and output functions is to know about
different input and output functions and their use in different programs. It will help
us to be familiar with some frequently used I/O functions like scanf(),printf(),
gets(),puts, putchar() ,getchar() etc. Having used functions, we will be able to input
and output numerical and string related data easily
Table of contents
Theory ................................................. Error! Bookmark not defined.
Program to input three numbers and print sum and average. ............... 6
Program to find the square root of a number ........................................ 8
Program to calculate Simple interest and net amount ........................ 10
Program to calculate distance using s=ut+1/2at^2 ............................. 11
Program to calculate area and circumference of a circle .................... 13
Program to convert Centigrade (C) into Fahrenheit (F) ..................... 15
Program to calculate sum of two distances measured in terms feet and inches 17
Program to enter number of days and convert it into years, months and days 19
Complete the programs and discuss the output .................................. 21
Complete the programs and discuss the output .................................. 23
Conclusion .......................................................................................... 25
THEORY
Input/Output(I/O) Functions:
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.
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;.
getchar () and putchar():
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.
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.
scanf()
The scanf function is used to read formatted input data. The format in which input data is to be
provided is specified by the scanf function itself as it's first parameter. The scanf function is written
as -
scanf(<control string>, &address1, &address2, . . . , &addressn);
Note that there must be the same number of format specifiers and addresses as there are input data.
For instance, in the following example:
scanf("%d %f",&x,&y)
q1.Program to input three numbers and print sum and average.
Algorithm
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 average
Step 5: Stop
Flowchart
CODE
#include<stdio.h>
int 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);
return 0;}
Output
Enter three numbers: 1
2
3
Sum of the numbers is 6
Average of the numbers
Q2.Program to find the square root of a number
Algorithm
Step 1: Start
Step2:Input number as n
Step 3: Use sqrt function to find square root and store it in a
Step 4: Display a
Step 5: Stop
Flowchart
Code
#include<stdio.h>
int main(){
int n,a;
printf(" Enter a number : ");
scanf("%d",&n);
a=sqrt(n);
printf(" Square root of %d is %d ",n,a);
return 0;}
Output
Enter a number: 16
Square root of 16 is 4
Q3Program to calculate Simple interest and net amount
Algorithm
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
Code
#include<stdio.h>
int 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);
return 0;}
Output
Enter principal, time and rate: 1000
2
10
Simple interest is 200
Net amount is 1200
Q4.Program to calculate distance using s=ut+1/2at^2
Algorithm
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
Code
#include<stdio.h>
int 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);
return 0;}
Output
Enter initial velocity, time and acceleration: 10
5
6
Distance covered is 125.00
Program no.5
Program to calculate area and circumference of a circle
Algorithm
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
Code
#include<stdio.h>
int 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);
return 0;}
Output
Enter radius : 1
Area = 3.14
Circumference = 6.28
Q6.Program to convert Centigrade (C) into Fahrenheit (F)
Algorithm
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
Program
#include<stdio.h>
int main()
float c,f;
printf(" Enter temperature in centigrade ");
scanf("%f",&c);
f=1.8*c+32;
printf(" Temperature in Fahrenheit = %.2f ",f);
return 0;
Output
Enter temperature in centigrade 0
Temperature in Fahrenheit = 32
Q7.Program to calculate sum of two distances measured in terms
feet and inches
Step 1: Start
Step 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
Program
#include<stdio.h>
int 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);
return 0;
Output
Enter feet1, feet2, inch1 and inch2: 2
3
4
5
Feet=6 Inch=8
Q8.Program to enter number of days and convert it into years,
months and days
Algorithm
Step 1 Start
Step 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
Program
#include<stdio.h>
int 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);
return 0;
}
Output
Enter the days: 370 Year=1 Month=0 Day=5
Q9.Complete the programs and discuss the output
#include<stdio.h>
int main()
float x;
int x1=5;
int x2=2;
x=x1/x2;
printf("%f",x);
return 0;
Output
2.000000 In this
case, implicit type conversion is used which convert the final data(x) into float data type not x1
and x2.
#include<stdio.h>
int main()
float x;
int x1=5;
int x2=2;
x=(float)x1/x2;
printf("%f",x);
return 0;
Output
2.500000
Conclusion: In this case, explicit type conversion is used which convert the x1 and x2 into float
before the division.
Q10.Complete the programs and discuss the output
Program 1
#include<stdio.h>
int main()
int j;
int i=5;
j=++i;
printf(" i=%d\n j=%d",i,j);
return 0;
Output
i=6
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.
Program 2
#include<stdio.h>
int main()
int j;
int i=5;
j=i++;
printf(" i=%d\n j=%d",i,j);
return 0;
Output
i=6
j=5
Conclusion: (i++) is Postfix increment unary operator first assigns the value on the left and then
increments the operand
Conclusion
It was a wonderful time in the lab and classroom while learning. The outcome of the learning is
great and is very similar to the prediction made in the beginning. We learned about input and
output functions in detail. It helped us a lot in our programming. We came to know its syntax,
semantic with example. I hope that this will help us in future in my project work. At last, I would
like to thank my class subject teacher for his support and cooperation.