26. WRITE A C PROGRAM TO FIND SUM OF 1 TO N ODD NUMBERS USING WHILE

/*C program to find Sum of 1 to n odd numbers using while*/

#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,sum=0,n;
clrscr();
printf("enter the number ");
scanf("%d",&n);
while(i<=n)
{
if(i%2!=0)
{
sum=sum+i;
}
i=i+1;
}
printf("sum of odd number=%d",sum);
getch();
}
output

enter the number10
sum of odd numbers = 25

/*program to Check the given number is armstrong number or not using c program*/
#include<stdio.h>
int main(){
int num,r,sum=0,temp;
printf("Enter a number: ");
scanf("%d",&num);
temp=num;
while(num!=0){
r=num%10;
num=num/10;
sum=sum+(r*r*r);
}
if(sum==temp)
printf("%d is an Armstrong number",temp);
else
printf("%d is not an Armstrong number",temp);
return 0;
}

output

Sample output:
Enter a number: 153
153 is an Armstrong number

Comments