Explanation
The following code is written in C programming language and is a program that calculates the sum of two numbers entered by the user.
#include <stdio.h>
The first line includes the header file stdio.h
, which stands for Standard Input/Output.
This header
file provides various functions that allow a program to input and output data, such as reading from
the keyboard and
writing to the console.
int main()
The main
function is the entry point of the program, where execution starts. The
int
keyword before the main
function indicates that the function will return an integer
value.
float d, m, j;
The next line declares three floating-point variables d
, m
, and
j
.
printf("\n\n Program to calculate sum aftertaking two numbers as input from user. \n\n");
The printf
function is used to print a message to the console that explains the purpose
of the program.
printf("Enter 2 numbers - (Example: 2.34 5.66) and press enter: ");
The next line prints a prompt to the console, asking the user to enter two numbers.
scanf("%f %f", &d, &m);
The scanf
function is used to read two floating-point numbers entered by the user. The
%f
format specifier is used to indicate that floating-point numbers are being read, and the
& symbol before
the variable names d
and m
is used to pass the addresses of the variables
to the
scanf
function, allowing it to store the input values in those variables.
printf("Sum of the the entered numbers %.2f and %.2f is %.2f\n", d, m, d + m);
The printf
function is used to print the sum of the two numbers, which is calculated as
d + m
. The %.2f
format specifier is used to indicate that a floating-point
number with two
decimal places should be printed.
return 0;
The return 0;
statement at the end of the main
function indicates that the
program executed
successfully and returns an exit code of 0
to the operating system.
Code
/*
* -----------------------------------------------------------
* Logic Building with Computer Programming (CSU1128)
* Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
*
* This code is a part of the educational initiative by dmj.one
* with aim of empowering and inspiring learners in the field of
* Computer Science and Engineering through the respective courses.
*
* (c) 2022, Divya Mohan for dmj.one. All rights reserved.
* -----------------------------------------------------------
*/
#include <stdio.h>
int main()
{
float d, m, j;
printf("\n\n Program to calculate sum aftertaking two numbers as input from user. \n\n");
printf("Enter 2 numbers - (Example: 2.34 5.66) and press enter: ");
scanf("%f %f", &d, &m);
printf("Sum of the the entered numbers %.2f and %.2f is %.2f\n", d, m, d + m);
return 0;
}
Output
Program to calculate sum aftertaking two numbers as input from user.
Enter 2 numbers - (Example: 2.34 5.66) and press enter: 3 20 Sum of the the entered numbers 3.00 and 20.00 is 23.00