C program to Addition, Subtraction, Multiplication, and Division of input two numbers.
Introduction:
In this article, we will discuss how to write a C program to addition, subtraction, multiplication, and division of input two numbers. These four basic operations are fundamental in many computer programs and are the building blocks for more complex mathematical calculations.
Problem Statement:
Write a C program that takes two numbers as input from the user and performs the following arithmetic operations on them: addition, subtraction, multiplication, and division. The program should output the results of each operation to the console.
To solve this problem, we need to break it down into smaller tasks. Here's a step-by-step approach:
Step 1: Get Input from the User We need to get two numbers as input from the user. We can use the scanf function to read input from the user.
Step 2: Perform Addition To perform addition, we simply add the two numbers and store the result in a variable. We can then print the result to the console using printf.
Step 3: Perform Subtraction To perform subtraction, we subtract the second number from the first number and store the result in a variable. We can then print the result to the console using printf.
Step 4: Perform Multiplication To perform multiplication, we multiply the two numbers and store the result in a variable. We can then print the result to the console using printf.
Step 5: Perform Division To perform division, we divide the first number by the second number and store the result in a variable. We can then print the result to the console using printf. Note that we need to handle the case where the second number is zero, as division by zero is undefined.
Programming Coding:
#include <stdio.h>
int main() {
int num1, num2;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
int sum = num1 + num2;
printf("Sum = %d\n", sum);
int diff = num1 - num2;
printf("Difference = %d\n", diff);
int product = num1 * num2;
printf("Product = %d\n", product);
if (num2 == 0)
{
printf("Cannot divide by zero.\n");
}
printf("Cannot divide by zero.\n");
}
else
No comments:
Post a Comment