Search

Sum of first N natural numbers in C || C Programs

Code -

#include <stdio.h> 
#include <conio.h> 
void main()  
{  
 int num, i, sum = 0; // declare local variables 
    printf(" Enter a positive number: ");  
    scanf("%d", &num); // take any positive number 
 // executes until the condition remains true. 
 for (i = 0; i <= num; i++)  
    {  
        sum = sum + i; // at each iteration the value of i is added to the sum variable 
    }  
 // display the sum of natural number 
    printf("\n Sum of the first %d number is: %d", num, sum);   
    getch();  
}  

Output -

Enter a positive number: 25
Sum of the first 25 number is: 325

6 views0 comments

Related Posts

See All

Program of Random Number in C || C Programs

Code - #include <stdio.h> #include <conio.h> #include <stdlib.h> void main() { // use rand() function to generate the number printf (" The random number is: %d", rand()); pri

Getchar() function in C || C Programs

Code - #include <stdio.h> #include <conio.h> void main() { char c; printf ("\n Enter a character \n"); c = getchar(); // get a single character printf(" You have passed ")

  • 1024px-Telegram_logo.svg
  • download
  • YouTube