Skip to main content

C Program to find the sum of natural numbers using while loop

Programming Languages

Welcome to coding world

C Program to find the sum of natural numbers using while loop


#include <stdio.h>

int main()

{

    int n, count, sum = 0;

 

    printf("Enter the value of n(positive integer): ");

    scanf("%d",&n);

 

    /* When you use while loop, you have to initialize the

     * loop counter variable before the loop and increment

     * or decrement it inside the body of loop like we did

     * for the variable "count"

     */

    count=1;

    while(count <= n){

                sum = sum + count;

                count++;

    }

 

    printf("Sum of first %d natural numbers is: %d",n, sum);

 

    return 0;

}

Output:

 Enter the value of n(positive integer): 7

Sum of first 7 natural numbers is: 28

Popular posts from this blog

Good Morning Wishes

Aeza khan and Danish taimoor love story

Ayeza Khan bridal collection 2021 Ayeza Khan celebrate 10M on Instagram Danish Taimoor Danish Taimoor born on 16 February 1983 is a Pakistani actor and model.  He began his career in 2005 and seemed in "Mystery Series" (Do saal baad, Dracula); both episodes were directed by Imran khokhar and aired on Indus Vision. He is notable for his role in Urdu drama series' and telefilms in Pakistan.  Notable dramas include:  Haseena Moin's Meri Behen Maya  Mannchalay  Jab we Wed  Shert  Sari Bhool Humari thi  Aik Pal He made his film debut in Jalaibee in March 2015, directed by Yasir Jaswal

Ayeza Khan Bridal collection 2021

Pakistan Showbiz Ayeza khan celebrate 10M on Instagram Ayeza Khan & Danish Taimoor love story