C program to calculate and print sum of two number

Problem In Hand

C Program to take input for two numbers calculate and print their sum

Sum of two numbers program in C is discussed below in various methods

Without using function
Using functions with out passing arguments
Using function by passing arguments

Process and Solution Of Problem

Take input for two numbers and store them in two variables
calculate and store sum of the number in a third variable
further display the result



Solution : Without using function
Program/Source Code

#include <stdio.h>
 int main()
 {
     int a,b,c;
     printf("Enter 1st no ");
     scanf("%d",&a);
     printf("Enter 2nd no ");
     scanf("%d",&b);
     c=a+b;
     printf("Sum = %d",c);
     return 0;
 }

Program Explanation

On execution of the program

* user enteres the first number and it is stored in a variable “a”
* user enteres the second number and it is stored in a variable “b”
* further calculation is performed and sum of values stored in “a” and “b” are stored in a variable “c” i.e. c=a+b;
* then the result is displayed on the screen

Output:

Enter 1st no 10
Enter 2nd no 20
Sum = 30


Solution : Using function Without Passing Arguments
Program/Source Code

#include <stdio.h>
void sum()
{
    int a,b,c;
    printf("Enter 1st no ");
    scanf("%d",&a);
    printf("Enter 2nd no ");
    scanf("%d",&b);
    c=a+b;
    printf("Sum = %d\n",c);
}

int main()
{
    sum();
    return 0;
}

Output:

Enter 1st no 10
Enter 2nd no 20
Sum = 30



Solution : Using function Passing Arguments
Program/Source Code

#include <stdio.h>
void sum(int a,int b)
{
    int c;
    c=a+b;
    printf("Sum = %d\n",c);
}

int main()
{
    int a,b;
    printf("Enter 1st no ");
    scanf("%d",&a);
    printf("Enter 2nd no ");
    scanf("%d",&b);
    sum(a,b);
    return 0;
}

Output:

Enter 1st no 100
Enter 2nd no 200
Sum = 300


You May Also Like:

C Program to find largest of 3 numbers
C Program to print table of a number
C program to check number is +ve, -ve or zero
C program to check number is prime number or not
C program to convert total inches to feet and inches.


Previous : Sum Of Two Numbers

Next : Product Of Three Number

All Programs      C Simple Programs      If Condition Programs      Switch Statement Programs      Loop Programs       Conversion Programs      Pattern Programs       Single Dimension Numeric Array programs       Double Dimension Numeric Array programs      Single Dimension Character Array programs

Tutorials

Technical Questions

Interview Questions

C Programming
C++ Programming
Class 11 (Python)
Class 12 (Python)
C Language
C++ Programming
Python

C Interview Questions
C++ Interview Questions


Number Of Visitors

0238237