To display sum of first n numbers using while loop #c

//The Program By Tittu Varghese


// To display the sum of first n numbers.


#include<stdio.h>

#include<math.h>

#include<conio.h>

void main()

{int i,n,s;

i = 0;

s = 0;

printf("Enter the value of n to find sum of n numbers\n");

scanf("%d",&n);

while (i <= n)

{s = s+i;

i = i+1;

}

printf("The sum of first %d numbers is %d\n",n,s);

getch();

}



//The Program By Tittu Varghese
// To display the sum of first n even numbers.


#include<stdio.h>

#include<math.h>

#include<conio.h>

void main()

{int i,n,s;

i = 2;

s = 0;

printf("Enter the value of n to find sum of n even numbers\n");

scanf("%d",&n);

while (i <= n+n)

{s = s+i;

i = i+2;

}

printf("The sum of first %d even numbers is %3d\n",n,s);

getch();

}


//The Program By Tittu Varghese
// To display the sum of first n odd numbers.




#include<stdio.h>

#include<math.h>

#include<conio.h>

void main()

{int i,n,s;

i = 1;

s = 0;

printf("Enter the value of n to find sum of n odd numbers\n");

scanf("%d",&n);

while (i <=(n+n)-1)

{s = s+i;

i = i+2;

}

printf("The sum of first %d odd numbers is %d\n",n,s);

getch();

}



*******compare these programs and then think how it is working.Then do it your own way.*******

Comments

  1. Casino de Vivo - The Italian Casino - JMHub
    Visit the 춘천 출장마사지 Italian Casino. Your 충청남도 출장안마 complete guide to what 하남 출장샵 makes the casino great. We 아산 출장안마 have created a collection of the hottest slot machines 군산 출장샵 around.

    ReplyDelete

Post a Comment

Popular posts from this blog

Algorithm to display "n" natural numbers #c

Algorithm to display the sum of n natural numbers #c

Algorithm for Sum of two numbers