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

pattern&matrix questions

The document contains a series of C programming exercises focused on printing various patterns (solid and hollow rectangles, pyramids, diamonds) and performing matrix operations (addition, subtraction, transpose, identifying triangular matrices, and calculating sums). Each exercise includes a brief description and a complete C code implementation. The document serves as a practical guide for learning programming concepts related to patterns and matrices.

Uploaded by

Ananya Samanta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

pattern&matrix questions

The document contains a series of C programming exercises focused on printing various patterns (solid and hollow rectangles, pyramids, diamonds) and performing matrix operations (addition, subtraction, transpose, identifying triangular matrices, and calculating sums). Each exercise includes a brief description and a complete C code implementation. The document serves as a practical guide for learning programming concepts related to patterns and matrices.

Uploaded by

Ananya Samanta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Pattern and Matrix

1. WAP to print Solid rectangle star pattern

/* *****
*****
*****
***** */
#include<stdio.h>
int main(){
int i,j,r,c;
printf("Enter no of rows\n");
scanf("%d",&r);
printf("Enter no of columns\n");
scanf("%d",&c);
for(i=1;i<=r;i++){
for(j=1;j<=c;j++){
printf("*");
}
printf("\n");
}
}

2. WAP to print holow rectangle star pattern

/* *****
* *
* *
***** */
#include<stdio.h>
int main(){
int i,j,r,c;
printf("Enter no of rows\n");
scanf("%d",&r);
printf("Enter no of columns\n");
scanf("%d",&c);
for(i=1;i<=r;i++){
for(j=1;j<=c;j++){
if(i==1||i==r||j==1||j==c){
printf("*");
}else{
printf(" ");
}
}
printf("\n");
}
}

3. WAP to print Pyramid pattern using stars

/* * i=1 spaces=3 star=1


*** i=2 spaces=2 star=3
***** i=3 1 5
******* i=4 0 7
spaces=(r-i) star=(2*i-1) */
#include<stdio.h>
int main(){
int i,j,r;
printf("Enter no of rows\n");
scanf("%d",&r);
for(i=1;i<=r;i++){
for(j=1;j<=r-i;j++){
printf(" ");
}
for(j=1;j<=2*i-1;j++){
printf("*");
}
printf("\n");
}
return 0;
}

4. WAP to print Pyramid pattern using numbers

/* 1
1 2
1 2 3
1 2 3 4 */
#include <stdio.h>
int main()
{
int r,i,j;
printf("Enter the number of rows you want:");
scanf("%d",&r);
for(i=1; i<=r; i++){
for(j=1; j<=r-i; j++ ){
printf(" ");
}
for(j=1; j<=i; j++)
{
printf("%2d",j);

}
printf("\n");
}
return 0;
}

5. WAP to print Diamond pattern using numbers

/* 1
12
123
1234
12345
12345
1234
123
12
1 */
#include <stdio.h>
int main()
{
int r,i,j;
printf("Enter the number of rows you want:");
scanf("%d",&r);
for(i=1; i<=r; i++){
for(j=1; j<=r-i; j++ ){
printf(" ");
}
for(j=1; j<=i; j++)
{
printf("%2d",j);

}
printf("\n");
}
for(i=r; i>=1; i--){
for(j=1; j<=r-i; j++ ){
printf(" ");
}
for(j=1; j<=i; j++)
{
printf("%2d",j);

}
printf("\n");
}

return 0;
}
// Diamond pattern using stars
#include <stdio.h>
int main()
{
int r,i,j;
printf("Enter the number of rows you want:");
scanf("%d",&r);
for(i=1; i<=r; i++){
for(j=1; j<=r-i; j++ ){
printf(" ");
}
for(j=1; j<=2*i-1; j++)
{
printf("*");

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

}
printf("\n");
}

return 0;
}

6. WAP to print Floyd’s triangle

/*1
23
456
78910 */
#include<stdio.h>
int main(){
int i,j,number=1,n;
printf ("Enter number of rows\n");
scanf("%d",&n);
for(i=1;i<=n;i++){
for(j=1;j<=i;j++){
printf("%d",number);
number++;
}
printf("\n");
}
return 0;
}

7. WAP to print Pascal triangle

/* 1
1 1
1 2 1
1 3 3 1
1 4 6 4 1 */
#include <stdio.h>
int main()
{
int r,i,j,k;
printf("Enter the number of rows you want:");
scanf("%d",&r);
for(i=1; i<=r; i++){
for(j=1; j<=r-i; j++ ){
printf(" ");
}
int flag=1;
for(k=1; k<=i; k++)
{
printf("%2d",flag);
flag=flag*(i-k)/k;
}
printf("\n");
}
return 0;
}
//Or,
#include <stdio.h>
int main()
{
int r,i,j,value=1;
printf("Enter the number of rows you want:");
scanf("%d",&r);
for(i=1; i<=r; i++){
for(j=1; j<=r-i; j++ ){
printf(" ");
}
for(j=1; j<=i; j++)
{
if(j==1)
value=1;
else
value=value*(i-j+1)/(j-1);
printf("%2d",value);
}
printf("\n");
}
printf("Pascal's pyramid pattern\n");
return 0;
}

8. WAP to operate Matrix operations (Addition, subtraction and multiplication)

#include <stdio.h>
int main()
{
int i,j,A[50][50],B[50][50],C[50][50],r,c ;
printf("Enter number of row and column of matrix A\n");
scanf("%d%d",&r,&c);
printf("Enter elements of matrix A\n");
for(i=0;i<r;i++){
for(j=0;j<c;j++){
scanf("%d",&A[i][j]);
}
}
printf("Enter number of row and column of matrix B\n");
scanf("%d%d",&r,&c);
printf("Enter elements of matrix B\n");
for(i=0;i<r;i++){
for(j=0;j<c;j++){
scanf("%d",&B[i][j]);
}
}
printf("Sum of matrices\n");
for(i=0;i<r;i++){
for(j=0;j<c;j++){
C[i][j]=A[i][j]+B[i][j];
printf("%d\t",C[i][j]);
}
printf("\n");
}
}
9. WAP to find out Transpose of a matrix
#include <stdio.h>
int main()
{
int i,j,A[50][50],r,c,sum_row,sum_column;
printf("Enter number of row and column of matrix \n");
scanf("%d%d",&r,&c);
printf("Enter elements of matrix \n");
for(i=0;i<r;i++){
for(j=0;j<c;j++){
scanf("%d",&A[i][j]);
}
}

for(i=0;i<r;i++){
for(j=0;j<c;j++){
printf("%d\t",A[j][i]);
}
printf("\n");
}
return 0;
}

10. WAP to identify Upper triangular matrix or not


#include <stdio.h>
int main()
{
int i,j,A[50][50],r,c,isUpper ;
printf("Enter number of row and column of the matrix \n");
scanf("%d%d",&r,&c);
printf("Enter elements of the matrix \n");
for(i=0;i<r;i++){
for(j=0;j<c;j++){
scanf("%d",&A[i][j]);
}
}
isUpper=1;
for(i=0;i<r;i++){
for(j=0;j<c;j++){
if(j<i && A[i][j]!=0){
isUpper=0;
}
}
}
if(isUpper==1){
printf("The matrix is uppertriangular\n");
}else{
printf("The matrix is not upper triangular\n");
}
}

11. WAP to identify Lower triangular matrix or not


#include <stdio.h>
int main()
{
int i,j,A[50][50],r,c,isLower ;
printf("Enter number of row and column of the matrix \n");
scanf("%d%d",&r,&c);
printf("Enter elements of the matrix \n");
for(i=0;i<r;i++){
for(j=0;j<c;j++){
scanf("%d",&A[i][j]);
}
}
isLower=1;
for(i=0;i<r;i++){
for(j=0;j<c;j++){
if(j>i && A[i][j]!=0){
isLower=0;
}
}
}
if(isLower==1){
printf("The matrix is lowertriangular\n");
}else{
printf("The matrix is not lower triangular\n");
}
}

12. WAP to find out the maximum element in a row of an matrix


#include <stdio.h>
int main()
{
int i,j,A[50][50],r,c ,max,min;
printf("Enter number of row and column of matrix A\n");
scanf("%d%d",&r,&c);
printf("Enter elements of matrix A\n");
for(i=0;i<r;i++){
for(j=0;j<c;j++){
scanf("%d",&A[i][j]);
}
}
printf("Enter the index of the row");
scanf("%d",&i);
max=min=A[i][0];

for(j=0;j<c;j++){
if (A[i][j]>max){
max=A[i][j];
}
if(A[i][j]<min){
min=A[i][j];
}
}
printf("%d\n",max);
printf("%d\n",min);
}

13. WAP to find out the maximum element in a column of an matrix


#include <stdio.h>
int main()
{
int i,j,A[50][50],r,c ,max,min;
printf("Enter number of row and column of matrix A\n");
scanf("%d%d",&r,&c);
printf("Enter elements of matrix \n");
for(i=0;i<r;i++){
for(j=0;j<c;j++){
scanf("%d",&A[i][j]);
}
}

printf("Enter the index of the column");


scanf("%d",&j);
max=min=A[0][j];
for(i=0;i<r;i++){
if (A[i][j]>max){
max=A[i][j];
}
if(A[i][j]<min){
min=A[i][j];
}
}
printf("max=%d\n",max);
printf("min=%d\n",min);
}
// for each column of the matrix
for(j=0;j<c;j++){
max=min=A[0][j];
for(i=0;i<r;i++){
if (A[i][j]>max){
max=A[i][j];
}
if(A[i][j]<min){
min=A[i][j];
}
}
printf("max=%d\n",max);
printf("min=%d\n",min);
}

14. WAP to find out Sum of each row and column of a matrix

#include <stdio.h>
int main()
{
int i,j,A[50][50],r,c,sum_row,sum_column;
printf("Enter number of row and column of matrix \n");
scanf("%d%d",&r,&c);
printf("Enter elements of matrix \n");
for(i=0;i<r;i++){
for(j=0;j<c;j++){
scanf("%d",&A[i][j]);
}
}

for(i=0;i<r;i++){
sum_row=0;
for(j=0;j<c;j++){
sum_row=sum_row+A[i][j];
}
printf("sum of row index %d =%d\n",i,sum_row);
}

for(j=0;j<c;j++){
sum_column=0;
for(i=0;i<r;i++){
sum_column=sum_column+A[i][j];
}
printf("sum of column index %d =%d\n",j,sum_column);
}
return 0;
}

15. WAP to find out Sum of boundary elements of a matrix


#include<stdio.h>
int main(){
int r, c,i,j, sum = 0,A[50][50];
printf("Enter number of rows and column of the matrix :\n ");
scanf("%d %d",&r,&c);
printf("Input the matrix elements\n");
for(i = 0; i<r; i++){
for(j = 0; j<c; j++)
scanf("%d",&A[i][j]);
}
printf("Boundary Matrix\n");
for(i = 0; i<r; i++){
for(j = 0; j<c; j++){
if (i == 0|| j == 0||i==r-1||j==c-1){
printf("%d ", A[i][j]);
sum = sum + A[i][j];
}else{
printf(" ");
}
printf("\n ");
}
}
printf("Sum of boundary is %d", sum);
}

You might also like