Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C Programs: 1. Write A C Program To Determine The Sum of Odd and Even Numbers

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 16

C Programs

1. write a c program to determine the sum of odd and even numbers.


# include<stdio.h>
# include<conio.h>
void main()
{
int n, i, seven=0, sodd=0;
int a[25];
clrscr();
rint!("#n enter the total num$er to $e entered"%);
scan!(&'d%,(n);
rint!(&#n enter the values%);
!or(i=0;i<n;i)))
{
i!(a[ i]'2==0)
seven=seven)a[i];
else
sodd=sodd)a[i];
*
rint!(&#n the sum o! even num$er is 'd%,seven);
rint!(&#n the sum o! odd num$er is 'd%,sodd);
+etch();
*
2. write a c program to count the number of positive, negative and zero
number in the given list of numbers.
# include <stdio.h>
# include <conio.h>
void main()
{
int n, i, nos=0, nne+=0, n,ero=0;
int a[25];
clrscr();
rint!("#n enter the total num$er to $e entered"%);
scan!(&'d%,(n);
rint!(&#n enter the values%);
- .reared /0
1r. 2eeendra 3asto+i, 4ecturer
2eartment o! 5omuter 6cience,718
C Programs
!or(i=0;i<n;i)))
{
i!(a[ i]>0)
nos=nos)-;
i!(a[i]<0)
nne+=nne+)-;
else
n,ero=n,ero)-;
*
rint!(&#n the num$er o! ositive value is 'd%,nos);
rint!(&#n the num$er o! ne+ative value is 'd%,nne+);
rint!(&#n the num$er o! ,eros is 'd%,n,ero);
+etch();
*
9. write a c program to find the given number using linear search method.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,a[90],sea,!la+;
clrscr();
rint!(&#n enter the num$er o! terms "%);
scan!(&'d%,(n);
rint!(&#n enter the values"%);
!or(i=0;i<n;i)))
scan!(&'d%,a[i]);
rint!(&#n enter the num$er to $e searched "%);
scan!(&'d%,(sea);
!or(i=0;i<n;i)))
{
i!(a[i] = = sea)
{
!la+=-;
$rea:;
2 .reared /0
1r. 2eeendra 3asto+i, 4ecturer
2eartment o! 5omuter 6cience,718
C Programs
*
else
!la+=0;
*
i!(!la+= = -)
rint!(&#n the +iven num$er 'd is resent in the osition num$er
'd%,sea,i);
else
rint!(&#n the +iven num$er is not resent%);
+etch();
*
4. write a c program to find the given number using binary search method.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,a[90],sea,!la+,;,0,t;
int lo<,hi+h,mid;
clrscr();
rint!(&#n enter the num$er o! terms "%);
scan!(&'d%,(n);
rint!(&#n enter the values"%);
!or(i=0;i<n;i)))
scan!(&'d%,a[i]);
!or(;=0;;<n=-;;)))
!or(0=;)-;0<n;0)))
{
i!(a[;]>a[0])
{
t=a[;];
a[;]=a[0];
a[0]=t;
9 .reared /0
1r. 2eeendra 3asto+i, 4ecturer
2eartment o! 5omuter 6cience,718
C Programs
*
*
rint!(&#n the sorted num$ers are "%);
!or(;=0;;<n;;)))
rint!(&'d#n%,a[;]);
rint!(&#n enter the num$er to $e searched "%);
scan!(&'d%,(sea);
lo<=0;
hi+h=n;
<hile(lo<<=hi+h)
{
mid=(lo<)hi+h)>2;
i!(t<a[mid])
hi+h=mid=-;
i!(t>a[mid])
lo<=mid)-;
i!(t= = a[mid])
{
rint!(&#n the num$er 'd is resent in the osition 'd%,t,mid);
!la+=0;
$rea:;
*
i!(mid = =- ? ? mid= = n)
$rea:;
*
i!(!la+)
rint!(&#n the +iven num$er is not resent%);
+etch();
*
5. program to perform the matrix additions
#include <stdio.h>
@ .reared /0
1r. 2eeendra 3asto+i, 4ecturer
2eartment o! 5omuter 6cience,718
C Programs
#include <conio.h>
void main()
{
int a[-0][-0],$[-0][-0],c[-0][-0],ro<,col,r,co,i,A,:;
clrscr();
rint!(&#t#t matri; addition#n%);
rint!(&enter ro< order o! matri; a " &);
scan!(&'d%,(ro<);
rint!(&enter column order o! matri; a " &);
scan!(&'d%,(col);
rint!(&enter ro< order o! matri; $ " &);
scan!(&'d%,(r);
rint!(&enter column order o! matri; $ " &);
scan!(&'d%,(co);
i! ((ro<B=r) ?? (col B= co) )
{
rint!(&matri; multilication is imossi$le#n%);
+etch();
*
else
{
rint!(&enter !irst matri; elements " &);
!or (i=0;i<ro<;i)))
!or (A=0;A<col;A)))
scan!(&'d%,(a[i][A]);
rint!(&#n!irst matri; is#n%);
!or (i=0;i<ro<;i)))
{
!or (A=0;A<col;A)))
{
rint!(&'t'd%,a[i][A]);
*
ritn!(&#n%);
*
rint!(&#nenter second matri; elements " &);
!or (i=0;i<r;i)))
!or (A=0;A<co;A)))
scan!(&'d%,($[i][A]);
5 .reared /0
1r. 2eeendra 3asto+i, 4ecturer
2eartment o! 5omuter 6cience,718
C Programs
rint!(&#nsecond matri; is#n%);
!or (i=0;i<r;i)))
{
!or (A=0;A<co;A)))
{
rint!(&'t'd%,$[i][A]);
*
ritn!(&#n%);
*
!or (i=0;i<ro<;i)))
!or (A=0;A<col;A)))
c[i][A] = a[i][A] ) $[i][A];
rint!(&#nthe resultant matri; is #n%);
!or (i=0;i<ro<;i)))
{
!or (A=0;A<col;A)))
{
rint!(&'t'd%,c[i][A]);
*
rint!(&#n%);
*
+etch();
*
C. program to perform the matrix subtraction
#include <stdio.h>
#include <conio.h>
void main()
{
int a[-0][-0],$[-0][-0],c[-0][-0],ro<,col,r,co,i,A,:;
clrscr();
rint!(&#t#t matri; addition#n%);
rint!(&enter ro< order o! matri; a " &);
scan!(&'d%,(ro<);
rint!(&enter column order o! matri; a " &);
C .reared /0
1r. 2eeendra 3asto+i, 4ecturer
2eartment o! 5omuter 6cience,718
C Programs
scan!(&'d%,(col);
rint!(&enter ro< order o! matri; $ " &);
scan!(&'d%,(r);
rint!(&enter column order o! matri; $ " &);
scan!(&'d%,(co);
i! ((ro<B=r) ?? (col B= co) )
{
rint!(&matri; multilication is imossi$le#n%);
+etch();
*
else
{
rint!(&enter !irst matri; elements " &);
!or (i=0;i<ro<;i)))
!or (A=0;A<col;A)))
scan!(&'d%,(a[i][A]);
rint!(&#n!irst matri; is#n%);
!or (i=0;i<ro<;i)))
{
!or (A=0;A<col;A)))
{
rint!(&'t'd%,a[i][A]);
*
ritn!(&#n%);
*
rint!(&#nenter second matri; elements " &);
!or (i=0;i<r;i)))
!or (A=0;A<co;A)))
scan!(&'d%,($[i][A]);
rint!(&#nsecond matri; is#n%);
!or (i=0;i<r;i)))
{
!or (A=0;A<co;A)))
{
rint!(&'t'd%,$[i][A]);
*
ritn!(&#n%);
D .reared /0
1r. 2eeendra 3asto+i, 4ecturer
2eartment o! 5omuter 6cience,718
C Programs
*
!or (i=0;i<ro<;i)))
!or (A=0;A<col;A)))
c[i][A] = a[i][A] = $[i][A];
rint!(&#nthe resultant matri; is #n%);
!or (i=0;i<ro<;i)))
{
!or (A=0;A<col;A)))
{
rint!(&'t'd%,c[i][A]);
*
rint!(&#n%);
*
+etch();
*
7. write a c program for swapping of two arrays
#include<stdio.h>
#include<conio.h>
void main()
{
int a[-0],$[-0],c[-0],i;
rint!(Eenter !irst arra0=>E);
!or(i=0;i<-0;i)))
scan!(E'dE,(a[i]);
rint!(E#nenter second arra0=>E);
!or(i=0;i<-0;i)))
scan!(E'dE,($[i]);
rint!(Earra0s $e!ore s<ain+E);
rint!(E#n!irst arra0=>E);
!or(i=0;i<-0;i)))
{
rint!(E'dE,a[i]);
*
rint!(E#nsecond arra0=>E);
!or(i=0;i<-0;i)))
{
rint!(E'dE,$[i]);
F .reared /0
1r. 2eeendra 3asto+i, 4ecturer
2eartment o! 5omuter 6cience,718
C Programs
*
!or(i=0;i<-0;i)))
{
>><rite an0 s<ain+ techniGue
c[i]=a[i];
a[i]=$[i];
$[i]=c[i];
*
rint!(E#narra0s a!ter s<ain+E);
rint!(E#n!irst arra0=>E);
!or(i=0;i<-0;i)))
{
rint!(E'dE,a[i]);
*
rint!(E#nsecond arra0=>E);
!or(i=0;i<-0;i)))
{
rint!(E'dE,$[i]);
*
+etch();
*
8. !rogram for matrix multiplication.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5][5],$[5][5],c[5][5],i,A,:,sum=0,m,n,o,;
rint!(E#nHnter the ro< and column o! !irst matri;E);
scan!(E'd 'dE,(m,(n);
rint!(E#nHnter the ro< and column o! second matri;E);
scan!(E'd 'dE,(o,();
i!(nB=o)
{
rint!(E1atri; mutilication is not ossi$leE);
rint!(E#n5olumn o! !irst matri; must $e same as ro< o! second matri;E);
*
Hlse
{
rint!(E#nHnter the Iirst matri;=>E);
J .reared /0
1r. 2eeendra 3asto+i, 4ecturer
2eartment o! 5omuter 6cience,718
C Programs
!or(i=0;i<m;i)))
!or(A=0;A<n;A)))
scan!(E'dE,(a[i][A]);

rint!(E#nHnter the 6econd matri;=>E);
!or(i=0;i<o;i)))
!or(A=0;A<;A)))
scan!(E'dE,($[i][A]);

rint!(E#n7he Iirst matri; is#nE);
!or(i=0;i<m;i)))
{
rint!(E#nE);
!or(A=0;A<n;A)))
{
rint!(E'd#tE,a[i][A]);
*
*

rint!(E#n7he 6econd matri; is#nE);
!or(i=0;i<o;i)))
{
rint!(E#nE);
!or(A=0;A<;A)))
{
rint!(E'd#tE,$[i][A]);
*
*

!or(i=0;i<m;i)))
{
!or(A=0;A<;A)))
{
c[i][A]=0;
!or(:=0;:<n;:)))
c[i][A]=c[i][A])a[i][:]K$[:][A];
*
*
*
-0 .reared /0
1r. 2eeendra 3asto+i, 4ecturer
2eartment o! 5omuter 6cience,718
C Programs
rint!(E#n7he multilication o! t<o matri; is#nE);
!or(i=0;i<m;i)))
{
rint!(E#nE);
!or(A=0;A<;A)))
{
rint!(E'd#tE,c[i][A]);
*
*
+etch();
*
J. Sum of diagonal elements of a matrix in c
#include<stdio.h>
#include<conio.h>
void main()
{
int a[-0][-0],i,A,sum=0,m,n;
rint!(E#nHnter the ro< and column o! matri;" E);
scan!(E'd 'dE,(m,(n);
rint!(E#nHnter the elements o! matri;"#n E);
!or(i=0;i<m;i)))
!or(A=0;A<n;A)))
scan!(E'dE,(a[i][A]);
rint!(E#n7he matri; is#nE);
!or(i=0;i<m;i)))
{
rint!(E#nE);
!or(A=0;A<m;A)))
{
rint!(E'd#tE,a[i][A]);
*
*
-- .reared /0
1r. 2eeendra 3asto+i, 4ecturer
2eartment o! 5omuter 6cience,718
C Programs
!or(i=0;i<m;i)))
{
!or(A=0;A<n;A)))
{
i!(i==A)
sum=sum)a[i][A];
*
*
rint!(E#n#n6um o! the dia+onal elements o! a matri; is" 'dE,sum);
+etch();
*
6amle outut"
Hnter the ro< and column o! matri;" 9 9
Hnter the elements o! matri;"
2
9
5
C
D
J
2
C
D
7he matri; is
2 9 5
C D J
2 C D
6um o! the dia+onal elements o! a matri; is" -C
-0. program to find transpose of given matrix
#include<stdio.h>
#include<conio.h>
void main()
{
int a[-0][-0],$[-0][-0],i,A,:=0,m,n;
rint!(E#nHnter the ro< and column o! matri;E);
scan!(E'd 'dE,(m,(n);
rint!(E#nHnter the Iirst matri;=>E);
-2 .reared /0
1r. 2eeendra 3asto+i, 4ecturer
2eartment o! 5omuter 6cience,718
C Programs
!or(i=0;i<m;i)))
!or(A=0;A<n;A)))
scan!(E'dE,(a[i][A]);
rint!(E#n7he matri; is#nE);
!or(i=0;i<m;i)))
{
rint!(E#nE);
!or(A=0;A<m;A)))
{
rint!(E'd#tE,a[i][A]);
*
*
!or(i=0;i<m;i)))
!or(A=0;A<n;A)))
$[i][A]=0;
!or(i=0;i<m;i)))
{
!or(A=0;A<n;A)))
{
$[i][A]=a[A][i];
*
*
rint!(E#n#n7rasose o! a matri; is => E);
!or(i=0;i<m;i)))
{
rint!(E#nE);
!or(A=0;A<m;A)))
{
rint!(E'd#tE,$[i][A]);
*
*
+etch();
*
--. code to print or display lower triangular matrix
#include<stdio.h>
#include<conio.h>
-9 .reared /0
1r. 2eeendra 3asto+i, 4ecturer
2eartment o! 5omuter 6cience,718
C Programs
void main()
{
int a[9][9],i,A;
!loat determinant=0;
rint!(EHnter the J elements o! matri;" E);
!or(i=0;i<9;i)))
!or(A=0;A<9;A)))
scan!(E'dE,(a[i][A]);
rint!(E#n7he matri; is#nE);
!or(i=0;i<9;i)))
{
rint!(E#nE);
!or(A=0;A<9;A)))
rint!(E'd#tE,a[i][A]);
*
rint!(E#n6ettin+ ,ero in uer trian+ular matri;#nE);
!or(i=0;i<9;i)))
{
rint!(E#nE);
!or(A=0;A<9;A)))
i!(i>=A)
rint!(E'd#tE,a[i][A]);
else
rint!(E'd#tE,0);
*
+etch();
*
6amle outut"
Hnter the J elements o! matri;"
-
2
9
@
5
C
D
-@ .reared /0
1r. 2eeendra 3asto+i, 4ecturer
2eartment o! 5omuter 6cience,718
C Programs
F
J
7he matri; is
- 2 9
@ 5 C
D F J
6ettin+ ,ero in uer trian+ular matri;
- 0 0
@ 5 0
D F J
12. !rogram for display upper triangle of "atrix.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[9][9],i,A;
!loat determinant=0;
rint!(EHnter the J elements o! matri;" E);
!or(i=0;i<9;i)))
!or(A=0;A<9;A)))
scan!(E'dE,(a[i][A]);
rint!(E#n7he matri; is#nE);
!or(i=0;i<9;i)))
{
rint!(E#nE);
!or(A=0;A<9;A)))
rint!(E'd#tE,a[i][A]);
*
rint!(E#n6ettin+ ,ero in uer trian+ular matri;#nE);
!or(i=0;i<9;i)))
{
-5 .reared /0
1r. 2eeendra 3asto+i, 4ecturer
2eartment o! 5omuter 6cience,718
C Programs
rint!(E#nE);
!or(A=0;A<9;A)))
i!(i<=A)
rint!(E'd#tE,a[i][A]);
else
rint!(E'd#tE,0);
*
+etch();
*
Hnter the J elements o! matri;"
-
2
9
@
5
C
D
F
J
7he matri; is
- 2 9
@ 5 C
D F J
6ettin+ ,ero in uer trian+ular matri;
- 2 9
0 5 C
0 0 J
-C .reared /0
1r. 2eeendra 3asto+i, 4ecturer
2eartment o! 5omuter 6cience,718

You might also like