w3resource

C Exercises: Calculate the sum of the two integers

C Basic Declarations and Expressions: Exercise-9 with Solution

Write a C program that accepts two integers from the user and calculates the sum of the two integers.

Pictorial Presentation:

C Programming: Calculate the sum of the two integers

C Code:

#include <stdio.h>
int main() 
{
    int x, y, sum; // Declare variables for two integers and their sum
    
    // Prompt user for input and store in 'x'
    printf("\nInput the first integer: "); 
    scanf("%d", &x);
    
    // Prompt user for input and store in 'y'
    printf("\nInput the second integer: ");
    scanf("%d", &y);
    
    sum = x + y; // Calculate the sum of 'x' and 'y'
    
    // Print the sum
    printf("\nSum of the above two integers = %d\n", sum);
    
    return 0; // Indicate successful execution
}

Sample Output:

Input the first integer: 25                                            
                                                                       
Input the second integer: 38                                           
                                                                       
Sum of the above two integers = 63

Flowchart:

C Programming Flowchart: Calculate the sum of the two integers

C Programming Code Editor:

Previous: Write a C program to convert specified days into years, weeks and days.
Next: Write a C program that accepts two integers from the user and calculate the product of the two integers.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.