Programs Based on Displaying Patterns
15. Program to display the following pattern:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
#include <stdio.h>
void main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d ",j);
}
printf("\n");
}
}
16. Program to display the following numeric pattern:
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
#include <stdio.h>
void main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=5;j>=i;j--)
{
printf("%d ",j);
}
printf("\n");
}
}
16. Program to display the following numeric pattern:
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
#include <stdio.h>
void main()
{
int i,j;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
printf("%d ",j);
}
printf("\n");
}
}
17. Program to display the following numeric pattern:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
#include <stdio.h>
void main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d ",i);
}
printf("\n");
}
}
18. Program to display the following numeric pattern:
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
#include <stdio.h>
void main()
{
int i,j;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
printf("%d ",i);
}
printf("\n");
}
}
19. Program to display the following numeric pattern:
2 4 6 8 10
2 4 6 8
2 4 6
2 4
2
#include <stdio.h>
void main()
{
int i,j;
for(i=10;i>=2;i=i-2)
{
for(j=2;j<=i;j=j+2)
{
printf("%d ",j);
}
printf("\n");
}
}
Programs based on Matrix and Arrays
20. Program to find the addition of two 3*3 matrices.
#include <stdio.h>
void main()
{
int a[3][3],b[3][3],sum[3][3],i,j;
printf("Enter the elements for matrix A:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter the number for place of %d%d:",i,j);
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements for matrix B:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter the number for place of %d%d:",i,j);
scanf("%d",&b[i][j]);
}
}
printf("\n The sum of the two matrix is :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
sum[i][j]=a[i][j]+b[i][j];
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",sum[i][j]);
}
printf("\n");
}
}
21. Program to find the addition of any two matrices of size 2*2 using an array.
void main()
{
int a[2][2],b[2][2],sum[2][2],i,j;
printf(“enter the elements for matrix a: \n”);
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf(“enter the number %d%d: ”,i,j);
scanf(“%d”,&a[i][j]);
}
}
printf(“enter the elements for matrix b: \n”);
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf(“enter the number %d%d: ”,i,j);
scanf(“%d”,&b[i][j]);
}
}
printf(“\n The sum of two matrics is : \n”);
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
sum[i][j]=a[i][j]+b[i][j];
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf(“%d\t”,sum[i][j]);
}
printf(“\n”);
}
getch();
}
22. Program to enter elements into 4*4matrix and find the sum of the elements of the matrix.
#include <stdio.h>
void main()
{
int a[4][4],b[4][4],sum[4][4],i,j;
printf("Enter the elements for matrix a: \n");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
printf("Enter the number %d%d: ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements for matrix B: \n");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
printf("Enter the number %d%d: ",i,j);
scanf("%d",&b[i][j]);
}
}
printf("\nThe sum of two matrics is : \n");
for(i=0;i<2;i++)
{
for(j=0;j<4;j++)
{
sum[i][j]=a[i][j]+b[i][j];
}
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
printf("%d\t",sum[i][j]);
}
printf("\n");
}
}
Commenti