Monday, 7 March 2016

C Program to Find Sum of Natural Numbers Using While Loop

Problem :- Write A C Program to Find Sum of Natural Numbers Using While Loop

Logic :- What is Natural Number ?
Natural Numbers Are Positive numbers Start With 1 Not 0 ( Zero )
So idea is simple use loop ans initialize with 1 and run into last terms ( Till you Want ) ,you can also print the sum of natural number using formula there are 2 method for solve.
Method 1 :-Using Formula

Natural Number = (N*(N+1))/2

Method 2:- Using Loop

while(count<=n)
{
sum+=count;
++count;
}

Try Yourself C++ Program To Check Number Is Armstrong Or Not

Solution :-

Method 1:-

#include<stdio.h>
int main()
{
  //Ghanendra Yadav
    int n, count=1, sum=0;
    
    while(1)
    {
printf("\nEnter The Natural Number :\n");
    scanf("%d",&n);
    
while(count<=n)
    {
        sum+=count;
        ++count;
    }
    
printf("\nSum Of Natural Number Is  = %d\n",sum);
    
}
return 0;
}

Method 2:-

#include<stdio.h>
int main()
{
  //Ghanendra Yadav
    int n,sum=0;
    
    while(1)
    {
printf("\nEnter The Natural Number :\n");
    scanf("%d",&n);
    
sum=(n*(n+1))/2;
printf("\nSum Of Natural Number Is  = %d\n",sum);
    
}
return 0;
}

Output:-

C Program to Find Sum of Natural Numbers Using While Loop

Extreme Recommended:- Like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date. If you have any Query or Question you can ask in the group, I will Try To Solve your Query and try to answers of your Questions withing 24 Hours, You can also Email me or comment below Please suggest to your Friends to join and like our page and don't forget to Subscribe. Enter your Email and click to subscribe.

Ghanendra Yadav

Ghanendra Yadav

Hello, I Am Ghanendra Yadav Owner of This Blog, I am professional Blogger and Programmer. I Love Programming, Logo Making, And Banner Designing. My Highest Qualification is MCA From NIT Warangal. You Can Find Me On Social Media Through Below Link And If You Have Any Query Related To Programming And Other Subject Comment Below or You Can Mail Me I Will Try To Answer Within 24 Hours Email:- yghanendra@student.nitw.ac.in

Find me on Social Media

Facebook | Twitter | Google+ | RSS Feed

No comments:

Post a Comment