36. Pascal triangle
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
#include<stdio.h>
#include<conio.h>
long fact(int);
void main()
{
int line, i, j;
clrscr();
printf("Enter the number : ");
scanf("%d", &line);
for (i = 0; i < line; i++)
{
for (j = 0; j < line - i - 1; j++)
{
printf(" ");
}
for (j = 0; j <= i; j++)
{
printf("%ld ", fact(i) / (fact(j) * fact(i - j)));
}
printf("\n");
}
getch();
}
long fact(int num)
{
long f = 1;
int i = 1;
while (i <= num)
{
f = f * i;
i++;
}
return f;
}
37. Pascal triangle without using function
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
#include<stdio.h>
#include<conio.h>
void main()
{
int x, y, n, a, z, s;
clrscr();
printf("Enter the number : ");
scanf("%d", &n);
s = n;
for (x = 0; x <= n; x++)
{
a = 1;
for (z = s; z >= 0; z--)
printf(" ");
s--;
for (y = 0; y <= x; y++)
{
printf("%d ", a);
a = (a * (x - y) / (y + 1));
}
printf("\n");
}
getch();
}
38. Pascal triangle 2
1
121
12321
1234321
123454321
#include<stdio.h>
#include<conio.h>
void main()
{
int n, c, k, number = 1, space = n;
clrscr();
printf("Enter number of rows : ");
scanf("%d", &n);
printf("\n");
space = n;
for (c = 1; c <= n; c++)
{
for (k = space; k > 1; k--)
printf(" ");
space--;
for (k = 1; k <= 2 * c - 1; k++)
{
if (k <= c)
{
printf("%d", number);
if (k < c)
number++;
}
else
{
number--;
printf("%d", number);
}
}
number = 1;
printf("\n");
}
getch();
}
39. Number Alphabet Pattern
1
A B
2 3 4
C D E F
5 6 7 8 9
G H I J K L
#include<stdio.h>
#include<conio.h>
void main()
{
int num, r, c;
int i = 1;
char ch = 'A';
clrscr();
printf("Enter the number of rows : ");
scanf("%d", &num);
printf("\n");
for (r = 1; r <= num; r++)
{
for (c = 1; c <= r; c++)
{
if (r % 2 == 0)
{
printf(" %c", ch++);
}
else
{
printf(" %d", i++);
}
}
printf("\n");
}
getch();
}
40. Number Diamond Pattern
1
1 2 3
1 2 3 4 5
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7
1 2 3 4 5
1 2 3
1
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, k;
clrscr();
for(i=1;i<=5;i++)
{
for(j=i;j<5;j++)
{
printf(" ");
}
for(k=1;k<(i*2);k++)
{
printf("%d",k);
}
printf("\n");
}
for(i=4;i>=1;i--)
{
for(j=5;j>i;j--)
{
printf(" ");
}
for(k=1;k<(i*2);k++)
{
printf("%d",k);
}
printf("\n");
}
getch();
}
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
#include<stdio.h>
#include<conio.h>
long fact(int);
void main()
{
int line, i, j;
clrscr();
printf("Enter the number : ");
scanf("%d", &line);
for (i = 0; i < line; i++)
{
for (j = 0; j < line - i - 1; j++)
{
printf(" ");
}
for (j = 0; j <= i; j++)
{
printf("%ld ", fact(i) / (fact(j) * fact(i - j)));
}
printf("\n");
}
getch();
}
long fact(int num)
{
long f = 1;
int i = 1;
while (i <= num)
{
f = f * i;
i++;
}
return f;
}
37. Pascal triangle without using function
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
#include<stdio.h>
#include<conio.h>
void main()
{
int x, y, n, a, z, s;
clrscr();
printf("Enter the number : ");
scanf("%d", &n);
s = n;
for (x = 0; x <= n; x++)
{
a = 1;
for (z = s; z >= 0; z--)
printf(" ");
s--;
for (y = 0; y <= x; y++)
{
printf("%d ", a);
a = (a * (x - y) / (y + 1));
}
printf("\n");
}
getch();
}
38. Pascal triangle 2
1
121
12321
1234321
123454321
#include<stdio.h>
#include<conio.h>
void main()
{
int n, c, k, number = 1, space = n;
clrscr();
printf("Enter number of rows : ");
scanf("%d", &n);
printf("\n");
space = n;
for (c = 1; c <= n; c++)
{
for (k = space; k > 1; k--)
printf(" ");
space--;
for (k = 1; k <= 2 * c - 1; k++)
{
if (k <= c)
{
printf("%d", number);
if (k < c)
number++;
}
else
{
number--;
printf("%d", number);
}
}
number = 1;
printf("\n");
}
getch();
}
39. Number Alphabet Pattern
1
A B
2 3 4
C D E F
5 6 7 8 9
G H I J K L
#include<stdio.h>
#include<conio.h>
void main()
{
int num, r, c;
int i = 1;
char ch = 'A';
clrscr();
printf("Enter the number of rows : ");
scanf("%d", &num);
printf("\n");
for (r = 1; r <= num; r++)
{
for (c = 1; c <= r; c++)
{
if (r % 2 == 0)
{
printf(" %c", ch++);
}
else
{
printf(" %d", i++);
}
}
printf("\n");
}
getch();
}
40. Number Diamond Pattern
1
1 2 3
1 2 3 4 5
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7
1 2 3 4 5
1 2 3
1
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, k;
clrscr();
for(i=1;i<=5;i++)
{
for(j=i;j<5;j++)
{
printf(" ");
}
for(k=1;k<(i*2);k++)
{
printf("%d",k);
}
printf("\n");
}
for(i=4;i>=1;i--)
{
for(j=5;j>i;j--)
{
printf(" ");
}
for(k=1;k<(i*2);k++)
{
printf("%d",k);
}
printf("\n");
}
getch();
}
0 comments:
Post a Comment