basic

C program to addition of two numbers | 5 different Methods

C program to the addition of two numbers

In this tutorial, we will discuss the  C program to the addition of two numbers 

Addition of two numbers

In this post, we are going to learn how to find the sum of two numbers through different 5 ways in C programming language

Program 1:

Sum of two numbers – the standard method

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num1=15,num2=35, sum;//Declare variables num1,num2,sum
    sum=num1+num2;
    printf("Sum of two integer : %d",sum);
    getch();
    return 0;
}

When the above code is executed it produces the following output

Sum of two integer: 50

In this program

  1. Integer variable num1,num2 are declared
  2. Integer variable num1,num2 are initialized
  3. the num1 and num 2 both are added together  and the value is added to the sum
  4. Then, the program is Displayed the output (Sum of two integers) using  printf() function

Program 2

Sum of two numbers- entered by user

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num1,num2,sum;
    printf("Enter the first number: ");
    scanf("%d",&num1);
    printf("Enter the second number: ");
    scanf("%d",&num2);
    sum=num1+num2;
printf("Sum of two numbers is:%d ",sum);
getch();
    return 0;
}

 

When the above code is executed it produces the following output

Enter the first number: 12
Enter the second number: 34 
Sum of two numbers is: 46

Approach

  1. Declare variables num1,num2,sum
  2. The program requires input from the user
  3. Then the user enters the input value for num1, num2(variables)
  4. The program will read the input using scanf() function and stores in the variables num1 and num2
  5. the num1 and num 2 both are added together  and the value is added to the sum
  6. Then, the program Displays the value of the sum using printf() function

Program 3

Sum of two numbers- Using function

#include <stdio.h>
#include <stdlib.h>
int sum_Num(int,int);//function prototype - 1
int main()
{
    int num1,num2, sum;//Declare variable num1,num2,sum - 2
    printf("Enter two integer for find sum: ");  -3
    scanf("%d %d",&num1,&num2);    - 4,5
    sum=sum_Num(num1, num2); - 7
    printf("Sum of given integer : %d",sum);
    getch();
    return 0;
}
int sum_Num(int a, int b){  - 6
int result=a+b;
return result;
}

When the above code is executed it produces the following output

Enter two integer for find sum: 125
275
Sum of given numbers: 400

Approach

  1. Declare a function named as sum_Num() with two int parameter
  2. Declare variables num1,num2,sum
  3. The program requires input from the user
  4. Then the user enters the input value for num1, num2
  5. The program will read the input using scanf() function and stores in the variables num1 and num2 respectively
  6. Define the function (sum_Num()) for find sum
  7. Call the function to produce output
  8. Then, the program Displays the value of the sum using printf() function

 

Program 4

Sum of two numbers- Using recursion

#include <stdio.h>
#include <stdlib.h>
int sum_Num(int,int);//function prototype
int main()
{
    int num1,num2, sum;//Declare variables num1,num2,sum
    printf("Enter two integer for find sum: ");
    scanf("%d %d",&num1,&num2);
    sum=sum_Num(num1, num2);//function call
    printf("Sum of given integer : %d",sum);
    getch();
    return 0;
}
int sum_Num(int a, int b){//Function definition
    if(b==0)               //recursive function
        return a;
    else
        return(1+sum_Num(a,b-1));
}

When the above code is executed it produces the following output

Enter the two integer for find sum: 48
32
Suym of given integers : 80

Approach

  1. Declare a function named as sum_Num() with two int parameter
  2. Declare variables num1,num2,sum
  3. The program requires input from the user
  4. Then the user enters the input value for num1, num2
  5. The program will read the input using scanf() function and stores in the variables num1 and num2 respectively
  6. Define the recursive function (sum_Num()) for find sum
  7. Call the function to produce output
  8. Then, the program Displays the value of the sum using printf() function

 

Program 5

Sum of two numbers- Using pointers

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int *ptr1,*ptr2;//declare pointer variables
    int num1,num2,tot;//Declare normal variable

    printf("Enter two integer for find sum: ");
    scanf("%d %d",&num1,&num2);
    ptr1=&num1;
    ptr2=&num2;
    tot = *ptr1 + *ptr2;
    printf("Sum of given integer : %d",tot);
    getch();
    return 0;
}

When the above code is executed it produces the following output

Enter two integer for find sum: 23
34
Sum of given integer: 57

Approach

  1. Declare two pointer variables ptr1,ptr2.
  2. Declare variables num1,num2,tot.
  3. The program requires input from the user
  4. Then the user enters the input value for num1, num2
  5. The program will read the input using scanf() function and stores in the variables num1 and num2 respectively
  6. The addresses of variable num1,num2 are assigned to pointer variables ptr1, ptr2 respectively
  7. the pointer variables ptr1 and ptr2 both are added together  and the value is added to the tot
  8. Then, the program Displays the value of tot using printf() function

Similar post

Java program to the sum of two numbers

Python program to the sum of two numbers

C++ program to the sum of two numbers

 

Suggested for you

The operator in C language

Function in C language

Pointer in C language

 

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

5 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

5 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

6 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

6 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

6 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

6 months ago

This website uses cookies.