w3resource

C Exercises: Calculate the sum of three numbers getting input in one line

C Input Output statement: Exercise-8 with Solution

Write a program in C to calculate the sum of three numbers with getting input in one line separated by a comma.

Pictorial Presentation:

C Input Output: Calculate the sum of three numbers getting input in one line

Sample Solution:

C Code:

#include <stdio.h>
int num1,num2,num3;         /* declaration of three variables */  
int sum;
char line_text[50];      /* line of input from keyboard */ 
  
int main() 
{  
    printf("Input three numbers separated by comma : "); 
	fgets(line_text, sizeof(line_text), stdin);
	sscanf(line_text, "%d, %d, %d", &num1, &num2, &num3);
    sum = num1+num2+num3; 
    printf("The sum of three numbers : %d\n", sum);  
    return(0);  
}  

Sample Output:

Input three numbers seperated by comma : 5,10,15                                                              
The sum of three numbers : 30 

Flowchart:

C Programming Input Output Flowchart: Calculate the sum of three numbers getting input in one line.

C Programming Code Editor:

Improve this sample solution and post your code through Disqus.

Previous: Write a program in C that reads a firstname, lastname and year of birth and display the names and the year one after another sequentially.
Next: Write a C program to perform addition, subtraction, multiplication and  division of two numbers.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

C Programming: Tips of the Day

How to increment a pointer address and pointer's value?

First, the ++ operator takes precedence over the * operator, and the () operators take precedence over everything else.

Second, the ++number operator is the same as the number++ operator if you're not assigning them to anything. The difference is number++ returns number and then increments number, and ++number increments first and then returns it.

Third, by increasing the value of a pointer, you're incrementing it by the sizeof its contents, that is you're incrementing it as if you were iterating in an array.

Ref : https://bit.ly/3vMewPt