Loops in C
// Hello world in while loop
#include
#include
void main()
{
int i;
i=1;
while(i<=10)
{
printf(“%dHello While Loop “,i);
i++;
}
}
Output:
1Hello While Loop
2Hello While Loop
3Hello While Loop
4Hello While Loop
5Hello While Loop
6Hello While Loop
7Hello While Loop
8Hello While Loop
9Hello While Loop
10Hello While Loop
//Simple Do-While Loop
#include
#include
int main()
{
int n = 0;
do
{
printf( ” Enter No.( 0 for exit) :: ” );
scanf( “%d”, &n );
}while( n != 0 );
return 0;
}
Output:
Enter No.( 0 for exit) :: 2
Enter No.( 0 for exit) :: 6
Enter No.( 0 for exit) :: 9
Enter No.( 0 for exit) :: 0
/* Print pattern
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
*/
#include
#include
void main()
{
int i,j;
clrscr();
i=5;
while(i>=1)
{
j=i;
while(j>=1)
{
printf(“%d”,i);
j–;
}
i–;
}
getch();
}
Output:
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
/* Print Pattern
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
*/
#include
#include
void main()
{
int i,j;
clrscr();
i=1;
while(i<=5)
{
j=1;
while(j<=i)
{
printf(“%d”,i);
j++;
}
i++;
}
}
Output:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
/* Find LCM and GCD of two number */
#include
int main()
{
int a,b,t1,t2,lcm,gcd;
printf(“Enter two number: “);
scanf(“%d%d”,&a,&b);
t1=a;
t2=b;
while(t1!=t2)
{
if(t1>t2)
{
t1=t1-t2;
}
else
{
t2=t2-t1;
}
}
gcd=t1;
lcm=(a*b)/gcd;
printf(“LCM = %d , GCD= %d”,lcm,gcd);
return 0;
}
Output:
Enter two number: 25 15
LCM = 75 , GCD= 5
C Programs. Get More Example from
https://play.google.com/store/apps/details?id=com.kapidhvaj.cprograms
// Find factorial of given number
#include
#include
void main()
{
long int a,m=1;
clrscr();
printf(“a::”);
scanf(“%ld”,&a);
while(a>0)
{
m=a*m;
a–;
}
printf(“fact:%ld”,m);
getch();
}
Output:
a::5
fact:120
/*
* C program to accept a decimal number and convert it to binary
* and count the number of 1’s in the binary number */
#include
void main()
{
long num, decimal_num, remainder, base = 1, binary = 0, no_of_1s = 0;
printf(“Enter a decimal integer”);
scanf(“%ld”, &num);
decimal_num = num;
while (num > 0)
{
remainder = num % 2;
/* To count no.of 1s */
if (remainder == 1)
{
no_of_1s++;
}
binary = binary + remainder * base;
num = num / 2;
base = base * 10;
}
printf(“Input number is = %d”, decimal_num);
printf(“Its binary equivalent is = %ld”, binary);
printf(“No.of 1’s in the binary number is = %d”, no_of_1s);
}
Output:
Enter a decimal integer 134
Input number is = 134
Its binary equivalent is = 10000110
No.of 1’s in the binary number is = 3
// Break statement
#include
int main()
{
int i;
for( i=1 ; i<=10 ; i++ )
{
if(i == 5)
{
break;
}
printf( “%d\t”, i );
}
return 0;
}
Output:
1 2 3 4
// continue statement
#include
int main()
{
int i;
clrscr();
for( i=1 ; i<=10 ; i++ )
{
if(i == 5)
{
continue;
}
printf( ” %d”, i );
}
return 0;
}
Output:
1 2 3 4 6 7 8 9 10
/*Check given number is prime number or not*/
#include
void main(){
int num,i,count=0;
printf(“Enter a number: “);
scanf(“%d”,&num);
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
if(count==0 && num!= 1)
printf(“%d is a prime number”,num);
else
printf(“%d is not a prime number”,num);
return 0;
}
Output:
Enter a number: 7
7 is a prime number