Dr. A.P.J. Abdul Kalam Technical University, Lucknow
KCS101 / KCS201 Programming for Problem Solving - Using C Language
Lab Exercises
12. Write a program to print the sum of all numbers up to the given number.
/*
File: Prgrm12.c
Author: Aditya Saini
Date: Sept 30, 2019
Description: Program to print the sum of all numbers up to given number.
*/
#include <stdio.h>
int main (void)
{
int number;
int sum;
int i;
//Input number.
printf ("Input number: ");
scanf ("%d", &number);
int sum;
int i;
//Input number.
printf ("Input number: ");
scanf ("%d", &number);
//Finding the sum.
for (sum = 0, i = 1; i <= number; i++)
{
sum = sum + i;
}
//Output the result
printf ("Sum of all numbers up to %d is %d.", number, sum);
{
sum = sum + i;
}
//Output the result
printf ("Sum of all numbers up to %d is %d.", number, sum);
};
Output
Input number: 9
Sum of all numbers up to 9 is 45.
No comments:
Post a Comment
Please do not post spam links.