skip to main |
skip to sidebar
/*
Name: WAP to print binary triangle
Author: Parveen Malik
Date: 18/09/11 05:40
*/
#include<stdio.h>
int main()
{
int i,j,row,mod,temp=1;
char inputstr[35]; /* make longer than reasonable input will expected */
/* error checking should still be employed */
/* to clear buffer overflow, if input is too long */
while ( temp==1)
{
row=0;
while(row<=0)
{
printf("\n Enter the number of rows you want : ");
fflush(stdout); /* printf didn't have newline, need to flush */
fgets(inputstr,34,stdin);
/* error checking should go here for buffer overflow */
row = atoi (inputstr);
if(row<=0)
printf("invalid input, try again \n");
}
for ( i=0;i<=row-1;i++)
{
mod=i%2;
for(j=0;j<=i;j++)
{
printf("%d",mod);
if(mod==0)
mod=1;
else
mod=0;
}
printf("\n");
}
}
return 0;
}
OUTPUT:
/*
Name: WAP to calculate the addition of two matrics
Author: Parveen Malik
Date: 18/09/11 03:31
*/
#include<stdio.h>
#include<conio.h>
int main(void)
{
float a[10][10],b[10][10],c[10][10];
int i,j,m,n;
printf("Enter the size of Matrices as m,n : ");
scanf("%d%d",&m,&n);
printf("Enter %d elements of Matrix A row-wise\n",m*n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%f",&a[i][j]);
}
}
printf("Enter the %d elements of Matrix B row-wise\n",m*n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%f",&b[i][j]);
}
}
/* now add A Matrix + B Matrix */
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
/* to print the result */
printf("\n Addition of Matrix A + Matrix B : \n\n");
for (i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%3.2f\t",c[i][j]);
}
printf("\n");
}
getch();
return 0;
}
OUTPUT :
/*
Name: WAP to find the smallest of 10 numbers using for loop
Author: Parveen Malik
Date : 18/09/11 03:21
*/
#include<stdio.h>
#include<conio.h>
int main(void)
{
int a[15],min,i;
printf("Enter any ten numbers \n");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
if(i==0)
min=a[i];
if( a[i]<min)
min=a[i];
}
printf("\nMinimum : %d",min);
getch();
return 0;
}
OUTPUT :
/*
Name: WAP to find the largest of 10 numbers using for loop
Author: Parveen Malik
Date: 18/09/11 02:58
*/
#include<stdio.h>
#include<conio.h>
int main(void)
{
int a[15],max,i;
printf("Enter any ten numbers \n");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
if(i==0)
max=a[i];
if( a[i]>max)
max=a[i];
}
printf("\nMaximum : %d",max);
getch();
return 0;
}
OUTPUT :
/*
Name: WAP to Multiply two Matrix
Author: Parveen Malik
Date: 18/09/11 01:50
*/
#include<stdio.h>
#include<conio.h>
int main()
{
float a[10][10], b[10][10],c[10][10];
int m,n,p,q,i,j,k;
printf("Enter the size of Matrix A as m,n : ");
scanf("%d%d",&m,&n);
printf("Enter the size of Matrix B as p,q : ");
scanf("%d%d",&p,&q);
if(n!=p)
{
printf("\n Matrix Multipication AxB is not feasible\n");
exit(1);
}
printf("\n Enter the %d elements of Matrix A row-wise \n",m*n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%f",&a[i][j]);
}
}
printf("\n Enter the %d elements of Matrix B row-wise \n",p*q);
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%f",&b[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
printf("\n Product AxB is : \n\n\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%5.2f\t",c[i][j]);
}
printf("\n");
}
getch();
return 0;
}
OUTPUT :