Input and Output are the foundation of every C program. Input allows users to enter data into a program, while Output displays information on the screen. In C programming, the most commonly used functions are scanf() for taking input and printf() for displaying output. C Input and Output practice questions with solutions help to understand the concepts.
Understanding these functions is essential because almost every real-world C program requires user interaction. In this chapter, you’ll practice beginner-friendly C Input and Output programs with complete solutions, sample outputs, explanations, and concepts covered.
1. C Program to Read and Display an Integer
Problem Statement
Write a C program to accept an integer from the user and display it.
C Solution
#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d", &number);
printf("You entered: %d", number);
return 0;
}
Sample Output
Enter an integer: 25
You entered: 25
Explanation
scanf()accepts an integer from the user.%dis the format specifier for integers.&numberpasses the memory address of the variable.printf()displays the entered value.
Concepts Covered
- scanf()
- printf()
- Integer Input
- Format Specifier
%d
2. C Program to Read and Display a Floating-Point Number
Problem Statement
Write a C program to accept a floating-point number and display it.
C Solution
#include <stdio.h>
int main()
{
float price;
printf("Enter product price: ");
scanf("%f", &price);
printf("Price = %.2f", price);
return 0;
}
Sample Output
Enter product price: 199.75
Price = 199.75
Explanation
%fis used to read floating-point values.%.2fdisplays the value with two digits after the decimal point.
Concepts Covered
- Float Input
- scanf()
- printf()
- Format Specifier
%f
3. C Program to Read and Display a Character
Problem Statement
Write a C program to accept a character from the user and display it.
C Solution
#include <stdio.h>
int main()
{
char letter;
printf("Enter a character: ");
scanf(" %c", &letter);
printf("You entered: %c", letter);
return 0;
}
Sample Output
Enter a character: A
You entered: A
Explanation
%cis used to read a character.- A space before
%cinscanf()ignores any leftover newline character from previous input. printf()displays the entered character.
Concepts Covered
- Character Input
- scanf()
- Character Variables
- Format Specifier
%c
4. C Program to Read and Display a String
Problem Statement
Write a C program to accept a string (name) from the user and display it.
C Solution
#include <stdio.h>
int main()
{
char name[50];
printf("Enter your name: ");
scanf("%s", name);
printf("Hello, %s!", name);
return 0;
}
Sample Output
Enter your name: Rahul
Hello, Rahul!
Explanation
char name[50]creates a character array to store the string.%sis the format specifier used to read and print strings.scanf("%s", name)reads a single word (without spaces).
Note: If the input contains spaces (e.g., Rahul Kumar),
scanf("%s")reads only the first word. Later chapters will coverfgets()for reading complete lines.
Concepts Covered
- String Input
- Character Array
%sFormat Specifier- scanf()
5. C Program to Read Integer, Float, and Character Together
Problem Statement
Write a C program to accept an integer, a floating-point number, and a character from the user, then display all the entered values.
C Solution
#include <stdio.h>
int main()
{
int age;
float percentage;
char grade;
printf("Enter Age: ");
scanf("%d", &age);
printf("Enter Percentage: ");
scanf("%f", &percentage);
printf("Enter Grade: ");
scanf(" %c", &grade);
printf("\n----- Student Details -----\n");
printf("Age : %d\n", age);
printf("Percentage : %.2f\n", percentage);
printf("Grade : %c\n", grade);
return 0;
}
Sample Output
Enter Age: 21
Enter Percentage: 89.50
Enter Grade: A
----- Student Details -----
Age : 21
Percentage : 89.50
Grade : A
Explanation
scanf("%d", &age)reads an integer.scanf("%f", &percentage)reads a floating-point number.scanf(" %c", &grade)reads a character.printf()displays all the values in a formatted output.
This program demonstrates how different data types can be accepted from the user within the same program.
Concepts Covered
- Multiple User Inputs
- Integer Input
- Float Input
- Character Input
- Formatted Output
- scanf()
- printf()
6. C Program to Calculate the Sum of Two Numbers Using User Input
Problem Statement
Write a C program to accept two integers from the user and display their sum.
C Solution
#include <stdio.h>
int main()
{
int first, second, sum;
printf("Enter first number: ");
scanf("%d", &first);
printf("Enter second number: ");
scanf("%d", &second);
sum = first + second;
printf("Sum = %d", sum);
return 0;
}
Sample Output
Enter first number: 15
Enter second number: 25
Sum = 40
Explanation
The program accepts two integers using scanf(), adds them using the + operator, and displays the result.
Concepts Covered
- User Input
- Addition
- Variables
- Arithmetic Operator
7. C Program to Calculate the Average of Three Numbers
Problem Statement
Write a C program to accept three numbers from the user and calculate their average.
C Solution
#include <stdio.h>
int main()
{
float first, second, third, average;
printf("Enter first number: ");
scanf("%f", &first);
printf("Enter second number: ");
scanf("%f", &second);
printf("Enter third number: ");
scanf("%f", &third);
average = (first + second + third) / 3;
printf("Average = %.2f", average);
return 0;
}
Sample Output
Enter first number: 10
Enter second number: 20
Enter third number: 30
Average = 20.00
Explanation
The average is calculated by adding all three numbers and dividing the total by 3.
Concepts Covered
- Float Input
- Average Calculation
- Arithmetic Operators
8. C Program to Display ASCII Value of a User-Entered Character
Problem Statement
Write a C program to accept a character from the user and display its ASCII value.
C Solution
#include <stdio.h>
int main()
{
char character;
printf("Enter a character: ");
scanf(" %c", &character);
printf("ASCII Value = %d", character);
return 0;
}
Sample Output
Enter a character: A
ASCII Value = 65
Explanation
Characters are internally stored as integers. Printing a character using %d displays its ASCII value.
Concepts Covered
- Character Input
- ASCII Values
- Type Conversion
9. C Program to Swap Two Numbers Entered by the User
Problem Statement
Write a C program to accept two numbers from the user and swap their values using a temporary variable.
C Solution
#include <stdio.h>
int main()
{
int first, second, temp;
printf("Enter first number: ");
scanf("%d", &first);
printf("Enter second number: ");
scanf("%d", &second);
temp = first;
first = second;
second = temp;
printf("After Swapping:\n");
printf("First Number = %d\n", first);
printf("Second Number = %d", second);
return 0;
}
Sample Output
Enter first number: 10
Enter second number: 20
After Swapping:
First Number = 20
Second Number = 10
Explanation
A temporary variable stores the first value before the numbers are exchanged.
Concepts Covered
- User Input
- Swapping Variables
- Temporary Variable
10. C Program to Calculate Simple Interest Using User Input
Problem Statement
Write a C program to calculate Simple Interest using the formula:
Simple Interest = (Principal × Rate × Time) / 100
C Solution
#include <stdio.h>
int main()
{
float principal, rate, time, simpleInterest;
printf("Enter Principal Amount: ");
scanf("%f", &principal);
printf("Enter Rate of Interest: ");
scanf("%f", &rate);
printf("Enter Time (Years): ");
scanf("%f", &time);
simpleInterest = (principal * rate * time) / 100;
printf("Simple Interest = %.2f", simpleInterest);
return 0;
}
Sample Output
Enter Principal Amount: 5000
Enter Rate of Interest: 8
Enter Time (Years): 2
Simple Interest = 800.00
Explanation
The program accepts the principal amount, interest rate, and time period from the user, then calculates the Simple Interest using the standard formula.
Concepts Covered
- User Input
- Mathematical Formula
- Float Variables
- Arithmetic Operators
11. C Program to Calculate the Area of a Rectangle
Problem Statement
Write a C program to accept the length and width of a rectangle from the user and calculate its area.
C Solution
#include <stdio.h>
int main()
{
float length, width, area;
printf("Enter length: ");
scanf("%f", &length);
printf("Enter width: ");
scanf("%f", &width);
area = length * width;
printf("Area of Rectangle = %.2f", area);
return 0;
}
Sample Output
Enter length: 12
Enter width: 8
Area of Rectangle = 96.00
Explanation
The area of a rectangle is calculated using the formula:
Area = Length × Width
Concepts Covered
- User Input
- Mathematical Formula
- Float Variables
12. C Program to Calculate the Area of a Circle
Problem Statement
Write a C program to accept the radius of a circle and calculate its area.
C Solution
#include <stdio.h>
int main()
{
float radius, area;
printf("Enter radius: ");
scanf("%f", &radius);
area = 3.14159 * radius * radius;
printf("Area of Circle = %.2f", area);
return 0;
}
Sample Output
Enter radius: 7
Area of Circle = 153.94
Explanation
The area of a circle is calculated using:
Area = π × r²
where π is approximately 3.14159.
Concepts Covered
- User Input
- Circle Formula
- Float Variables
13. C Program to Convert Temperature from Celsius to Fahrenheit
Problem Statement
Write a C program to convert Celsius temperature into Fahrenheit.
C Solution
#include <stdio.h>
int main()
{
float celsius, fahrenheit;
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);
fahrenheit = (9.0 / 5.0) * celsius + 32;
printf("Temperature in Fahrenheit = %.2f", fahrenheit);
return 0;
}
Sample Output
Enter temperature in Celsius: 30
Temperature in Fahrenheit = 86.00
Explanation
The conversion formula is:
Fahrenheit = (9/5 × Celsius) + 32
Concepts Covered
- User Input
- Temperature Conversion
- Arithmetic Expressions
14. C Program to Calculate the Perimeter of a Rectangle
Problem Statement
Write a C program to calculate the perimeter of a rectangle.
C Solution
#include <stdio.h>
int main()
{
float length, width, perimeter;
printf("Enter length: ");
scanf("%f", &length);
printf("Enter width: ");
scanf("%f", &width);
perimeter = 2 * (length + width);
printf("Perimeter = %.2f", perimeter);
return 0;
}
Sample Output
Enter length: 15
Enter width: 10
Perimeter = 50.00
Explanation
The perimeter of a rectangle is calculated using:
Perimeter = 2 × (Length + Width)
Concepts Covered
- User Input
- Rectangle Formula
- Arithmetic Operations
15. C Program to Calculate the Square of a Number
Problem Statement
Write a C program to accept a number from the user and calculate its square.
C Solution
#include <stdio.h>
int main()
{
int number, square;
printf("Enter a number: ");
scanf("%d", &number);
square = number * number;
printf("Square = %d", square);
return 0;
}
Sample Output
Enter a number: 9
Square = 81
Explanation
The square of a number is obtained by multiplying the number by itself.
Concepts Covered
- User Input
- Arithmetic Operations
- Integer Variables
Chapter Summary
In this chapter, you learned the fundamentals of Input and Output in C programming. You practiced accepting user input using scanf() and displaying formatted output using printf(). You also solved practical programs involving integers, floating-point numbers, characters, strings, mathematical calculations, temperature conversion, area and perimeter formulas, and simple interest calculations. These concepts form the basis of interactive C programs and are essential before learning decision-making statements and loops.
Key Takeaways
printf()is used to display output on the screen.scanf()is used to accept input from the user.- Different data types require different format specifiers (
%d,%f,%c,%s). - The address operator (
&) is required withscanf()for variables. - Strings are stored using character arrays.
- User input makes programs interactive.
- Mathematical formulas can be implemented easily using arithmetic operators.
- Proper formatting improves program readability.
- Input and Output functions are used in almost every C program.
- Mastering I/O functions is essential before learning conditional statements and loops.
Frequently Asked Questions (FAQs)
1. What is Input and Output in C?
Input allows users to provide data to a program, while Output displays processed information on the screen.
2. Which function is used to display output in C?
The printf() function is used to display output.
3. Which function is used to accept user input?
The scanf() function is used to read input from the keyboard.
4. Why is the & operator used in scanf()?
The & operator passes the memory address of a variable so that scanf() can store the entered value.
5. Which format specifier is used for integers?
%d is used for integer values.
6. Which format specifier is used for floating-point numbers?
%f is used for floating-point values.
7. How are strings stored in C?
Strings are stored in character arrays and are commonly read using %s with scanf().
8. Why are Input and Output functions important?
Input and Output functions allow programs to interact with users, process data, display results, and build real-world applications such as calculators, billing systems, banking software, and management systems.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
