C语言教程
C语言控制语句
C语言函数
C语言数组
C语言指针
C语言字符串
C语言数学函数
C语言结构
C语言文件处理
C预处理器

C语言N个自然数总和

我们知道自然数包含从1、2、3到n个数或无穷大的所有正数。例如,假设我们计算前25个数字的总和。这意味着我们开始将数字从1加到给定的数字25,此过程称为第N个自然数的总和。在本主题中,我们将学习如何使用 C 程序查找前n个数字的和。
C中前N个自然数之和

数学公式

以下是使用数学公式来查找n个自然数之和的表示形式:
Sum of n natural number = n * (n + 1) / 2
其中n定义自然数。
假设我们要计算总和在前20个自然数中,我们需要输入一个数学公式来求和:
Sum = 20 * (20 + 1) / 2 = 20 * 10.50 = 210
Or 
20 * (20 + 1) /2 = 10 * 21 = 210

伪代码

int i, sum = 0, num input positive number i = 0 do sum = sum + i i = i + 1 iterate the value of i < =num display the sum of the first natural number.

使用循环

让我们创建一个C程序,该程序使用 for循环。
SumOfNaturalNumber1.c
#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();
}
输出:
Enter a positive number: 25
Sum of the first 25 number is: 325

在循环中使用

让我们创建一个C程序,该程序使用。
SumOfNaturalNumber2.c
#include <stdio.h>
#include <conio.h>
void main()
{
    int num, i, sum = 0; // initialize and declare the local variables
    printf("Enter a positive number : ");
    scanf("%d", &num); // take a value up to which find the sum of n natural number
    i = 0;
    while (i <= num) // define the while loop and i should be less than num
    {
        sum = sum + i; // store the sum of natural number
        i++; // increment by 1
    }
    // print the sum of natural number
    printf(" \n Sum of first %d natural number is : %d", num, sum);
    getch();
}
输出:
Enter a positive number: 20
Sum of the first 20 natural number is: 210
在上面的示例中,当我们输入正数20时,while循环不断迭代i = 0到20之间的计数器值。在每次迭代时,i的值都会添加到变量 sum中,和i递增1、当 while条件变为false时,它退出循环并打印前20个自然数之和。

使用do-while循环

让我们考虑以下示例,使用执行while循环。
SumOfNaturalNumber3.c
#include <stdio.h>
#include <conio.h>
void main()
{
    int num, i, sum = 0; // initialize and declare the local variables
    printf("Enter a positive number: ");
    scanf("%d", &num); // take a value up to which find the sum of natural number
    i = 0;
    do
    {
        sum = sum + i; // store the sum of natural number
        i++; // increment by 1  
    } while (i <= num); // define the while loop and i should be less than num
    
    // print the sum of natural number
    printf(" \n Sum of first %d natural number is : %d", num, sum);
    getch();
}
输出:
Enter a positive number: 30
Sum of the first 30 natural number is: 465
在上面的示例中,当我们输入正数30时,do循环在i = 0到30之间连续迭代计数器值。在每次迭代中,i的值都会添加到变量 sum中, ,并且i递增1、当 while条件变为false时,它将从while循环中退出并显示前30个自然数的和。

使用数学公式

我们编写一个程序来使用数学公式打印n个自然数的和。
SumOfNaturalNumber4.c
#include<stdio.h>
int main()
{
    int n = 40; // declare & initialize local variable n.
    int sum = (n * (n + 1) ) / 2; /* define the mathematical formula to calculate the sum of given number. */
    printf("Sum of %d natural number is = %d", n, sum); // print the sum of natural number
    return 0;
}
输出:
Sum of 40 natural number is = 840

使用函数

让我们考虑以下示例,使用 C语言中的功能。
SumOfNaturalNumber5.c
#include <stdio.h>
#include <conio.h>
void main()
{
    int num, total; // local variable
    printf("Enter a natural number: ");
    scanf("%d", &num); // take a natural number from the user
    total = natural_no(num);  // call the function
    printf(" Sum of the %d natural number is: %d", num, total);
}
int natural_no(num)
{
    int i, sum = 0;
    // use for loop until the condition becomes false
    for (i = 0; i <= num; i++)
    {
         // adding the counter variable i to the sum value
        sum = sum + i;
    }
    return sum;
}
输出:
Enter a natural number: 100
Sum of the 100 natural number is: 5050

在给定范围之间的n个自然数之和

计算从任何起始数字到指定的最后一个数字的n个自然数的总和。
SumOfNaturalNumber6.c
#include <stdio.h>
#include <conio.h>
void main()
{
    int num, i, sum = 0; // define the local variables
    printf("Enter the first number: ");
    scanf("%d", &i); // accept the starting number
    
    printf(" Up to the last natural number: ");
    scanf("%d", &num); // accept the last number
    
    // As long as the loop condition is true, it continuously iterates the statement while(i <= num)
    {
        // adding the counter variable i to the sum variable
        sum = sum + i;
        i++; // increment by 1
    }
    printf("Sum of natural number is = %d", sum);
    getch();
}
输出:
Enter the first number: 1
Up to the last number natural number: 25
Sum of natural number is = 325

使用递归

让我们考虑以下示例,使用递归。
SumOfNaturalNumber7.c
#include <stdio.h>
#include <conio.h>
int sum_natural_no(int num); // declare function outside the main function
int
 main()
{
    int num, sum = 0; // declare local variable
    printf("Enter any positive number to calculate the sum of natural no. ");
    scanf("%d", &num); // take an input from the user
    
    sum = sum_natural_no(num); // call the function
    
    printf("Sum of the first %d natural number is: %d", num, sum); // print the sum of natural number
    return 0;
}
int sum_natural_no(int num)
{
    if( num == 0) // define if condition
    {
        return num;    
    }
    else
    {   // return the else condition
        return( num + sum_natural_no( num-1));
    }
}
输出:
Enter any positive number to calculate the sum of natural no. 50
Sum of the first 50 natural number is: 1275

使用数组

SumOfNaturalNumber8.c
#include <stdio.h>
int main()
{
    // declare & initialize local variable
   int num, sum = 0, i, array[50];
    printf(" Enter a positive number as we want to sum the natural number: ");
   scanf("%d", &num); // take a positive number 
   printf("\n Enter the number one by one: \n");
   for (i = 0; i < num; i++)
   {
    
      scanf("%d", &array[i]); // read value one by one 
      sum = sum + array[i]; // store number into the sum variable
   }
   printf("Sum of the given number is = %d\n", sum);
   return 0;
}
输出:
Enter a positive number as we want to sum the natural number: 5
 Enter the number one by one: 
2
4
5
6
7
Sum of the given number is = 24

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4