Digital जीवन

Free Online Education for India...

Full width home advertisement

Computer Basic

C Programming

Engineering Graphics

Post Page Advertisement [Top]

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);

               //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);
               
               return 0;
};

Output

Input number: 9
Sum of all numbers up to 9 is 45.



No comments:

Post a Comment

Please do not post spam links.

Bottom Ad [Post Page]

| Designed by Colorlib