You are currently viewing Write a program in C to display the sum of two numbers.

Write a program in C to display the sum of two numbers.

Share this to everyone:

Following is a program to display the sum of two numbers.

#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int a,b,s;

printf(“Enter any two numbers\n”);

scanf(“%d%d”,&a,&b);

s=a+b;

printf(“The sum is %d\n”,s);

getch();

}

Output:

Enter any two numbers

2

3

The sum is 5

Let’s break down the code step by step:

int a, b, s;
This line declares three variables a, b, and s of type int. a and b will be used to store the numbers entered by the user, while s will store their sum.
printf("Enter any two numbers\n");

This line uses the printf function to display the message "Enter any two numbers" on the console. It prompts the user to input two numbers.
scanf("%d%d", &a, &b);

The scanf function is used to read input from the user. In this case, it expects two integers (%d) to be entered by the user, which are stored in the variables a and b. The & operator is used to pass the address of the variables where the input should be stored.
s = a + b;

This line calculates the sum of the two numbers entered by the user and assigns it to the variable s. The + operator is used for addition.

printf("The sum is %d\n", s);

This line uses the printf function to display the message “The sum is” followed by the value of s on the console. The %d is a format specifier used to represent an integer value, and it is replaced by the value of s during the execution.

Leave a Reply