Experiment 1 – Basic I/O and Arithmetic
Aim
To understand and implement basic input/output operations in C programming using printf() and scanf() functions, and to perform basic arithmetic operations (addition, subtraction, multiplication, division, and modulus) on user-input values. This experiment introduces fundamental programming concepts that form the foundation for all C programming.
Through this experiment, students will learn to interact with users through console I/O, understand format specifiers, and perform basic mathematical computations using C operators.
Learning Outcomes
After completing this experiment, students will be able to:
- Use
printf()for formatted output with various format specifiers - Use
scanf()for reading user input with proper format specifiers - Understand and use arithmetic operators (+, -, *, /, %)
- Handle different data types (int, float, double) in I/O operations
- Write programs that interact with users through console
- Perform basic mathematical calculations in C programs
- Understand the importance of format specifiers in I/O operations
- Debug common I/O errors like missing & in scanf or wrong format specifiers
Algorithm
The algorithm for basic I/O and arithmetic operations involves reading input, performing calculations, and displaying results:
Algorithm Steps:
- Start: Begin program execution
- Include Header: Include
stdio.hfor I/O functions - Declare Variables: Declare variables for two numbers and results (sum, diff, prod, quot, mod)
- Display Prompt: Use
printf()to ask user for first number - Read Input: Use
scanf()to read first number into variable - Display Prompt: Use
printf()to ask user for second number - Read Input: Use
scanf()to read second number - Calculate Sum: sum = a + b
- Calculate Difference: diff = a - b
- Calculate Product: prod = a * b
- Calculate Quotient: quot = a / b (use type casting for float division)
- Calculate Modulus: mod = a % b
- Display Results: Use
printf()to display all results with appropriate format specifiers - Stop: End program execution
Procedure
- Include the standard input/output header file
stdio.h - Declare variables to store two numbers and results of arithmetic operations
- Use
printf()to display prompts asking the user to enter two numbers - Use
scanf()to read the input values from the user - Perform arithmetic operations: addition (+), subtraction (-), multiplication (*), division (/), and modulus (%)
- Display the results using
printf()with appropriate format specifiers - Compile and execute the program to verify the output
Flowchart
START │ ├─→ Include stdio.h │ ├─→ Declare variables: a, b, sum, diff, prod, quot, mod │ ├─→ Display "Enter first number: " │ ├─→ Read value of 'a' using scanf() │ ├─→ Display "Enter second number: " │ ├─→ Read value of 'b' using scanf() │ ├─→ Calculate: sum = a + b │ ├─→ Calculate: diff = a - b │ ├─→ Calculate: prod = a * b │ ├─→ Calculate: quot = a / b │ ├─→ Calculate: mod = a % b │ ├─→ Display "Sum = " + sum │ ├─→ Display "Difference = " + diff │ ├─→ Display "Product = " + prod │ ├─→ Display "Quotient = " + quot │ ├─→ Display "Modulus = " + mod │ └─→ END
Program Code
#include <stdio.h>
int main() {
int a, b;
int sum, diff, prod, mod;
float quot;
// Input two numbers
printf("Enter first number: ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);
// Perform arithmetic operations
sum = a + b;
diff = a - b;
prod = a * b;
quot = (float)a / b; // Type casting for accurate division
mod = a % b;
// Display results
printf("\n--- Results ---\n");
printf("Sum: %d + %d = %d\n", a, b, sum);
printf("Difference: %d - %d = %d\n", a, b, diff);
printf("Product: %d * %d = %d\n", a, b, prod);
printf("Quotient: %d / %d = %.2f\n", a, b, quot);
printf("Modulus: %d %% %d = %d\n", a, b, mod);
return 0;
}Sample Input and Output
Sample 1:
Input:
Enter first number: 15 Enter second number: 4
Output:
--- Results --- Sum: 15 + 4 = 19 Difference: 15 - 4 = 11 Product: 15 * 4 = 60 Quotient: 15 / 4 = 3.75 Modulus: 15 % 4 = 3
Sample 2:
Input:
Enter first number: 100 Enter second number: 7
Output:
--- Results --- Sum: 100 + 7 = 107 Difference: 100 - 7 = 93 Product: 100 * 7 = 700 Quotient: 100 / 7 = 14.29 Modulus: 100 % 7 = 2
Use Case / Real-world Relevance
Basic I/O and arithmetic operations form the foundation of all programming tasks. Understanding these concepts is essential for:
- Calculator Applications: Building simple to complex calculators that perform mathematical operations
- Financial Software: Calculating totals, differences, percentages, and interest in banking and accounting applications
- Data Processing: Performing calculations on user inputs in forms, surveys, and data entry systems
- Scientific Computing: Basic mathematical computations in scientific and engineering applications
- Game Development: Calculating scores, health points, damage, and other game mechanics
- E-commerce: Computing prices, discounts, taxes, and totals in shopping applications
Mastery of input/output operations and arithmetic is crucial as these are used in virtually every program you will write. Understanding format specifiers and proper I/O handling prevents common errors and makes your programs more robust and user-friendly.
Viva Questions
Q1: What is the difference between printf() and scanf()?
printf() is used for formatted output (displaying data), whilescanf() is used for formatted input (reading data). printf() doesn't require & before variables, but scanf() requires & (address operator) before variables (except arrays/strings) to store input values.
Q2: Why do we use & in scanf() but not in printf()?
scanf() needs the memory address of variables to store input values, so we use & (address operator). printf() only needs the value to display, so no & is needed. Arrays and strings are exceptions because their name represents the address.
Q3: What are format specifiers? List common ones.
Format specifiers tell printf/scanf how to interpret data. Common ones: %d (int),%f (float), %lf (double),%c (char), %s (string),%u (unsigned int). Using wrong specifiers causes undefined behavior.
Q4: What is the difference between / and % operators?
/ (division) returns the quotient (result of division), while% (modulus) returns the remainder. For example: 15 / 4 = 3, 15 % 4 = 3. Modulus only works with integer operands.
Q5: Why do we use type casting in division (float)a / b?
When dividing two integers, C performs integer division (truncates decimal part). Type casting one operand to float ensures floating-point division, giving accurate decimal results. Without casting, 15 / 4 = 3, but (float)15 / 4 = 3.75.
Q6: What happens if we divide by zero in C?
Division by zero causes undefined behavior. For integers, it may crash or produce garbage. For floats, it may produce infinity (inf) or NaN (Not a Number). Always check for zero before division to prevent errors.
Q7: Can we use scanf() to read multiple values in one statement?
Yes, scanf() can read multiple values: scanf("%d %d", &a, &b);. Values can be separated by spaces, tabs, or newlines. The format string should match the input format for reliable reading.
Q8: What is the return value of printf() and scanf()?
printf() returns the number of characters printed (or negative on error).scanf() returns the number of successfully read items (or EOF on end of file). These return values can be used for error checking.
Related Links
🔹 Author: Dr. J. Siva Ramakrishna
🔹 Institution: Narayana Engineering College, Gudur
🔹 Last Updated: 9 January 2026