Course Content
Detailed Content of Programming in C
0/1
Introduction
0/1
Structure of C program
0/1
Answers of ‘C’ Pointers Programs
0/1
About Lesson

Write a C program that takes two integer inputs from the user, performs addition, subtraction, multiplication, and division operations, and displays the results. Use appropriate data types.

#include <stdio.h>

int main() {
// Declare variables to store user input and results
int num1, num2;
float sum, difference, product, quotient;

// Input: Get two integer numbers from the user
printf(“Enter first integer: “);
scanf(“%d”, &num1);

printf(“Enter second integer: “);
scanf(“%d”, &num2);

// Process: Perform addition, subtraction, multiplication, and division
sum = num1 + num2;
difference = num1 – num2;
product = num1 * num2;

// Check for division by zero before performing division
if (num2 != 0) {
quotient = (float)num1 / num2; // Type casting to float for accurate division
} else {
printf(“Error: Division by zero is undefined.n”);
return 1; // Exit the program with an error code
}

// Output: Display the results
printf(“Sum: %d + %d = %.2fn”, num1, num2, sum);
printf(“Difference: %d – %d = %.2fn”, num1, num2, difference);
printf(“Product: %d * %d = %.2fn”, num1, num2, product);
printf(“Quotient: %d / %d = %.2fn”, num1, num2, quotient);

return 0; // Exit the program successfully
}