pattern&matrix questions
pattern&matrix questions
/* *****
*****
*****
***** */
#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");
}
}
/* *****
* *
* *
***** */
#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");
}
}
/* 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;
}
/* 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;
}
/*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;
}
/* 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;
}
#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;
}
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);
}
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;
}