//program to get/sum of all odd numbers lying between 1 to n natural numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int k=0,n,sum=0;
printf("enter value of 'n'\n");
scanf("%d",&n);
while(k<=n)
{
if(k%2!=0)
{ sum=sum+k;
}
k++;
}
printf("the sum=%d",sum);
getch();
}
-----------------------------
we can also write this program as shown below.
-------------------------------------
//program to get/sum of all odd numbers lying between 1 to n natural numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int k=1,n,sum=0;
printf("enter value of 'n'\n");
scanf("%d",&n);
while(k<=n)
{
sum=sum+k;
k=k+2;
}
printf("the sum=%d",sum);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int k=0,n,sum=0;
printf("enter value of 'n'\n");
scanf("%d",&n);
while(k<=n)
{
if(k%2!=0)
{ sum=sum+k;
}
k++;
}
printf("the sum=%d",sum);
getch();
}
-----------------------------
we can also write this program as shown below.
-------------------------------------
//program to get/sum of all odd numbers lying between 1 to n natural numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int k=1,n,sum=0;
printf("enter value of 'n'\n");
scanf("%d",&n);
while(k<=n)
{
sum=sum+k;
k=k+2;
}
printf("the sum=%d",sum);
getch();
}
No comments:
Post a Comment