Explanation
The program finds the sum of two numbers entered by the user using a function.
Header
The code begins with including the standard input-output header file using #include <stdio.h>
.
Function Definition
The program defines a function sum
to calculate the sum of two integers:
int sum(int o, int n)
: Defines the function with two integer parameterso
andn
.return o + n;
: Returns the sum of the two parameters.
Variable Declaration
The main()
function is defined, and two variables are declared:
int d, m
: Variables to hold the input numbers.
Program Description
The program prints a description message for the user to understand its functionality.
User Input
The user is prompted to enter two numbers:
printf("Enter first and second number: ");
: Prompts the user to enter the two numbers.scanf("%d %d", &d, &m);
: Reads the two numbers and stores them ind
andm
.
Sum Calculation
The program calculates the sum of the two numbers using the sum
function:
printf("Sum: %d\n", sum(d, m));
: Calls thesum
function withd
andm
as arguments and prints the result.
Program End
The program ends with return 0;
to indicate successful execution.
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 sum(int o, int n)
{
return o + n;
}
int main()
{
printf("\n\n Program to find sum of two numbers entered by the user through function. \n\n");
// Declare variables to hold the input numbers
int d, m;
// Read the input numbers from the user
printf("Enter first and second number: ");
scanf("%d %d", &d, &m);
// Print the result
printf("Sum: %d\n", sum(d, m));
return 0;
}
Output
Program to find sum of two numbers entered by the user through function.
Enter first and second number: 2 5
Sum: 7