Q:
Write a C Program to find out the sum of first n numbers
belongs to collection: C Basic Solved Programs – C Programming
C Basic Solved Programs – C Programming
- Write a C Program to Print Hello World Program
- Write a C Program to calculate simple interest
- C program to convert Total days to year, month and days
- Write a C program to find Sum and Average of two numbers
- Write a C Program to check whether number is EVEN or ODD
- C Program to find largest number among three numbers
- Write a C Program to calculate AREA of rectangle
- Write a C program to calculate Gross Salary of an employee
- C program to convert temperature from Fahrenheit to Celsius and vice versa
- C Program to display sum of even and product of odd numbers
- Write a C Program to check occurence of digit in a given number
- Write a C Program to find out the sum of first n numbers
- Write a C Program to generate table of a given input number
- Write a C Program to check triangle is a equilateral, isosceles or scalene
- C Program to check whether character is vowel or not
- Write a C Program To find the biggest and smallest number and positions in the given array
- Write a C Program to find the gross salary and net salary
- Write a C Program to find the roots of quadratic equation
- Write a C Program to find area and circumference of circle
- C program to find largest and smallest of three numbers
- Write a C program to find power of a number ( x^n ) using pow function
- Write a C Program to Print integer value in Decimal, Octal, Hexadecimal
- Write a C program to find size of variables using sizeof() operator
- Write a C program to find HCF of two numbers
- Write a C program to multiply two numbers using plus operator
- Write a C program to demonstrate example of global and local scope
- Write a C program to convert feet to inches
- Write a C program to print ASCII value of entered character
- Write a C Program to input values into an array and display them
- Write a C Program to pass array elements to a function
- Write a C Program to pass array to function to calculate sum
- C Program to print value and address of elements of an array without pointer
- Write a C Program to perform Call By Value and Call By Reference methods
- Write a C Program to display student details using structure members
- Write a C Program to understand how structure members sent to function
- Write a C Program to understand how structure variable returned from function
LOGIC : :
The program asks the user to enter the number of terms he/she would like to evaluate the sum of, which is stored in variable n. Another variable i is assigned value 1, which is used for counting the number of terms in the loop and also as a natural number which is added to the result.
The variable sum stores sum of the natural numbers. The sum is initialized to 0 to avoid addition of garbage numbers.As the program enters the loop, value of i is added to the variable sum which now equals the sum of all natural numbers till i. Then the value of i is increased by 1.
The loop continues as long as the value of i is less than or equal to n as we are calculating the sum of first n natural numbers. When, the loop exits, the result is displayed on the screen.
Below is the source code for C Program to find out the sum of first n numbers which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
OUTPUT : :
Above is the source code for C Program to find out the sum of first n numbers which is successfully compiled and run on Windows System.The Output of the program is shown above .
need an explanation for this answer? contact us directly to get an explanation for this answer