5 Computer -- Programming II

Write a C program to calculate sum of n natural number.

Write a C program to calculate sum of n natural number.

OUPUT:

enter the number 4

The sum of n natural numbers is 10 


PROGRAMS:

#include<stdio.h>

int sum(int);

int main()

{

    int n;

    printf("enter the number ");

    scanf("%d",&n);

    int c=sum(n);

    printf("The sum of n natural numbers is %d ",c);

}

int sum(int n)

{

    int d=n*(n+1)/2;

    return d;

}

More questions on Programming II

Differentiate between structure and union.

Structures in Cis a user-defined data type available in C that allows to combining of data items of different kinds. Structures are used to represent a record.

Defining a structure:To define astructure, you must use thestructstatement. The struct statement defines a newdata type, with more than or equal to one member. The format of the struct statement is as follows:

struct [structure name] { member definition; member definition; ... member definition; }; (OR) struct [structure name] { member...
Close Open App