0% found this document useful (0 votes)
148 views

Pattern Program in C

The document contains several C programs that print different patterns using loops and conditional statements. The programs take user input for the number of rows/layers and print patterns like triangles, pyramids, Pascal's triangle, etc. ranging from 1 to the input number. Loops are used to iterate through the rows/layers while conditional logic determines the number and position of characters or symbols to print on each line based on the row count.

Uploaded by

ARUN KANTI MANNA
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
148 views

Pattern Program in C

The document contains several C programs that print different patterns using loops and conditional statements. The programs take user input for the number of rows/layers and print patterns like triangles, pyramids, Pascal's triangle, etc. ranging from 1 to the input number. Loops are used to iterate through the rows/layers while conditional logic determines the number and position of characters or symbols to print on each line based on the row count.

Uploaded by

ARUN KANTI MANNA
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 45

/*Write C programs to print the following Output */

/*
*
* *
* * *
* * * *
* * * * *
*/

#include<stdio.h>
#include<conio.h>
void main ()
{
int i,j,row;
clrscr();
printf("Enter the number of rows");
scanf("%d",&row);
for(i=1;i<=row;i++) // for-1(outer) used to control row
{
for(j=1;j<=i;j++) // for-2(inner) used
{
printf("*"); // to print ‘*’
}
printf ("\n");
}
}

/* OUTPUT:
Enter the number of rows 5

*
* *
* * *
* * * *
* * * * *
*/

//Write a program to ptint like this//


/*
*
**
***
****
*****
*/

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,row;
clrscr();
printf("Enter the number of rows");
scanf("%d",&row);
for(i=1;i<=row;i++) // for-1(outer) used to control row
{
for(j=1;j<=(row-i);j++)
printf(" "); // to print blank space
for(j =1;j<=i;j++) // for-2(inner) used
printf("*"); // to print ‘*’
printf ("\n");
}
}

/*
OUTPUT:
Enter the number of rows 5
*
**
***
****
*****
*/

/* Program 7.17: Write C programs to print the following Output.//

/*
*
* *
* * *
* * * *
*/

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,row;
clrscr();
printf("enter row:");
scanf("%d",&row);
for(i=1;i<=row;i++) // line-1
{
for(j=1;j<=(row-i);j++) // line-2
printf(" "); // line-3
for(k=1;k<=i;k++) // line-4
printf("* "); // line-5
printf("\n"); // line-6

}
getch();
}

/* OUTPUT:
enter row 4
*
* *
* * *
* * * *
*/
/* Program 7.33: Write a C program to print the following pattern (till n rows, where n is taken as
input).

/*
1

2 3 2

3 4 5 4 3

4 5 6 7 6 5 4

5 6 7 8 9 8 7 6 5 */

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,p;
clrscr();
printf("Enter Number of rows ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\t");
for(j=1;j<=(n-i);j++)
printf(" ");
p=i;
for(j=1;j<2*i;j++)
{
printf("%3d",p);
if(j>=i)
p--;
else
p++;
}

printf("\n\n");
}

getch();
}

/*

N = 5 i Char Blank
------------------
ABCDEEDCBA 1 A-E 0 Blank=2*(i-1)
ABCD..DCBA 2 A-D 2
ABC....CBA 3 A-C 4 Char={0 to (n-i)}+65
AB......BA 4 A-B 6
A........A 5 A-A 8
------------------
*/

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

void main(void)
{
int i,j,n;
clrscr();

printf("\n\n\t\tPlease enter the number of layers....");


scanf("%d",&n);

for(i=1;i<=n;i++)
{
for(j=0;j<=(n-i);j++)
{
printf("%c",j+65);
}
for(j=1;j<=2*(i-1);j++)
{
printf(".");
}

for(j=(n-i);j>=0;j--)
{
printf("%c",j+65);
}
printf("\n");
}
printf("\n\n\t\tEnd of the program....");

getch();
}

O/P-> Please enter the number of layers..........5

ABCDEEDCBA
ABCD .. DCBA
ABC .... CBA
AB ...... BA
A ........ A

End of the program......

/*
i b *
-----------
**** n=4 1 0 4
.*** 2 1 3 * => n-(i-1)
..** 3 2 2 b => i-1
...* 4 3 1
-----------

*/

#include <stdio.h>
#include<conio.h>
void main(void)
{
int n,i,j;
clrscr();
printf("\n\n\t\tPlease enter the number of terms....");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i-1;j++)
{
printf(".");
}

for(j=1;j<=(n-(i-1));j++)
{
printf("*");
}

printf("\n");
}

getch();
}

O/P-> Please enter the number of term......4


****
.***
..**
...*

/* 1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

*/
#include <stdio.h>
#include <conio.h>

void main()
{
int i,j,r,ncr=1;

clrscr();

printf("Enter the number of row");


scanf("%d",&r);

for(i=0;i<r;i++)
{
for(j=1;j<(r-i);j++)
printf(" ");
for(j=0;j<=i;j++)
{
if(j==0 || i==0)
ncr=1;
else
ncr=ncr*(i-j+1)/j;
printf("%6d",ncr);
}
printf("\n\n");
}
getch();
}

O/P->Enter the Number of row 5


1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

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

O/P->

1
0 1
0 1 0
1 0 1 0
1 0 1 0 1
/* Write a C program to illustrate Tower of Hannoi. */

#include<stdio.h>

void tower(int ,char ,char ,char );


void main()
{
int n;
printf("\n\n Enter number of disk");
scanf("%d",&n);
tower(n,'S','A','D');
}

void tower(int N,char S,char A,char D)


{
if (N== 1)
printf("\nMove from tower %c to tower %c",S,D) ;
else {
tower(N-1, S, D, A);
printf("\nMove from tower %c to tower %c", S,D) ;
tower(N-1, A, S, D);
}
}

O/P->
Enter number of disk 3

Move from tower S to tower D


Move from tower S to tower A
Move from tower D to tower A
Move from tower S to tower D
Move from tower A to tower S
Move from tower A to tower D
Move from tower S to tower D
-------------------------------------------------------------
Enter number of disk 4

Move from tower S to tower A


Move from tower S to tower D
Move from tower A to tower D
Move from tower S to tower A
Move from tower D to tower S
Move from tower D to tower A
Move from tower S to tower A
Move from tower S to tower D
Move from tower A to tower D
Move from tower A to tower S
Move from tower D to tower S
Move from tower A to tower D
Move from tower S to tower A
Move from tower S to tower D
Move from tower A to tower D
/*

n = 11 (odd) m = (n+1)/2 = 6
i b *
----------
.....* 1 5 1
....*** 2 4 3 b => m-i
...***** 3 3 5 * => 2*i-1
..******* 4 2 7
.********* 5 1 9
*********** __6__0_11__
.********* 7 1 9
..******* 8 2 7 b => i-m
...***** 9 3 5 * => 2*(n-i)+1
....*** 10 4 3
.....* 11 5 1
----------
*/

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

void main(void)
{
int i,j,n,b,s,m;

clrscr();
printf("\n\n\t\tPlease enter the number of layers....");
scanf("%d",&n);

m=(n+1)/2;

for(i=1;i<=n;i++)
{
if (i>m)
{
b=i-m;
s=2*(n-i)+1;
}
else
{
b=m-i;
s=2*i-1;
}

for(j=1;j<=b;j++)
{
printf(".");
}

for(j=1;j<=s;j++)
{
printf("*");
}

printf("\n");
}

printf("\n\n\t\tEnd of the program....");

getch();
}
/*
i j
------------------
1 1-10,1 => 10
3 1-10,3 => 4 count = 20 i = 11 j = 19
5 1-10,5 => 2
7 1-10,7 => 2
9 1-10,9 => 2
------------------
*/

/*
n = 11 (odd) i b * m = (n+1)/2 = 6
-----------
*********** 1 0 11
.********* 2 1 9 b => i-1
..******* 3 2 7 * => 2*(m-i)+1
...***** 4 3 5
....*** 5 4 3
.....* __6__5__1__
....*** 7 4 3
...***** 8 3 5 b => n-i
..******* 9 2 7 * => 2*(i-m)+1
.********* 10 1 9
*********** 11 0 11
-----------
*/

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

void main(void)
{
int s,b,m,n,i,j;

clrscr();

printf("\n\n\t\t\tPlease enter the odd number of layers....");


scanf("%d",&n);

m=(n+1)/2;

for(i=1;i<=n;i++)
{
if (i>m)
{
b=n-i;
s=2*(i-m)+1;
}
else
{
b=i-1;
s=2*(m-i)+1;
}

for(j=1;j<=b;j++)
{
printf(".");
}
for(j=1;j<=s;j++)
{
printf("*");
}

printf("\n");
}

getch();
}

/*
i j
--------------
1 1-3,2 => 2
2 1-6,4 => 2
3 1-9,6 => 2
4 1-12,8 => 2
5 1-15,10 => 2
-------------- ----
10 6 21

*/

/*
i *
--------
* n=5 1 1
** 2 2 * => i
*** 3 3
**** 4 4
***** 5 5
--------
*/

#include <stdio.h>

void main(void)
{
int n,i,j;

clrscr();

printf("\n\n\t\tPlease enter the number of terms....");


scanf("%d",&n);

for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}

getch();
}

/*
i b *
-----------
********* n=5 1 0 9
.******* 2 1 7 * => 2*(n-i)+1
..***** 3 2 5 b => i-1
...*** 4 3 3
....* 5 4 1
-----------

*/

#include <stdio.h>

void main(void)
{
int n,i,j;

clrscr();

printf("\n\n\t\tPlease enter the number of terms....");


scanf("%d",&n);

for(i=1;i<=n;i++)
{
for(j=1;j<=i-1;j++)
{
printf(".");
}

for(j=1;j<=2*(n-i)+1;j++)
{
printf("*");
}

printf("\n");
}

getch();
}
/*
i * m = (n+1)/2 = 5
--------
* n=9 1 1
** (odd) 2 2 * => i
*** 3 3
**** 4 4
***** __5__5__
**** 6 4
*** 7 3 * => (n-i)+1
** 8 2
* 9 1
--------

*/

#include <stdio.h>

void main(void)
{
int s,m,n,i,j;

clrscr();

printf("\n\n\t\tPlease enter the number of terms....");


scanf("%d",&n);

m=(n+1)/2;

for(i=1;i<=n;i++)
{
if (i>m) s=(n-i)+1; else s=i;

for(j=1;j<=s;j++)
{
printf("*");
}

printf("\n");
}

getch();
}

/*
i b * m = (n+1)/2 = 5
-----------
....* n=9 1 4 1
...*** (odd) 2 3 3 * => 2*i-1
..***** 3 2 5 b => m-i
.******* 4 1 7
********* __5__0__9__
.******* 6 1 7
..***** 7 2 5 * => 2*(n-i)+1
...*** 8 3 3 b => i-m
....* 9 4 1
-----------

*/

#include <stdio.h>

void main(void)
{
int b,s,m,n,i,j;

clrscr();

printf("\n\n\t\tPlease enter the number of terms....");


scanf("%d",&n);

m=(n+1)/2;

for(i=1;i<=n;i++)
{
if (i<=m)
{
s=2*i-1;
b=m-i;
}
else
{
s=2*(n-i)+1;
b=i-m;
}

for(j=1;j<=b;j++)
{
printf(".");
}

for(j=1;j<=s;j++)
{
printf("*");
}

printf("\n");
}
getch();
}

/*

n=6 i b *
---------
.....* 1 5 1
....*** 2 4 3 b => n-i
...***** 3 3 5
..******* 4 2 7 * => 2*i-1
.********* 5 1 9
*********** 6 0 11
---------
*/

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

void main(void)
{
int i,j,n,offset;

clrscr();

printf("\n\n\t\tPlease enter the number of layers....");


scanf("%d",&n);

offset=(80-(2*n-1))/2;

for(i=1;i<=n;i++)
{
for(j=1;j<=offset;j++)
{
printf(" ");
}

for(j=1;j<=(n-i);j++)
{
printf(".");
}

for(j=1;j<=2*i-1;j++)
{
printf("*");
}
printf("\n");
}

getch();
}

/*

n=13 (odd) m=(n+1)/2=7

i b *
-----------
......* 1 6 1
.....*** 2 5 3 b => m-i
....***** 3 4 5
...******* 4 3 7 * => 2*i-1
..********* 5 2 9
.*********** 6 1 11
************* _7___0__13_
.*********** 8 1 11
..********* 9 2 9 b => i-m
...******* 10 3 7
....***** 11 4 5 * => 2*(n-i)+1
.....*** 12 5 3
......* 13 6 1
-----------

*/

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

void main(void)
{
int i,j,n,m,s,b;
char ch;

clrscr();

do
{
printf("\n\n\t\tPlease enter the odd number of layers....");
scanf("%d",&n);

m=(n+1)/2;

for(i=1;i<=n;i++)
{
if (i<=m)
{
b=m-i;
s=2*i-1;
}
else
{
b=i-m;
s=2*(n-i)+1;
}

for(j=1;j<=b;j++)
{
printf(".");
}

for(j=1;j<=s;j++)
{
printf("*");
}

printf("\n");
}

printf("\n\n\t\tDo you want to continue? (Y/N)....");


ch=getche();
//printf("%c",ch);

}while(ch!='n' && ch!='N');

printf("\n\n\t\tEnd of the program....");

getch();
}

/* i # *. n = 5
--------
*.*.*.*.*. 1 0 5
#*.*.*.*. 2 1 4 # => i-1
##*.*.*. 3 2 3
###*.*. 4 3 2 *. => n-i+1
####*. 5 4 1
--------

*******
*****
***
*
*/
#include <stdio.h>
#include <conio.h>

void main(void)
{
int sum=0,i,n,j;

clrscr();

printf("\n\n\t\tPlease enter the number of layers....");


scanf("%d",&n);

for(i=1;i<=n;i++)
{
if(n%i==0)
{
sum=sum+i;
}
}

printf("\n\n\t\tSo the sum of factors of %d is %d....\n\n",n,sum);

for(i=1;i<=n;i++)
{
for(j=1;j<=(i-1);j++)
{
printf(" ");
}

for(j=1;j<=(n-i+1);j++)
{
printf("* ");
}

printf("\n");
}

getch();
}

/*

n = 6 i b *
-----------
.....* 1 5 1
....*** 2 4 3
...***** 3 3 5 * => 2*i-1
..******* 4 2 7 b => n-i
.********* 5 1 9
*********** 6 0 11
-----------
*/

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

void main(void)
{
int n,i,j;

clrscr();

printf("\n\n\t\tPlease enter the number of layers....");


scanf("%d",&n);

for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf(".");
}

for(j=1;j<=2*i-1;j++)
{
printf("*");
}

printf("\n");
}

getch();
}

/*

n=6 i *
----------
* 1 1
** 2 2
*** 3 3 * => i
**** 4 4
***** 5 5
****** 6 6
----------

*/

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

void main(void)
{
int i,j,n;

clrscr();

printf("\n\n\t\tPlease enter the number of layers....");


scanf("%d",&n);

for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("*");
}

printf("\n");
}

printf("\n\n\t\tEnd of the program....");

getch();
}

/*

n=6 i *
----------
A 1 1
AB 2 2
ABC 3 3 * => i
ABCD 4 4
ABCDE 5 5
ABCDEF 6 6
----------

*/

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

void main(void)
{
int i,j,n;

clrscr();

printf("\n\n\t\tPlease enter the number of layers....");


scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%c",j+64);
}

printf("\n");
}

printf("\n\n\t\tEnd of the program....");

getch();
}

/*

n=6 i b *
-------------
.....* 1 5 1
....*** 2 4 3
...***** 3 3 5 * => 2*i-1
..******* 4 2 7 b => n-i
.********* 5 1 9
*********** 6 0 11
-------------

*/

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

void main(void)
{
int i,j,n;

clrscr();

printf("\n\n\t\tPlease enter the number of layers....");


scanf("%d",&n);

for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf(".");
}

for(j=1;j<=2*i-1;j++)
{
printf("*");
}

printf("\n");
}

printf("\n\n\t\tEnd of the program....");

getch();
}

/*

n=11 (odd) i b* m = (n+1)/2 = 6


-------------
.....* 1 5 1
....*** 2 4 3
...***** 3 3 5 * => 2*i-1
..******* 4 2 7 b => m-i
.********* 5 1 9
*********** __6___0__11__
.********* 7 1 9
..******* 8 2 7 * => 2*(n-i)+1
...***** 9 3 5 b => i-m
....*** 10 4 3
.....* 11 5 1
-------------

*/

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

void main(void)
{
int i,j,n,s,b,m;

clrscr();

printf("\n\n\t\tPlease enter the number of layers....");


scanf("%d",&n);

m=(n+1)/2;

for(i=1;i<=n;i++)
{
if(i>m)
{
s=2*(n-i)+1;
b=i-m;
}
else
{
s=2*i-1;
b=m-i;
}

for(j=1;j<=b;j++)
{
printf(".");
}

for(j=1;j<=s;j++)
{
printf("*");
}

printf("\n");
}

printf("\n\n\t\tEnd of the program....");

getch();
}

/*

n=11 (odd) i b * m = (n+1)/2 = 6


-------------
*********** 1 5 1
.********* 2 4 3
..******* 3 3 5 * => 2*(m-i)+1
...***** 4 2 7 b => i-1
....*** 5 1 9
.....* __6___0__11__
....*** 7 1 9
...***** 8 2 7 * => 2*(i-m)+1
..******* 9 3 5 b => n-i
.********* 10 4 3
*********** 11 5 1
-------------
*/

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

void main(void)
{
int i,j,n,s,b,m;

clrscr();

printf("\n\n\t\tPlease enter the number of layers....");


scanf("%d",&n);

m=(n+1)/2;

for(i=1;i<=n;i++)
{
if(i>m)
{
s=2*(i-m)+1;
b=n-i;
}
else
{
s=2*(m-i)+1;
b=i-1;
}

for(j=1;j<=b;j++)
{
printf(".");
}

for(j=1;j<=s;j++)
{
printf("*");
}

printf("\n");
}

printf("\n\n\t\tEnd of the program....");

getch();
}

/*

n=21 (odd) i b * m = (n+1)/2 = 11


-------------
********************* 1 0 21
**********.********** 2 1 10
*********...********* 3 3 9
********.....******** 4 5 8 b => 2*i-3 ....*.
*******.......******* 5 7 7 ...*.*,
******.........****** 6 9 6 * => m-i+1 ..*.*.*.
*****...........***** 7 11 5 .*.*,*.*.
****.............**** 8 13 4 *.*.*.*.*.
***...............*** 9 15 3
**.................** 10 17 2
*...................* _11__19___1__
**.................** 12 17 2
***...............*** 13 15 3
****.............**** 14 13 4 b => 2*(n-i)-1
*****...........***** 15 11 5
******.........****** 16 9 6 * => i-m+1
*******.......******* 17 7 7
********.....******** 18 5 8
*********...********* 19 3 9
**********.********** 20 1 10
********************* 21 0 21
-------------
*/

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

void main(void)
{
int i,j,n,s,b,m;

clrscr();

printf("\n\n\t\tPlease enter the number of layers....");


scanf("%d",&n);

m=(n+1)/2;

for(i=1;i<=n;i++)
{
if (i==1 || i==n)
{
s=n;
b=0;
}
else
{
if(i>m)
{
s=i-m+1;
b=2*(n-i)-1;
}
else
{
s=m-i+1;
b=2*i-3;
}
}

for(j=1;j<=s;j++)
{
printf("*.");
}

for(j=1;j<=b;j++)
{
printf(".");
}

if (i!=1 && i !=n)


{
for(j=1;j<=s;j++)
{
printf("*");
}
}

printf("\n");
}

printf("\n\n\t\tEnd of the program....");

getch();
}

n=6 i *
----------
A 1 1
BB 2 2
CCC 3 3 * => i
DDDD 4 4
EEEEE 5 5
FFFFFF 6 6
----------

*/

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

void main(void)
{
int i,j,n;

clrscr();

printf("\n\n\t\tPlease enter the number of layers....");


scanf("%d",&n);

for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%c",i+64);
}

printf("\n");
}

printf("\n\n\t\tEnd of the program....");

getch();
}

/*

n = 13 m = (n+1)/2 = 7

i b *
-------------
......A 1 6 1
.....BBB 2 5 3 b => m-i
....CCCCC 3 4 5
...DDDDDDD 4 3 7 * => 2*i-1
..********* 5 2 9
.*********** 6 1 11
************* __7___0__13__
.*********** 8 1 11
..********* 9 2 9 b => i-m
...******* 10 3 7
....***** 11 4 5 * => 2*(n-i)+1
.....*** 12 5 3
......* 13 6 1
-------------

*/

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

void main(void)
{
int s,b,n,i,j,m;
clrscr();

printf("\n\n\t\tPlease enter the odd number of layers....");


scanf("%d",&n);

m=(n+1)/2;

for(i=1;i<=n;i++)
{
if (i>m)
{
b=i-m;
s=2*(n-i)+1;
}
else
{
b=m-i;
s=2*i-1;
}

for(j=1;j<=b;j++)
printf(".");

for(j=1;j<=s;j++)
printf("%c",i+64);

printf("\n");
}

getch();
}

/*

n = 13 m = (n+1)/2 = 7

i b *
-------------
......A 1 6 1
.....BBB 2 5 3 b => m-i
....CCCCC 3 4 5
...DDDDDDD 4 3 7 * => 2*i-1
..********* 5 2 9
.*********** 6 1 11
************* __7___0__13__
.*********** 8 1 11
..********* 9 2 9 b => i-m
...******* 10 3 7
....***** 11 4 5 * => 2*(n-i)+1
.....*** 12 5 3
......* 13 6 1
-------------

*/

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

void main(void)
{
int ch,s,b,n,i,j,m;

clrscr();

printf("\n\n\t\tPlease enter the odd number of layers....");


scanf("%d",&n);

m=(n+1)/2;
ch=64;

for(i=1;i<=n;i++)
{
if (i>m)
{
b=i-m;
s=2*(n-i)+1;
}
else
{
b=m-i;
s=2*i-1;
}

for(j=1;j<=b;j++)
printf(".");

ch=ch-(i>m)+(i<=m);

for(j=1;j<=s;j++)
printf("%c",ch);

printf("\n");
}

getch();
}

/*
n = 6
i *
--------
* 1 1
** 2 2
*** 3 3 * => i
**** 4 4
***** 5 5
****** 6 6
--------

*/

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

void main(void)
{
int i,j,n;

clrscr();

printf("\n\n\t\tPlease enter the number of layers....");


scanf("%d",&n);

for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("*");
}

printf("\n");
}

printf("\n\t\tEnd of the pattern....");

getch();
}

/*

integer i,j

write(*,*)'Doctor visit'

do 100 i=1,7
write(*,*) 'Good morning'

do 200 j=1,3
write(*,*) ' Day number = ',i,' and Medicine number = ',j
200 continue

write(*,*) 'Good night'

100 continue

write(*,*) 'Thank you'

*/

/*
n = 6
i *
--------
* 1 1
** 2 2
*** 3 3 * => i
**** 4 4
***** 5 5
****** 6 6
--------

*/

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

void main(void)
{
int i,j,n,m,d;

clrscr();

printf("\n\t\tDoctor's visit....");

for(i=1;i<=7;i++)
{
printf("\n\t\tGood morning....");

for(j=1;j<=3;j++)
{
printf("\n\t\tDay number = %d and medicine number =
%d....",i,j);
}

printf("\n\t\tGood night....");
}

printf("\n\t\tThank you, Doctor....");


getch();
}

/*

integer i,j

write(*,*)'Doctor visit'

do 100 i=1,7
write(*,*) 'Good morning'

do 200 j=1,3
write(*,*) ' Day number = ',i,' and Medicine number = ',j
200 continue

write(*,*) 'Good night'

100 continue

write(*,*) 'Thank you'

*/

/*
n = 6
i b *
-----------
.....* 1 5 1
....*** 2 4 3 b => n-i
...***** 3 3 5 * => 2*i-1
..******* 4 2 7
.********* 5 1 9
*********** 6 0 11
-----------

*/

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

void main(void)
{
int i,j,n;

clrscr();

printf("\n\n\t\tPlease enter the number of layers....");


scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf(".");
}

for(j=1;j<=2*i-1;j++)
{
printf("*");
}

printf("\n");
}

printf("\n\t\tEnd of the pattern....");

getch();
}

/*

integer i,j

write(*,*)'Doctor visit'

do 100 i=1,7
write(*,*) 'Good morning'

do 200 j=1,3
write(*,*) ' Day number = ',i,' and Medicine number = ',j
200 continue

write(*,*) 'Good night'

100 continue

write(*,*) 'Thank you'

*/

/*
n = 11 (odd) m=(n+1)/2=6

i b *
-----------
.....* 1 5 1
....*** 2 4 3 b => m-i
...***** 3 3 5 * => 2*i-1
..******* 4 2 7
.********* 5 1 9
*********** __6__0_11__
.********* 7 1 9
..******* 8 2 7 b => i-m
...***** 9 3 5 * => 2*(n-i)+1
....*** 10 4 3
.....* 11 5 1
-----------

*/

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

void main(void)
{
int i,j,n,m,b,s;

clrscr();

printf("\n\n\t\tPlease enter the number of layers....");


scanf("%d",&n);

m=(n+1)/2;

for(i=1;i<=n;i++)
{
if (i>m)
{
s=2*(n-i)+1;
b=i-m;
}
else
{
s=2*i-1;
b=m-i;
}

for(j=1;j<=b;j++)
{
printf(".");
}

for(j=1;j<=s;j++)
{
printf("*");
}

printf("\n");
}

printf("\n\t\tEnd of the pattern....");


getch();
}

/*

integer i,j

write(*,*)'Doctor visit'

do 100 i=1,7
write(*,*) 'Good morning'

do 200 j=1,3
write(*,*) ' Day number = ',i,' and Medicine number = ',j
200 continue

write(*,*) 'Good night'

100 continue

write(*,*) 'Thank you'

*/

/*
n = 11 (odd) m=(n+1)/2=6

i b *
-----------
*********** 1 0 11
.********* 2 1 9 b => i-1
..******* 3 2 7 * =>2*m-(2*i-1)
...***** 4 3 5
....*** 5 4 3
.....* __6__5__1__
....*** 7 4 3
...***** 8 3 5 b =>n-i
..******* 9 2 7 * =>2*i-n
*********** 11 0 11
-----------

*/

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

void main(void)
{
int i,j,n,m,b,s;

clrscr();

printf("\n\n\t\tPlease enter the number of layers....");


scanf("%d",&n);

m=(n+1)/2;

for(i=1;i<=n;i++)
{
if (i>m)
{
s=2*i-n;
b=n-i;
}
else
{
s=2*m-(2*i-1);
b=i-1;
}

for(j=1;j<=b;j++)
{
printf(".");
}

for(j=1;j<=s;j++)
{
printf("*");
}

printf("\n");
}

printf("\n\t\tEnd of the pattern....");

getch();
}

/*

integer i,j

write(*,*)'Doctor visit'

do 100 i=1,7
write(*,*) 'Good morning'
do 200 j=1,3
write(*,*) ' Day number = ',i,' and Medicine number = ',j
200 continue

write(*,*) 'Good night'

100 continue

write(*,*) 'Thank you'

*/

/* n = 11 (odd levels) i b *
-----------
*********** 1 0 11
.********* m = (n+1)/2 2 1 9 b => i-1
..******* = 6 3 2 7
...***** 4 3 5 * => 2*(m-i)+1
....*** 5 4 3
.....* __6__5__1__
....*** 7 4 3
...***** 8 3 5 b => n-i
..******* 9 2 7
.********* 10 1 9 * => 2*(i-m)+1
*********** 11 0 11
-----------
*/

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

void main(void)
{
int i,j,m,n,s,b;

clrscr();

printf("\n\n\t\tPlease enter the odd number of layers....");


scanf("%d",&n);

m=(n+1)/2;

printf("\n\n\t\tNow displaying the pattern....\n\n");

for(i=1;i<=n;i++)
{
if (i>m)
{
b=n-i;
s=2*(i-m)+1;
}
else
{
b=i-1;
s=2*(m-i)+1;
}

for(j=1;j<=b;j++)
{
printf(".");
}

for(j=1;j<=s;j++)
{
printf("*");
}

printf("\n");
}

printf("\n\n\t\tEnd of the program....");

getch();
}

/*
n = 15 (odd) m = (n+1)/2 = 8

i b *
-------------
.......* 1 7 1
......*** 2 6 3
.....***** 3 5 5 b => m-i
....******* 4 4 7
...********* 5 3 9 * => 2*i-1
..*********** 6 2 11
.************* 7 1 13
*************** __8___0__15__
.************* 9 1 13
..*********** 10 2 11 b => i-m
...********* 11 3 9
....******* 12 4 7 * => 2*(n-i)+1
.....***** 13 5 5
......*** 14 6 3
.......* 15 7 1
-------------
*/

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

main()
{
int i,j,n,m,s,b;

clrscr();

do
{
printf("\n\n\t\tPlease enter the odd number of
layers....");
scanf("%d",&n);
}while(n%2==0);

m=(n+1)/2;

printf("\n\n\t\tNow printing the pattern....\n\n");

for(i=1;i<=n;i++)
{
if (i>m)
{
b=i-m;
s=2*(n-i)+1;
}
else
{
b=m-i;
s=2*i-1;
}

for(j=1;j<=b;j++)
{
printf(".");
}

for(j=1;j<=s;j++)
{
printf("*");
}

printf("\n");
}

printf("\n\n\t\tEnd of the program....");

getch();

return(0);
}
/*
m = (n+1)/2 = 6
n = 11 (odd) i * b *
-----------
*********** 1 11 0 0 b => 2*(i-1)-1 (except i = 1)
*****.***** 2 5 1 5
****...**** 3 4 3 4 * => m-i+1
***.....*** 4 3 5 3
**.......** 5 2 7 2
*.........* _6__1__9__1_
**.......** 7 2 7 2 b => 2*(n-i)-1 (except i = 11)
***.....*** 8 3 5 3
****...**** 9 4 3 4 * => i-m+1
*****.***** 10 5 1 5
*********** 11 11 0 0
------------

*/

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

void main(void)
{
int i,j,n,m,s,b;

clrscr();

printf("\n\n\t\tPlease enter the odd number of layers....");


scanf("%d",&n);

m=(n+1)/2;

for(i=1;i<=n;i++)
{
if (i==1 || i==n)
{
b=0;
s=n;
}
else
{
if (i>m)
{
b=2*(n-i)-1;
s=i-m+1;
}
else
{
b=2*(i-1)-1;
s=m-i+1;
}
}

for(j=1;j<=s;j++)
{
printf("*");
}

for(j=1;j<=b;j++)
{
printf(".");
}

for(j=1;j<=s && i!=n && i!=1;j++)


{
printf("*");
}

printf("\n");
}

getch();
}

/*
i j
----
1 1-10,1 = 10
3 3-10,3 = 3
5 5-10,5 = 2
---
15 i = 7 j = 15

*/

/*

n = 6 i b *
---------
.....* 1 5 1 b => n-i
....*** 2 4 3
...***** 3 3 5 * => 2*i-1
..******* 4 2 7
.********* 5 1 9
*********** 6 0 11
---------

************
.**********
..********
...******
....***
.....*
n = 6 i b *.
---------
.....*. 1 5 1
....*.*. 2 4 2 b => n-i
...*.*.*. 3 3 3 *. => i
..*.*.*.*. 4 2 4
.*.*.*.*.*. 5 1 5
*.*.*.*.*.*. 6 0 6
---------

*/

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

void main(void)
{
int i,j,n;

clrscr();

printf("\n\n\t\tPlease enter the number of layers....");


scanf("%d",&n);

for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf(".");
}

for(j=1;j<=i;j++)
{
printf("*");

if (i!=j)
printf(".");

or

if (i==j)
printf("*");
else
printf("*.");
}

printf("\n");
}

printf("\n\n\t\tEnd of the program....");

getch();
}

/*
i j
---------- i= 1 to 5
1 1-3 ,1 => 3 j= 1 to i
3 3-9 ,3 => 3 15 times
5 5-15,5 => 3
7 7-21,7 => 3
9 9-27,9 => 3
---------- ---
15

*/
/*
looping in C
1 > for loop
2 > do-while
3 > while-do
*/

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy