Ticker

6/recent/ticker-posts

Header Ads Widget

Contact Us For Advertisement: 9858091920

C Programming Part 1

C Programming:

1) Sum of first 100 odd Number
2) Find maximum and minimum 
3) Matrix Multiplication 
4) Addition and Subtraction of two matrix



/* 1. Sum of first 100 odd numbers */ 
/* Program Coded By Chetan Thapa Magar. */ 
#include<stdio.h>
#include<conio.h>
void main()
{
int i, k, sum=0, l;
printf("Enter how many odd numbers:\n");
scanf("%d", &k);
l=2*k-1;
for(i=1; i<=l; i+=2)
sum=sum+i;
printf("\nThe sum of odd numbers from 1 to %d is %d", l, sum);
getch();
}
Output / Execution:
Enter how many odd numbers:
100 

The sum of odd numbers from 1 to 199 is 10000 

/* Find maximum and minimum */ 
/* Program Coded By Chetan Thapa Magar */ 


#include<stdio.h> 
#include<conio.h> 
#include<math.h> 
int max(int a[], int, int); 
int min(int b[], int, int); 
void main() 



int i, n, a[50], big, small; 
printf("Enter how many numbers:\t");
scanf("%d", &n);
printf("\nEnter the number one by one:\n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
big=a[0];
small=a[n-1];
printf("\nThe maximum value is %d", max(a, n, big));
printf("\nThe minimum value is %d", min(a, n, small));
getch();
}

int max(int a[], int n, int big) 
{
if(n==0)
return big;
else
n--;
if(a[n]>big)
big=a[n];
return(max(a,n,big));
}
int min(int a[], int n, int small)
{
if(n==0)
return small;
else
n--;
if(a[n]<small)
small=a[n];
return min(a, n, small);
}

Output / Execution:

Enter how many numbers: 5 
Enter the numbers one by one 

14 
56 
34 
63 
45 

The maximum value is 63 
The minimum value is 14 



/* Matrix Multiplication */ 
/* Program Coded By Chetan Thapa Magar */ 


#include<stdio.h> 
#include<conio.h> 
#include<math.h> 
void main() 


int m, n, p, q, i, j, k, a[10][10], b[10][10], c[10][10];
printf("Enter the order of A matrix (m*n):\n");
scanf("%d%d", &m, &n);
printf("\nEnter the order of B matrix (p*q):\n");
scanf("%d%d", &p, &q);
if(n==p)
{
printf("\nMatrix can be multiplied.\n");
printf("\nEnter the value of matrix A:\n");
for(i=0; i<m; i++)
for(j=0; j<n; j++)
scanf("%d", &a[i][j]);
printf("\nEnter the value of matrix B:\n");
for(i=0; i<p; i++)
for(j=0; j<q; j++)
scanf("%d", &b[i][j]);
for(i=0; i<m; i++)
for(j=0; j<m; j++)
{
c[i][j]=0;
for(k=0; k<n; k++)
c[i][j]=c[i][j]+a[i][j]*b[k][j];
}
printf("\nThe resultant matrix is:\n");
for(i=0; i<m; i++)
{
for(j=0; j<q; j++)
printf("\t%d", c[i][j]);
printf("\n");
}
}
else
{
printf("\nMatrix multiplication not possible");
printf("\nColumn of A should be equal to row of B\n");
}
getch();
}

Output / Execution:
Enter the order of A matrix (m*n):
2

Enter the order of B matrix (p*q):
2

Matrix can be multiplied.
Enter the value of Matrix A:
1



Enter the value of Matrix B:
7




The resultant matrix is: 
10 18
50 42 


/* Addition and Subtraction of two matrix */ 
/* Program Coded By Chetan Thapa Magar */ 


#include<stdio.h> 
#include<conio.h> 
#include<math.h> 
void main() 
{
int m, n, i, j, a[10][10], b[10][10], add[10][10], sub[10][10];
printf("Enter the numbers for rows:\t");
scanf("%d", &m);
printf("\nEnter the number of columns:\t");
scanf("%d", &n);
printf("\nEnter the element of matrix A:\n");
for(i=0; i<m; i++)
for(j=0; j<n; j++)
scanf("%d", &a[i][j]);
printf("\nEnter the elements of matrix B:\n");
for(i=0; i<m; i++)
for(j=0; j<n; j++)
scanf("%d", &b[i][j]);
printf("\nThe Addition of Two Matrix is:\n");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
add[i][j]=a[i][j]+b[i][j];
printf("%d\t", add[i][j]);
}
printf("\n");
}
printf("\nThe Subtraction of Two Matrix is:\n");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
sub[i][j]=a[i][j]-b[i][j];
printf("%d\t", sub[i][j]);
}
printf("\n");
}
getch();
}
Output / Execution:
Enter the number of rows: 3
Enter the number of columns: 3 
Enter the elements of matrix A: 
1








Enter the elements of matrix B:
9








The Addition of Two Matrix is:
10 10 10
10 10 10 
10 10 10 

The Subtraction of Two Matrix is: 
-8 -6 -4
-2  0  2 
 4  6  8 

Post a comment

0 Comments