11. Difference between While-do and do-while loop?
WHILE-DO DO-WHILE
Condition is checked first then Statement(s) is executed atleast once,
statement(s) is executed. thereafter condition is checked.
It might occur statement(s) is executed At least once the statement(s) is
zero times, If condition is false. executed.
No semicolon at the end of while. Semicolon at the end of while.
while(condition) while(condition);
If there is a single statement, brackets
are not required. Brackets are always required.
Variable in condition is initialized before variable may be initialized before or
the execution of loop. within the loop.
while loop is entry controlled loop. do-while loop is exit controlled loop.
while(condition) do { statement(s); }
{ statement(s); } while(condition);
#include <stdio.h> #include <stdio.h>
int main() int main()
{ {
int i = 5; int i = 5;
do {
while (i < 10) { printf("GFG\n");
printf("GFG\n"); i++;
i++; } while (i < 10);
} return 0;
return 0; }
}
12. Write a C program and show the following pyramid output.
1
123
12345
1234567
123456789
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,k,l=1;
for(i=1; i<=5; i++)
{
for(j=4; j>=i; j--)
{
printf(" ");
}
for(k=1; k<=l; k++)
{
printf("%d",k);
}
l = l+2;
printf("\n");
}
getch();
}
[Remember that, in case of star pyramid, you have to change printf(“%d”,k); to printf(“%d”);]
13. Write a C program and show the following Pattern.
I
IN
IND
INDI
INDIA
#include<stdio.h>
#include<conio.h>
void main()
{
char s[]="INDIA";
int i,j;
clrscr();
for(i=0;s[i];i++)
{
for(j=0;j<=i;j++)
printf("%c",s[j]);
printf("\n");
}
getch();
}
[Remember that, in case of other string such as IGNOU, you have to write char s[]=”IGNOU”
14. Write C program to show the followng diamond pattern.
A
AB
ABC
ABCD
ABCDE
ABCDEF
ABCDE
ABCD
ABC
AB
A
#include<stdio.h>
#include<conio.h>
void main()
{
int r,c,k,m,i;
printf("Enter the number of lines : ");
scanf("%d",&m);
for(r=1;r<=m-1;r++)
{
i=0;
for(c=1;c<=m-r;c++)
{
printf(" ");
}
for(k=1;k<=r;k++)
{
printf("%c ",65+i);
i++;
}
printf("\n");
}
for(r=0;r<=m-1;r++)
{
i=0;
for(k=1;k<=r;k++)
{
printf(" ");
}
for(c=1;c<=m-r;c++)
{
printf("%c ",65+i);
i++;
}
printf("\n");
}
getch();
15. Write a C progtam to show the following trinagle using star.
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
#include <stdio.h>
#include<conio.h>
void main()
int i, space, rows, k=0;
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=1; i<=rows; ++i, k=0)
{
for(space=1; space<=rows-i; ++space)
printf(" ");
while(k != 2*i-1)
printf("* ");
++k;
printf("\n");
getch();