0% found this document useful (0 votes)
6 views2 pages

Lab5_loop

The document provides examples of various programming loops in C, including while, do while, for, and nested for loops. It also discusses break and continue statements, along with problems for practice such as finding odd numbers, calculating factorials, summing a series, and printing patterns using nested loops. Sample outputs for the problems are included to illustrate expected results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Lab5_loop

The document provides examples of various programming loops in C, including while, do while, for, and nested for loops. It also discusses break and continue statements, along with problems for practice such as finding odd numbers, calculating factorials, summing a series, and printing patterns using nested loops. Sample outputs for the problems are included to illustrate expected results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Computing Programming Lab

while and do while loop:

#include<stdio.h> #include<stdio.h> #include<stdio.h>


int main() int main() int main()
{ { {
int i,times; int i, times; int i,times;
scanf("%d",&times); scanf("%d",&times); scanf("%d",&times);
i = 0; i = times; i = 0;
while (i <= times) while (i >=0) do
{ { {
i++; i--; i++;
printf("%d\n",i); printf("%d\n",i); printf("%d\n",i);
} } }
return 0; return 0; while (i <= times);
} } return 0;
}

for loop:

#include<stdio.h> #include<stdio.h>
int main() int main()
{ {
int i,times; int i,times;
scanf("%d",&times); scanf("%d",&times);
for( i=0;i<times;i++) for(i=times;i>0;i--)
{ {
printf("%d\n",i); printf("%d\n",i);
} }
return 0; return 0;
} }

Nested for loop:

#include<stdio.h> #include<stdio.h>
int main() int main()
{ {
int i; int i;
for (i=1; i<=9; i++) for (i=1; i<=3; i++)
{ {
printf("\n"); printf("\n");
for (int j=1; j<=i; j++) for (int j=1; j<=10; j++)
{ {
printf("%d", j); printf("%d X %d = %d\n", i, j, i*j);
} }
} }
printf("\n"); printf("\n");
return 0; return 0;
} }
break and continue statements:

#include<stdio.h> #include<stdio.h>
int main() int main()
{ {
int i=0; int num=10;
while (i < 10) for(int i=0;i<num;i++)
{ {
i++; if(i==4)
printf("%d\n",i); continue;
if (i == 5) printf("%d\n",i);
break; }
return 0;
} }
return 0;
}

Problems:

1. Write a program to show all the odd numbers between 0 and 100 inclusive.
2. Write a program that takes an integer as input and shows its factorial.

Sample Output 1: Sample Output 2:

Enter a number: 5 Enter a number: 8


Factorial of 5 is 120 Factorial of 8 is 40320

3. Find the sum of the following series


1+3+5+7+9+......................+(2*N-1) (Take N as input)
4. Print the following patterns. The number of lines in the pattern is user-input. Hint: Use Nested Loop.
1)
*
**
***
****
*****
2)
*****
****
***
**
*

You might also like