Sum of Two Numbers in C | 40+ Exercises

2
207
Two Numbers Sum

Write a C program to print the sum of the two numbers, Both inputs should be entered by the user. The program needs to pass all the conditions for example, the number may be Integer, Floats, and Double.

Problem Statement: The first program will ask the user to enter the first number then again program ask for the second number, then print the sum or addition of both numbers in the new line.

Required Skills: To solve this problem you have to know about the Input-output, Format Specifiers, and Addition.

Explanations:

First Step: As we know that for this problem we need three variables, first_number, second_number, and sum, and their datatypes we are taking double for all type of addition.

Second Step: Second Step is to take an input from the users and store the value in the first variable in this case the first variable is “first_number“. and repeat the step for the second value, and store the second number in “second_number” variable.

Third Step: Now the most important part of the program is to add both numbers using Addition operators and assign or store the value of adding in the third variable which is “sum“.

Last Step: Print the value of the sum. This is the last step. Please read the comments in the source code for every step.

C Program is tested on Dev-C++ V-5.11.

C Program to Sum of Two Numbers

#include <stdio.h>
/*
  C Program Code to Find the Sum of Two Numbers
 */
int main()
{
    double first_number, second_number, sum;
    
    /*
      Input by User
    */
    printf("Enter the First Number: ");
    /*
      First Number Entered by User
    */	
    scanf("%lf", &first_number);
	
    printf("Enter the Second Number: ");
    /*
      Second Number Entered by User
    */	
    scanf("%lf", &second_number);
    
    /* Adding First and Second Number with Addition Operater */
	
    sum = first_number + second_number;
    
    /* Printing the Sum of Both Numbers */
	
    printf("\nSum of %lf and %lf is = %lf\n", first_number, second_number, sum);
    
    return 0;
}

The output of Two Numbers Sum

Output with source code Sum

This is a beginner level for newbie coders or programmers, this is a part of 40+ Basic Programming Exercises and Solutions in C Language.

Similar Exercises

  • Perform All Arithmetic Operations – Addition, Substation, Multiplication, and Division | User input.
  • Input/Output of All Basic Data Types.
  • Print 1 to 100 Numbers
  • Greatest of Three Numbers
  • Print Even or Odd
  • Area of Triangle – Mathematics based
  • LCM and GCD of Two Numbers
  • Find Area of an Equilateral Triangle
  • Print All the Odd Number Till ‘N’
  • Swap Two Numbers Using 3rd Variable

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here