Problem: -
- Write a program to print 1 to 100 Natural Numbers.
- Write a program to print sum of 1 to 100 Natural Numbers.
- Write a program to print sum of 1 to 100 Even Numbers and print Even Numbers between them.
- Write a program to print sum of 1 to 100 Odd Numbers and print Odd Numbers between them.
Solution: -
1. Write a program to print 1 to 100 Natural Numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int i = 1;
while(i<=100)
{
printf("%d\t", i);
i++;
}
getch();
}
Output: -
2. Write a program to print sum of 1 to 100 Natural Numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int i = 1, sum = 0;
while(i<=100)
{
sum = sum + i;
i++;
}
printf("Sum of Natural Number is: %d", sum);
getch();
}
Output: -
3. Write a program to print sum of 1 to 100 Even Numbers and print Even Numbers between them.
#include<stdio.h>
#include<conio.h>
void main()
{
int i = 1, sum = 0;
printf("Even number is:\n");
while(i<=100)
{
if(i%2==0)
{
sum = sum + i;
printf("%d\t", i);
}
i++;
}
printf("\nSum of Even Number is: %d", sum);
getch();
}
Output: -
4. Write a program to print sum of 1 to 100 Odd Numbers and print Odd Numbers between them.
#include<stdio.h>
#include<conio.h>
void main()
{
int i = 1, sum = 0;
printf("Odd number is:\n");
while(i<=100)
{
if(i%2!=0)
{
sum = sum + i;
printf("%d\t", i);
}
i++;
}
printf("\nSum of Odd Number is: %d", sum);
getch();
}
Output: -
Tags
C Language