C program to print sum of two numbers using functions

Ex: Write a C program to print sum of two numbers using function. How to write a C program to print sum of two numbers using function. C program to print sum of two numbers using function.

Input from user:


Enter two numbers: 35 15


Expected output:


Sum = 50





  Step by step logic of the given program:


1. First declare function give it meaningful name add().


2. Inside the main() accept input (two numbers) from user, declare two varibles say no1 & no2 .

3. inside the sum pass no1 and no2 to the function using: sum= add(no1, no2);

4. After that print sum.


5. In the body of add() add n1 and n2 and store it some varible say sum.


After that return sum.







  C program to print sum of two numbers using functions:

#include<stdio.h>
int add(int n1,int n2); /*function declaration*/

int main()
{
    /*Variable declaration*/
    int no1,no2, sum;

    /*Input two numbers from user*/
    printf("Enter two numbers: ");
    scanf("%d%d", &no1,&no2);

    
    sum=add(no1, no2);

    /*Print value of sum*/
    printf("Sum = %d",sum);

    return 0;
}


int add(int n1,int n2)
{
    int sum= n1+n2;

    /*Return value of sum to the main*/
    return sum;
}


Above program shows the following output:


C program to print sum of two numbers using functions


No comments:

Post a Comment

If you have any doubts, please discuss here...👇