1.3 Programs in The Class Loop
1.3 Programs in The Class Loop
Program 1:
#include <stdio.h>
int main()
{
int i = 0;
while(i<5)
{
printf("Hello world\n");
}
return 0;
}
- How many times the above program will print “Hello world”?
Ans: Infinite
Program 2:
#include <stdio.h>
int main()
{
int i = 0;
while(i<5)
{
printf("i = %d\n",i);
i++;
}
return 0;
}
- How many times the above program prints value of i
Ans: 5
return 0;
}
Q.3 Even number (option 1): Print even numbers between 1 to 30.
#include <stdio.h>
int main()
{
int count=1;
while(count<=30)
{
if(count%2==0)
printf("\n %d",count);
count= count+1;
}
return 0;
}
Even number (option 2): Print even numbers between 1 to 30 (Alternate solution)
#include <stdio.h>
int main()
{
int count=2;
while(count<=30)
{
printf("\n %d",count);
count= count+2;
}
return 0;
}
Q.4 Table of 5
#include <stdio.h>
int main()
{
int count=5;
while(count<=50)
{
printf("\n %d", count);
count= count+5;
}
return 0;
}
return 0;
}
Prime no (option 2)
#include <stdio.h>
int main()
{
int num, count=2, divide=0;
printf("Enter any number");
scanf("%d",&num);
while(count<num) // count<=num-1
{
if(num%count==0)
{
divide++;
}
count++;
}
if(divide==0)
printf("\n Its a prime number");
else
printf("\n Its NOT a prime number");
return 0;
}
Prime no (option 3)
#include <stdio.h>
int main()
{
int num,count=2,prime=1;
printf("Enter any number");
scanf("%d",&num);
while(count<num) // count<=num-1
{
if(num%count==0)
{
prime=0;
break;
}
count++;
}
if(prime==1)
printf("\n Its a prime number");
else
printf("\n Its NOT a prime number");
return 0;
}
return 0;
}
Natural no reverse counting (option 2)
#include <stdio.h>
int main()
{
int i=10;
for(;i>=1;i--)
{
printf("\n %d",i);
}
return 0;
}
return 0;
}
Q.3 use of ; after for loop prints 11
#include <stdio.h>
int main()
{
int i;
for(i=1;i<=10;i++); // When i==11 the loop terminates
printf("\n %d",i);
return 0;
}
Q.4 Reverse of a number
Using while loop:
#include <stdio.h>
int main()
{
int n, rem,rev=0;
printf("\nEenter a number:");
scanf("%d",&n);
while(n>0) //while(n>0)
{
rem=n%10;
rev=rev*10+rem;
#include <stdio.h>
int main()
{
int n, rem,rev=0;
printf("\nEnter a number: ");
scanf("%d",&n);
for (;n>0;n/=10) //while(n>0)
{
rem=n%10;
rev=rev*10+rem;
}
printf("\n %d",rev);
return 0;
}
#include <stdio.h>
int main()
{
int n, rem;
printf("\n Enter any no");
scanf("%d", &n);
for(; n>0; n/=10)
{
rem=n%10;
printf("\n %d",rem);
}
return 0;
}
Natural nos b
#include <stdio.h>
int main()
{
int i=11;
do{
printf("\n %d",i);
i++;
}
while(i<=10);
return 0;
}
Nested Loop
Q.1 Enter marks in 5 subjects and calculate avg for three students
#include <stdio.h>
int main()
{
int i,j;float marks,total=0,avg=0;
for(j=1;j<=3;j++) // no of students =3
{
total=0; avg=0;
printf("\n Enter marks in 5 subjects");
for(i=1;i<=5;i++) // no of subjects =5
{
scanf("%f",&marks);
total=total+marks; // total+=marks;
}
avg=total/5;
printf("\n Avg is : %f",avg);
}
return 0;
}
b.
1
12
123
#include <stdio.h>
int main()
{
int i,j,n;
printf("Enter no of rows: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n");
for(j=1;j<=i;j++)
{
printf("%d",j);
}
}
return 0;
}
c.
1
22
333
#include <stdio.h>
int main()
{
int i,j,n;
printf("Enter no of rows: ");
scanf("%d",&n);
for(i=1; i<=n; i++)
{
printf("\n");
for(j=1;j<=i;j++)
{
printf("%d",i);
}
}
return 0;
}
________________
Pattern Printing
https://www.programiz.com/c-programming/examples/pyramid-pattern
https://www.javatpoint.com/star-program-in-c
https://www.educba.com/patterns-in-c-programming/
https://www.scaler.com/topics/pattern-program-in-c/
for loop
https://www.programiz.com/c-programming/c-for-loop
https://www.w3resource.com/c-programming-exercises/for-loop/index.php
https://www.javatpoint.com/for-loop-in-c
https://codeforwin.org/c-programming/for-do-while-loop-programming-exercises