0% found this document useful (0 votes)
2 views6 pages

Some Programs From Loop Control Structures

Uploaded by

Roshan Shaikh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Some Programs From Loop Control Structures

Uploaded by

Roshan Shaikh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Some programs from Loop Control Structures

[Link] a C program to find the sum of all odd integers between 1 to n.


#include<stdio.h>
void main()
{
int s,i,n;
printf("\n Enter value to n:");
scanf("%d",&n);
s=0;
i=1;
while(i<=n)
{
s=s+i;
i=i+2;
}
printf("\n Sum of odd integers=%d",s);
}
Output:
Enter value to n:10

Sum of odd integers=25


[Link] a program to generate the first 50 positive integers that are divisible by 7.
#include<stdio.h>
void main()
{
int n,i;
printf("\n Integer divisible by 7:\n");
n=7;
for(i=1;i<=10;i++)
{
printf("%d\n",n);
n=n+7;
}
}
Output:
Integer divisible by 7:
7
14
21
28
35
42
49
56
63
70
[Link] a C program to find the sum of the digits of any given positive integer.
Example when n=123 then sum of digits 1+2+3=6
#include<stdio.h>
void main()
{
int n,q,r,s;
printf("\n Enter a positive integer:");
scanf("%d",&n);
q=n;
s=0;
while(q>0)
{
r=q%10;
s=s+r;
q=q/10;
}
printf("\n Sum of digits =%d ",s);

}
Output:

Enter a positive integer:456

Sum of digits =15

[Link] a C program to check whether the given number is an Armstrong number.


A number that is equal to the sum of cubes of individual [Link] example,
153=13+53+33
=1+125+27
=153
#include<stdio.h>
void main()
{
int n,q,r,s;
printf("\n Enter an integer number: ");
scanf("%d",&n);
q=n;
s=0;
while(q>0)
{
r=q%10;
s=s+r*r*r;
q=q/10;
}
if(n==s)
printf("\n %d is an armstrong number ",n);
else
printf("\n %d is not an armstrong number ",n);
}
Output:
Enter an integer number: 153

153 is an armstrong number


Enter an integer number: 165

165 is not an armstrong number


5. Write a C program to reverse a given integer(for example 28431 is reversed as 13482).
#include<stdio.h>
void main()
{
int n,q,r,s;
printf("\n Enter an integer number: ");
scanf("%d",&n);
q=n;
s=0;
while(q>0)
{
r=q%10;
s=s*10+r;
q=q/10;
}
printf("\n %d is reversed as %d ",n,s);
}
Output:
Enter an integer number: 12345

12345 is reversed as 54321


[Link] a program to check whether the given number is a prime number.

#include <stdio.h>

int main()
{
int num, i, flag = 0;

printf("Enter a number: ");


scanf("%d", &num);

if(num <= 1)
{
printf("Not a Prime Number");
}
else
{
for(i = 2; i <= num/2; i++)
{
if(num % i == 0)
{
flag = 1;
break;
}
}

if(flag == 0)
printf("Prime Number");
else
printf("Not a Prime Number");
}

return 0;
}
Output:
Enter a number: 19
Prime Number
7. Write a C program to generate the Fibonacci series
0 1 1 2 3 5 8……..upto n.
#include<stdio.h>
void main()
{
int n,n1,n2,newtern;
printf("\n Enter the range of the series :");
scanf("%d",&n);
n1=0;
n2=1;
printf("%d\n%d ",n1,n2);
newtern=n1+n2;
while(newtern<=n)
{
printf("\n%d",newtern);
n1=n2;
n2=newtern;
newtern=n1+n2;
}
}
Output:
Enter the range of the series :22
0
1
1
2
3
5
8
13
21

8. Write a C program to accept an integer number and print the digits using words(for
example,356 is printed as Three Five Six).
#include<stdio.h>
void main()
{
int n,q,r,s;
printf("\n Enter an integer number :");
scanf("%d",&n);
q=n;
s=0;
while(q>0)
{
r=q%10;
s=s*10+r;
q=q/10;
}
while(s>0)
{
r=s%10;
switch(r)
{
case 1:printf("ONE ");
break;
case 2:printf("TWO ");
break;
case 3:printf("THREE ");
break;
case 4:printf("FOUR ");
break;
case 5:printf("FIVE ");
break;
case 6:printf("SIX ");
break;
case 7:printf("SEVEN ");
break;
case 8:printf("EIGHT ");
break;
case 9:printf("NINE ");
break;
case 0:printf("ZERO ");
break;
}
s=s/10;
}
}
Output:
Enter an integer number :456
FOUR FIVE SIX

You might also like