Lab 3: Input/Output Statements in C – C Programming Lab Report

Lab 3: Input/Output Statements in C

A C Programming Lab Report on 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, and putchar() prints one character.
  • gets() & puts(): Used for string I/O. gets() reads a line of text, and puts() 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:

Enter a character: a
You entered: a

Algorithm:

  1. Start
  2. Declare a character variable `ch`.
  3. Read a character using getchar() and store it in `ch`.
  4. Print the character `ch` using putchar().
  5. Stop

Flowchart:

Start
Declare char ch
Input character (ch)
Display character (ch)
Stop

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:

Enter a string: my name is diwakar.
You entered:
my name is diwakar.

Algorithm:

  1. Start
  2. Declare a character array `ch[100]`.
  3. Read a string using gets().
  4. Display the string using puts().
  5. Stop

Flowchart:

Start
Declare char ch[100]
Input string (ch)
Display string (ch)
Stop

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 name: Diwakar
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:

  1. Start
  2. Declare variables for name, address, phone, roll, and marks.
  3. Read all student data from the user.
  4. Display the entered data in a formatted table.
  5. Stop

Flowchart:

Start
Declare variables
Input name, roll, marks, add, ph
Display name, roll, marks, add, ph
Stop

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 num1: 3 5
Enter real part and imaginary part of num2: 5 7
The sum of the given numbers is 8 + 12i

Algorithm:

  1. Start
  2. Declare integer variables a, b, c, d, e, f.
  3. Read real (a) and imaginary (b) parts of the first number.
  4. Read real (c) and imaginary (d) parts of the second number.
  5. Calculate the sum of real parts: e = a + c.
  6. Calculate the sum of imaginary parts: f = b + d.
  7. Display the result in the format “e + fi”.
  8. Stop

Flowchart:

Start
Input a, b, c, d
e = a + c
f = b + d
Display “e + fi”
Stop

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:

Enter temp. in degree centigrade: 50
The temp. in Fahrenheit is 122.00 F

Algorithm:

  1. Start
  2. Declare float variables c and f.
  3. Read temperature in Centigrade (c).
  4. Calculate Fahrenheit: f = 1.8 * c + 32.
  5. Display the temperature in Fahrenheit (f).
  6. Stop

Flowchart:

Start
Input temp in C (c)
f = 1.8 * c + 32
Display temp in F (f)
Stop

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.

Scroll to Top