0% found this document useful (0 votes)
14 views

1.3 Programs in The Class Loop

Uploaded by

aadhila9524
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

1.3 Programs in The Class Loop

Uploaded by

aadhila9524
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Simple loop demos:

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

Program 3: For loop


#include <stdio.h>
int main()
{
int i;
for(i = 0; i<5; i++)
{
printf("i = %d\n",i);
}
return 0;
}
Program 4: do-while loop
#include <stdio.h>
int main()
{
int i = 0;
do{
printf("i = %d\n",i);
i++;
}
while(i<5);

return 0;
}

Program 5: Nested loops


#include <stdio.h>
int main()
{
int i = 0,j=0, count1=3,count2=5;
for(i = 0; i<count1; i++)
{
printf("I am inside outer loop %d\n",i);
for(j = 0; j<count2; j++)
{
printf("Inside inner loop %d\n",j);
}
}
return 0;
}

Loop (Iteration/ Repetition) a. While


Q.1 print your name 5 times
#include <stdio.h>
int main()
{
int count=1;
while(count<=5) // May also use: while(i<6)
{
printf("\n Your Name");
count= count+1; // May also use: count++; or count+=1;
}
return 0;
}

Q.2 print first 10 natural number


#include <stdio.h>
int main()
{
int count=1;
while(count<=10) // while(i<6)
{
printf("\n %d",count);
count= count+1; // count++; or count+=1;
}
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;
}

Q.5 Table of any number (option 1)


#include <stdio.h>
int main()
{
int table, count;
printf("\n Enter whose table you want to print?");
scanf("%d",&table);
count=table;
while(count<=table*10) // while(i<6)
{
printf("\n %d",count);
count= count+table;
}
return 0;
}

Table of any number (option 2): Display in tabular form


#include <stdio.h>
int main()
{
int table, count;
printf("\n Enter whose table ypu want to print?");
scanf("%d",&table);
count=1;
while(count<=10)
{
printf("\n %d * %d= %d",table,count, table*count);
count++; // count=count+1; or count+=1;
}
return 0;
}

Q.6 Prime no (option 1) : Check if a given number is prime or not


#include <stdio.h>
int main()
{
int num,count=2,flag=1;
printf("Enter any number");
scanf("%d",&num);
while(count<num) // count<=num-1
{
if(num%count==0)
{
printf("\n It is NOT a prime number");
break;
}
count++;
}
if(count==num) printf("\n Its a prime number");

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;
}

Q.7 digits of a no using while


#include <stdio.h>
int main()
{
int n=1234, rem;
while(n>0)
{
rem=n%10;
n=n/10;
printf("\n %d",rem);
}
return 0;
}

Loop (Iteration/ Repetition) b. for


Q.1 Natural no
#include <stdio.h>
int main()
{
int i;
for(i=1;i<=10;i++)
{
printf("\n %d",i);
}
return 0;
}

Q.2 Natural no reverse counting (option 1) (No user input)


#include <stdio.h>
int main()
{
int i;
for(i=10;i>=1;i--)
{
printf("\n %d",i);
}

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;
}

Natural no reverse (option 3)


#include <stdio.h>
int main()
{
int i=10;
for(;i>=1;)
{
printf("\n %d",i);
i--;
}

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;

n = n/10; // May use n/=10;


}
printf("\n %d",rev);
return 0;
}

Using for loop:

#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;
}

Q.5 Prime or not


#include <stdio.h>
int main()
{
int n,i,flag=1;
printf("\n enter a number");
scanf("%d",&n);
for (i=2;i<n;i++)//while(n>0)
{
if(n%i==0)
{
flag=0;
break; // out of for loop
}
}
if(flag==1) printf("\n Prime no");
else printf("\n Not prime");
return 0;
}

Q.6 Count no of digits in a number


#include <stdio.h>
int main()
{
int n, rem,count=0;
printf("\n Enter any no");
scanf("%d",&n);
for(;n>0;)
{
rem=n%10;
n=n/10;
count++;
}
printf("\n %d",count);
return 0;
}

Q.7 Print digits of a no a number (option1)


#include <stdio.h>
int main()
{
int n, rem;
printf("\n Enter any no");
scanf("%d",&n);
for(;n>0;)
{
rem=n%10;
n=n/10;
printf("\n %d",rem);
}
return 0;
}
7. Digits of number (option2)

#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;
}

Q.8 Sum of digits of a number


#include <stdio.h>
int main()
{
int n, rem=0,sum=0;
printf("\n Enter any no");
scanf("%d",&n);
for(;n>0;n/=10)
{
rem=n%10;
sum=sum+rem;
}
printf("\n Sum of digits = %d",sum);
return 0;
}
Q.9 power eg. 2^7
#include <stdio.h>
int main()
{
int n, power, res=1, i;
printf("\n Enter base and power");
scanf("%d %d", &n, &power);
for(i=1; i<=power; i++)
{
res=res*n;
}
printf("\n The res = %d", res);
return 0;
}
Q.10 Factorial
#include <stdio.h>
int main()
{
int n,f=1,i;
printf("Enter a no");
scanf("%d",&n);
for(i=1; i<=n; i++)
{
f=f*i;
}
printf("Factorial of %d is %d ",n,f);
return 0;
}

Loop (Iteration/ Repeatation) c. do while


Q.1 Natural nos a
#include <stdio.h>
int main()
{
int i=1;
do{
printf("\n %d",i);
i++;
}
while(i<=10);
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;
}

Q.2 Pattern Printing


a.
*
**
***
****
****
#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("*");
}
}
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

All over programming in C


https://www.programiz.com/c-programming/examples

You might also like