Showing posts with label C Patterns. Show all posts
Showing posts with label C Patterns. Show all posts

Friday, September 19, 2014

Pattern Printing Using C Language


In this program, the pattern as shown below will be displayed.
1 2
3
4 5
6
7 8
9

10 11

The upper range of the pattern ( as per the above pattern it is 11) will be given by user.

#include< stdio.h>
void main()
{
 int i=1,j,n,x=1;
 clrscr();
 printf("\nEnter the Last Number of the series:");
 scanf("%d",&n);

         for(;;)
         {
           
            for(j=0;j<=i;j++)
            {
   printf(" %d",x);
   x++;
}
   if(i==1)
   i=0;
   else
   i=1;
   if(x >n)
   break;
   printf("\n");
}
getch();
}

Saturday, August 4, 2012

Few Critical Pattern Printing Programs Using C Language


Today few pattern programs are posted. The programs are solved using C language 
First pattern is:

7654321
*54321
**321
***1
**321
*54321
7654321



#include <stdio.h>
int main()
{
int i,j,k,x,y=0,y1=0;

for ( i=0;i< 7;i++)
{
x=7;
for(k=0;k< y;k++)
{
 printf("*");
 x=x-2;
 }
for(j=0;j< 7-y1;j++)
  printf("%d",x--);
 printf("\n");

   if(i >= 7/2)
  {
   y--;
  y1=y1-2;
  
  }
  else
  {
 y++;
  y1=y1+2;
  }
  }
  return 0;
}


Pattern number 2 is as follows:

1
21
123
4321
12345



#include <stdio.h>
void main()
{
int i,j,x=0;
clrscr();
for ( i=0;i< 5;i++)
{
if(i%2==0)
x++;
 for(j=0;j<=i;j++)
 {
  printf("%d",x);
  if(i%2==0)
  x++;
  else
  x--;
  }
   printf("\n");
  }
  getch();
}

The third pattern is as follows:

1
01
010
1010
10101
010101

#include <stdio.h>
void main()
{
int i,j,x=1;
clrscr();
for ( i=0;i< 6;i++)
{
 for(j=0;j<=i;j++)
 {
  printf("%d",x);
  if(x>0)
  x--;
  else
  x++;
  }
    printf("\n");
  }
  getch();
}


Sunday, July 22, 2012

Pattern Program Using C Language

This is a pattern displaying program. The pattern is


1 2 3 4 5 6

1 3 5 7 9 11

1 4 7 10 13 16

1 5 9 13 17 21



#include<stdio.h>
void main()
{
int i,j,x;
clrscr();
for(i=1;i<=4;i++)
{
x=1;
for(j=1;j<=6;j++)
{
printf(" %d",x);
x=x+i;
}
printf("\n");
}
getch();
}


Here is another pattern programwhich depends on user interaction:

if user input 1 -> {}
if user input 2 ->
{}{}
{}{}

if user input 3 ->
{}{}{}
{}{}{}
{}{}{}




#include<stdio.h>
void main()
{
int i,j,n;
printf("Enter '1', '2' or '3':");
scanf("%d",&n);
for(i=0;i< n;i++)
{
for(j=0;j< n;j++)
{
printf("{}");
}
printf("\n");
}
getch ();
}

Monday, July 16, 2012

C Program to print a pattern

In this program we will print the pattern as shown below using C programming language.

The pattern is as follows:


1
2 1
1 2 3
4 3 2 1
1 2 3 4 5

Codes of the C program

#include
void main()
{
int i,j,x=1;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",x);
if(i%2==0)
x--;
else
x++;
}
if(i%2==0)
x=1;
else
x=j;
printf("\n");
}
getch();
}

Another pattern using C program

1
3
6
10
15

#include
void main()
{
int i,x=1,y=2;;
for(i=0;i<5;i++)
{
printf("%d\n",x);
x=x+y;
y++;
}
getch();
}

Wednesday, January 18, 2012

C Program on Pattern Printing


1234554321
1234---4321
123------321
12---------21
1------------1
12---------21
123------321
1234---4321
1234554321

C Programming codes to print the above pattern

#include<stdio.h>
void main()
{
int i,j,x,y=0,k,m,n;
m=5;
for (i=0;i<9;i++)
{
x=1;
for(j=0;j<m;j++)
printf("%d",x++);
for(k=0;k<y;k++)
printf(" ");
x--;
for(j=0;j<m;j++)
printf("%d",x--);
printf("\n");
if(i<9/2)
{
m--;
y=y+3;
}
else
{
m++;
y=y-3;
}
}
getch();
}

Sunday, January 8, 2012

Programs on Patterns Using C Language


Two programs on pattern using C Language are given below


1
333
55555
7777777
999999999
7777777
55555
333
1


#include<stdio.h>
void main()
{
int m,n,b=1;
for(m=0; m<5; m++)
{
for(n=0; n<m*2+1; n++)
{
 printf("%d",b);
}
b=b+2;
printf("\n");
}
b=b-4;
for(m=0; m<4; m++)
{
for(n=0; n< 7 - m*2; n++)
{
printf("%d",b);
}
b=b-2;
printf("\n");
}
getch();
}


Display the pattern using C Language program.


1
2_2
3_3_3
4_4_4_4
5_5_5_5_5
4_4_4_4
3_3_3
2_2
1




#include<stdio.h>
void main()
{
int m,n,b=1;
for(m=0; m< 5; m++)
{
for(n=0; n< m*2+1; n++)
{
if(n%2==0)
 printf("%d",b);
 else
 printf(" ");
}
b=b+1;
printf("\n");
}
b=b-2;
for(m=0; m< 4; m++)
{
for(n=0; n< 7 - m*2; n++)
{
if(n%2==0)
 printf("%d",b);
 else
 printf(" ");
}
b=b-1;
printf("\n");
}
getch();
}

Sunday, December 25, 2011

C Language program on pattern


C Program on printing the following pattern

1AAAA
22BBB
333CC
4444D
55555

#include<stdio.h>
void main()
{
int i,j,k;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
}
for(k=i;k<5;k++)
{
printf("%c",(i+64));
}
printf("\n");
}
getch();
}

Saturday, May 22, 2010

C Programs on Pattern using Nested Loop

*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
#include < stdio.h >
void main ()
{
int i,j,x;
clrscr();
for(i=0;i < 9;i++)
{
if(i > 4)
x=x-1;
else
x=i;
for(j=0;j < =x;j++)
{
printf("* ");
}
printf("\n");
}
getch();
}


Write a program to display the pattern:-
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

#include < stdio.h >
#include < conio.h >
void main()
{
int val,row,q,r,x;
clrscr();
val=1;
q=0;
printf("Enter the number of rows:-");
scanf("%d",&row);
while(q < row)
{
for(x=0;x < =q;x++)
{
if(x==0)
val=1;
else
val=(val*(q-x+1))/x;
printf("%4d",val);
}
printf("\n");
q++;
}
getch();
}

1
0 1
1 0 1
0 1 0 1
n terms
#include < stdio.h >
void main()
{
int i,j,n;
clrscr();
printf("Enter the number of rows:-");
scanf("%d",&n);
for(i=0;i < n;i++)
{
for(j=0;j < i+1;j++)
{
if(((i%2==0)&&(j%2==0))||((i%2!=0)&&(j%2!=0)))
printf("%2d",1);
else
printf("%2d",0);
}
printf("\n");
}
getch();
}

Output will be like

0 0 0 0 0 0
0
0
0
0
0 0 0 0 0 0

#include < stdio.h >

void main()
{

int i,j,row,col;
clrscr();
printf("\nEnter the row and column numbers::");
scanf("%d%d",&row,&col);
if(row!=col)
{
printf("\nEnter row and column numbers same.");
getch();
return;
}
for(i=0;i < row;i++)
{
for(j=0;j < col;j++)
{
if((i==0)||(i==row-1)||(i+j==row-1))
printf("%2c",'0');
else
printf("%2c",' ');
}
printf("\n");
}
getch();
}

pattern of digits

1
1 1
1 2 1
3 3 1
1 4 6 4 1

#include < stdio.h >
#include < conio.h >
void main()
{
int val,row,q,r,x;
clrscr();
val=1;
q=0;
printf("Enter the number of rows:-");
scanf("%d",&row);
while(q < row)
{
for(x=0;x < =q;x++)
{
if(x==0)
val=1;
else
val=(val*(q-x+1))/x;
printf("%4d",val);
}
printf("\n");
q++;
}
getch();
}

1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4

#include < stdio.h >
#include < conio.h >
void main()
{
int p,m,q,n;
clrscr();
printf("Enter the number of rows:-");
scanf("%d",&n);
for(p=1;p < =n;p++)
{
m=p
for(q=0;q < p;q++)
{
printf("%4d",m++);
}
m=m-2;
for(q=1;q < p;q++)
{
printf("%4d",m--);
}
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();
}

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

Saturday, March 27, 2010

C programs on pattern printing using nested loop

We know that a loop body contains statement or statements. Again a control statement and the body of a loop form a statement. So within a loop body we can put a loop. Loop inside a loop is called Nested loop. For every iteration of the outer loop the inner loop completes it's full course of iteration.
#include < stdio.h>
void main ()
{
int x, y, i, j;
char ch;
clrscr ();
printf ("Enter the character:\n");
scanf ("%c", &ch);
printf ("Enter the number of rows:\n");
scanf ("%d", &x);
printf ("Enter the number of columns:\n");
scanf ("%d", &y);
for (i = 0; i < x; i++) // outer loop 

{
 for (j = 0; j < y; j++) // inner loop
 {
 printf ("%c ", &ch); 

printf ("\n"); 
}
 /* this statement is purely an outer loop statement.*/ 
}
 getch (); 
}


  The next program will display the following output: - 0 0 1 0 1 2 0 1 2 3 0 1 2 3 4 ................. 0 1 2 3 4 5 6 7 8  #include < stdio.h> 
void main () 
{
 int i, j; 
clrscr ();
 for (i=0; i < 9; i++)
 {
 for (j=0; j < i+1; j++) 
{  
printf ("%d ", j); 
 }  
printf ("\n");  

getch ();
 } 


  #include " stdio.h" 
void main () 
{
 int i, j; char ch='*'; 
clrscr ();
 for (i=0;i < 5;i++)  

 for (j=i; j < 5;j++) 
{
 printf ("%c ", ch);
 }
  printf ("\n");
  }
 getch (); 
 }  






1 2 3 4 5 6 7 8 9 10 ............. ............... 79................91 */ 


  #include "stdio.h" 
void main()
 {
 int i,j,x=1; 
clrscr();
 for(i=0;i <13;i++) 

 for(j=0;j < i+1;j++) 

 printf("%4d",x); x++; 
}  printf("\n");  

 getch(); 



  In our next nested loop program we will display the pattern like: - a a a a a a a a a a ............ .............. a a a a a a a a a  


#include "stdio.h"
 void main ()
 { 
int i, j;
 clrscr (); 
for (i=0;i < 10;i++) 

 for (j=0;j <10;j++) 

if (j==9-i) 
printf ("%c ",'a'); 
else printf ("%c ",' '); 
}
 printf ("\n");
 }
 getch ();
 } 




To construct a pyramid

#include "stdio.h"
void main()
{
int i,j,k,row,col;
clrscr();
printf("Enter the row and column\t");
scanf("%d%d",&row,&col);
for(i=0;i < row;i++)
{
 for(k=i;k< row-1;k++)
printf("%2c",' ');
 for(j=0;j < i+1;j++)
 {
printf("%4c",'*');
}
printf("\n");
}
getch();
 }

 Try yourself 1.Write a program to display the following pattern: - 0 1 2 3 1 2 3 2 3 3 2.Write a program to display the following pattern: - 1 1 0 1 0 1 1 0 1 0 1 0 1 0 1 We fount that all our outputs are displayed at the left upper corner. How we can display output at other locations? The above program is modified so that the output will be displayed at a location, which are thirty spaces away from the left side.

#include "stdio.h"
void main ()
{
int i, j, s;
clrscr ();
for (i=0;i < 9;i++)
{
 for(s=0;s < 30;s++)
printf ("%c",' ');
for (j=0;j < i+1;j++)
{
printf ("%d ", j);
}
printf ("\n");
 }
getch ();
}


Using nested loop we can display the following output. 00 01 02 03 04 05 06 a 08 09 10 11 12 13 14 15 16 a 18 19 .......................................... 90 91 92 93 94 95 96 a 98 99 In every 8th column a character 'a' is printed. Also ‘0’ precedes the numbers, which are less than 10.

#include "stdio.h"
void main ()
{
int i, j, x=0;
clrscr ();
for (i=0;i < 10;i++)
{
for (j=1;j < =10;j++)
{
if (j%8==0)
printf ("%c ",'a');
else if(x<10)
printf ("%c%d ",'0',x);
else
printf ("%d ",x);
x++;
}
printf ("\n");
}
getch ();
}



Any Question ? Put Your comments

Subscribe via email

Enter your email address:

Delivered by FeedBurner