Thursday, April 15, 2010

Programs on pattern using nested loop

program on displaying a grid of S - pattern printing


#include < stdio.h >
void main()
{
int i,j,flag;
clrscr();
for(i=0;i < 15;i++)
{
flag=0;
for(j=0;j < 18;j++)
{
if(i<3)
{
printf("%c",'*');
flag=1;
}
if(((i > 2)&&( i < 6))&&(j < 4))
{
printf("%c",'*');
flag=1;
}
if((i < 9)&&(i > 5))
{
printf("%c",'*');
flag=1;
}
if((i < 12)&&(i > 8)&&(j > 13))
{
printf("%c",'*');
flag=1;
}
if(i > 11)
{
printf("%c",'*');
flag=1;
}
if(flag==0)
{
printf("%c",' ');
}
}
printf("\n");
}
getch();
}

Pattern of alphabets

A B C D E E D C B A
A B C D D C B A
A B C C B A
A B B A
A A

#include < stdio.h >
void main()
{
int a,i,j,m,k,x=1;
clrscr();
for(i=0;i < 4;i++)
{
a=65;
for(j=i;j < 4;j++)
{
printf("%2c",a);
a++;
}
a=a-1;
if(i!=0)
{
for(k=0;k < x+i;k++)
{
printf("%2c",' ');
}
x=x+1;
}
for(m=i;m < 4;m++)
{
printf("%2c",a);
a=a-1;
}
printf("\n");
}
getch();
}


Display the product, quotient and remainder of two integers without using operator of product and division.

#include < stdio.h >
#include < conio.h >
void main()
{
int a,b,x,y=0,i;
clrscr();
printf("Enter two values:-");
scanf("%d%d",&a,&b);
x=a;
for(i=1;i < b;i++)
{
a=a+x;
}
printf("The product is :-%d",a);
a=x;
while(a>=b)
{
a=a-b;
}
printf("\nThe Remainder is :-%d",a);
a=x;
while(a > =b)
{
a=a-b;
y++;
}
printf("\nThe quotient is :-%d",y);
getch();
}

What will happen if we execute the following program?

#include < stdio.h >
void main ()
{
char ch='n';
clrscr ();
for (;;)
{
printf ("%c ", ch);
}
getch ();
}

Here is a program that will take a number from the user and then the reverse of the number will be displayed and also the sum of the digits.

#include < stdio.h >
void main ()
{
int number, total = 0, reverse = 0, digit, orig;
clrscr ();
printf ("Program for summation of digits and \n");
printf ("Reverse the number\n\n");
printf ("Enter any number: ");
scanf ("%d", &orig);
number = orig;
do
{
digit = orig % 10;
total += digit;
reverse = reverse * 10 + digit;
orig /= 10;
}while (orig != 0);
printf ("\nSummation of number %d = %d\n", number, total);
printf ("Reverse of the number %d = %d\n", number, reverse);
getch ();
}

What will be the output ?
© int I,j,x=0;
for(I=0;I<5;++I)
for (j=0; j
x+=(I+j-1);
printf(“%d”,x);
break;
}
printf(“\nx=%d”,x);
output 0
and x=0





This page on nested  loop using C Language. Any Question ? Put Your comments

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner