Write a program using while loop to generate the first 10 natural numbers and their sum.
Answers
#include <stdio.h>
#include<conio.h>
void main()
{
int a, sum = 0;
printf("The first 10 natural numbers are as follows :\n");
for (a= 1; a <= 10; a++)
{
sum = sum + a;
printf("%d ",a);
}
printf("\nThe Sum of the 10 natural numbers is : %d\n", sum);
}
"No programming language has been specified in the question. So, I am going to answer the question with code in C programming language. The program in C programming language which uses a while loop to generate the first 10 natural numbers and their sum is as follows:
#include <stdio.h>
#include<conio.h>
void main()
{
int a, sum = 0;
printf("The first 10 natural numbers are as follows :\n");
for (a= 1; a <= 10; a++)
{
sum = sum + a;
printf("%d ",a);
}
printf("\nThe Sum of the 10 natural numbers is : %d\n", sum);
}"