Program for the sum of 0 to N

Program for the sum of 0 - N using the formula n (n+1) / 2.


Source Code


#include <stdio.h>

void main()
{
    int N, sum = 0;
    printf("Program for sum of all numbers from 0 - N\n");

    printf("Enter N: ");
    scanf("%d", &N);
   
    sum = N * (N+1) / 2;
   
    printf("The sum of all numbers between 0 and %d is: %d", N, sum);
}

Output



Program for sum of all numbers from 0 - N
Enter N: 100
The sum of all numbers between 0 and 100 is: 5050

5 মন্তব্যসমূহ

  1. #include"stdio.h"
    main(){
    int i,n,sum=0;
    printf("Enter value for n : \n");
    scanf("%d",&n);
    for (i=1;i<=n;i++){
    sum+=i;
    }
    printf("sum is %d",sum);
    }

    উত্তরমুছুন
  2. #include
    main()
    {
    int n,i,sum=0;
    printf("enter the value of n");
    scanf("%d",&n);
    sum=n*(n+2)/2;
    printf("sum is %d",sum);
    }

    উত্তরমুছুন
  3. for(i=0;i<2;i++){

    for(j=0;j<2;j++){

    printf("%d ",a[i][j]+b[i][j]);
    if(j==1){
    printf("\n");
    }

    উত্তরমুছুন
  4. #include

    int main()
    {
    int N, on = 0;
    printf("Program for On of all numbers from \n");

    printf("Enter N: ");
    scanf("%d", &N);

    on = N * (N+2) / 4;

    printf("on %d", on);
    }

    উত্তরমুছুন
  5. #include"stdio.h"
    main(){
    int i,n,sum=0;
    printf("Enter value for n : \n");
    scanf("%d",&n);
    for (i=1;i<=n;i++){
    sum+=i;
    }
    printf("sum is %d",sum);
    }

    উত্তরমুছুন
নবীনতর পূর্বতন