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",×); scanf("%d",×); scanf("%d",×);
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",×); scanf("%d",×);
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)
*****
****
***
**
*