Lab 3: Input/Output Statements in C
Lab Report Information
Lab No.: 3
Title: Input/Output Statements in C
Course: Computer Programming (CT 101), I Year I Part
Credit: Important Notes
C Programming Lab Report: Theory for Lab 3
Input/Output Statements in C
These are standard library functions used to get input from a user and display output. This lab report covers both major types of I/O statements.
Formatted Input/Output
printf(): Used to display formatted output. It uses a control string with format specifiers (e.g.,%d,%f) to print data in a specified format.scanf(): Used to read formatted input from the user. It also uses a control string to know what type of data to expect.
Unformatted Input/Output
getchar()&putchar(): Used for single character I/O.getchar()reads one character, andputchar()prints one character.gets()&puts(): Used for string I/O.gets()reads a line of text, andputs()prints a string followed by a newline.
C Programming Lab Report: Programs for Lab 3
1. Program for Character I/O
Question:
Write a program in C to input a character from the user and display the entered character using getchar() and putchar().
Program Code:
#include <stdio.h>
#include <conio.h>
void main() {
char ch;
printf("\nEnter a character: ");
ch = getchar();
printf("\nYou entered: ");
putchar(ch);
getch();
}
Output:
You entered: a
Algorithm:
- Start
- Declare a character variable `ch`.
- Read a character using
getchar()and store it in `ch`. - Print the character `ch` using
putchar(). - Stop
Flowchart:
2. Program for String I/O
Question:
Write a program in C to input a string from the user and display the entered string using gets() and puts().
Program Code:
#include <stdio.h>
#include <conio.h>
void main() {
char ch[100];
printf("\nEnter a string: ");
gets(ch);
printf("\nYou entered: ");
puts(ch);
getch();
}
Output:
You entered:
my name is diwakar.
Algorithm:
- Start
- Declare a character array `ch[100]`.
- Read a string using
gets(). - Display the string using
puts(). - Stop
Flowchart:
3. Program for Formatted Student Data I/O
Question:
Write a program to input the name, roll, marks, address, and phone number of a student using scanf() and display the information using printf().
Program Code:
#include <stdio.h>
#include <conio.h>
void main() {
char name[10], add[20], ph[10];
int r;
float m;
clrscr();
printf("\nEnter name: ");
scanf("%s", name);
printf("\nEnter roll no.: ");
scanf("%d", &r);
printf("\nEnter marks: ");
scanf("%f", &m);
printf("\nEnter address: ");
scanf("%s", add);
printf("\nEnter phone number: ");
scanf("%s", ph);
printf("\nNAME\tROLL\tMARKS\tADDRESS\tPHONE");
printf("\n%s\t%d\t%.2f\t%s\t%s", name, r, m, add, ph);
getch();
}
Output:
Enter roll no.: 28
Enter marks: 38.5
Enter address: Suryagadhi
Enter phone number: 9746425835
NAME ROLL MARKS ADDRESS PHONE
Diwakar 28 38.50 Suryagadhi 9746425835
Algorithm:
- Start
- Declare variables for name, address, phone, roll, and marks.
- Read all student data from the user.
- Display the entered data in a formatted table.
- Stop
Flowchart:
4. Program for Complex Number Addition
Question:
Write a program in C to add two complex numbers entered by the user and display the result.
Program Code:
#include <stdio.h>
#include <conio.h>
void main() {
int a, b, c, d, e, f;
printf("\nEnter real part and imaginary part of num1: ");
scanf("%d %d", &a, &b);
printf("\nEnter real part and imaginary part of num2: ");
scanf("%d %d", &c, &d);
e = a + c;
f = b + d;
printf("\nThe sum of the given numbers is %d + %di", e, f);
getch();
}
Output:
Enter real part and imaginary part of num2: 5 7
The sum of the given numbers is 8 + 12i
Algorithm:
- Start
- Declare integer variables a, b, c, d, e, f.
- Read real (a) and imaginary (b) parts of the first number.
- Read real (c) and imaginary (d) parts of the second number.
- Calculate the sum of real parts: e = a + c.
- Calculate the sum of imaginary parts: f = b + d.
- Display the result in the format “e + fi”.
- Stop
Flowchart:
f = b + d
5. Program for Temperature Conversion
Question:
Write a program to convert a temperature entered in degrees Centigrade into Fahrenheit.
Program Code:
#include <stdio.h>
#include <conio.h>
void main() {
float c, f;
printf("Enter temp. in degree centigrade: ");
scanf("%f", &c);
f = 1.8 * c + 32;
printf("The temp. in Fahrenheit is %.2f F", f);
getch();
}
Output:
The temp. in Fahrenheit is 122.00 F
Algorithm:
- Start
- Declare float variables c and f.
- Read temperature in Centigrade (c).
- Calculate Fahrenheit: f = 1.8 * c + 32.
- Display the temperature in Fahrenheit (f).
- Stop
Flowchart:
Discussion and Conclusion
In this lab on Input/Output Statements in C, we learned about the different I/O functions available in C. We used `printf()`, `scanf()`, `putchar()`, `getchar()`, `puts()`, and `gets()` functions for taking input from the user and displaying it on the screen. We learned the basic syntax of these input/output functions. Through the exercises in this C Programming Lab Report, we gained practical experience in handling different data types and formatting them according to specific requirements. We identified and fixed a logical error during the temperature conversion, reinforcing the importance of careful code review.
