WELCOME

Welcome to KoDes4U

Pages

Tuesday, March 15, 2011

distance conversion(basic C program)

/*The distance between two cities (in km) is entered through the keyboard. Write a program to convert and print this distance in meters, feet ,inches and centimeters. Code for Kodes4u.blogspot.com*/

#include
#include

void main()
{
float dis_km;
float dis_met,dis_feet,dis_inch,dis_cm;
clrscr();
printf("\nEnter a distance:");
scanf("%f",&dis_km);
dis_met=1000.00*dis_km;
printf("\nDistance in meter=%f",dis_met);
dis_feet=3280.839*dis_km;
printf("\nDistance in feet=%f",dis_feet);
dis_inch=39370.0787*dis_km;
printf("\nDistance in inches=%f",dis_inch);
dis_cm=100000.00*dis_km;
printf("\nDistance in cm=%f",dis_cm);
getch();
}

gross salary(basic C program)

/*Anil's basic salary is entered through the keyboard. His dearness allowance is 38% of basic salary, and house rent allowance is 20% of basic salary. Code a C program to find out his gross salary. coded for kodes4u.blogspot.com*/

#include
#include

void main()
{
float basic_salary,dearness_allow,house_rent,gross_salary;
clrscr();
printf("\Enter the basic salary of Anil:");
scanf("%f",&basic_salary);
dearness_allow=0.38*basic_salary;
house_rent=0.20*basic_salary;
gross_salary=basic_salary+dearness_allow+house_rent;
printf("\nAnil's gross salary is :%f",gross_salary);
getch();
}

Monday, March 14, 2011

prime number(c program)

/*Write a program to find whether a user entered number is prime or not. Coded for KoDes4U.blogspot.com*/

#include
#include

void main()
{
int num,i=2;
clrscr();
printf("\nEnter a number:");
scanf("%d",&num);
while(i<=num)
{
if(num%i==0)
{
printf("\nNot a prime number.");
break;
}
else
{
if(i==num/2)
printf("Prime number");
break;
}
i++;

}
getch();
}

Wednesday, March 9, 2011

templates(C++ program)

/*Write a C++ program using function template to read two matrices of
different data types such as integers and floating point values and perform
simple arithmetic operations on these matrices separately and display.
Coded for KoDes4.blogspot.com*/

#include

#include

#include

template

class CMatrix
{
int Row, Col;
T a[10][10];
public:
void Input();
void AddMat(CMatrix
mat1, CMatrix mat2);
int check(CMatrix
mat2);
void SubMat(CMatrix
mat1, CMatrix mat2);
void Display();
};
template

void CMatrix
::Input()
{
cout<<"\nEnter the order for the matrix "; cin>>Row>>Col;
cout<<"\n Enter the elements:"; for(int i=0; i
>a[i][j];
}
}
}
template

void CMatrix
::Display()
{
for(int i=0; i

void CMatrix
::AddMat(CMatrix mat1, CMatrix mat2)
{
Row = mat1.Row;
Col = mat1.Col;
for(int i=0; i

void CMatrix
::SubMat(CMatrix mat1, CMatrix mat2)
{
Row = mat1.Row;
Col = mat1.Col;
for(int i=0; i

int CMatrix
::check(CMatrix mat2)
{
int m,n,p,q;
m = Row;
n = Col;
p = mat2.Row;
q = mat2.Col;
if(m==p && n==q)
return 1;
else
return 0;
}
void main()
{
int ch,op,k,r;
clrscr();
do
{
cout<<"\n"; cout<<"\n Matrix operations using templates"; cout<<"\n1.Integer\n2.float\n3.exit"; cout<<"\n Choice..."; cin>>ch;
switch(ch)
{
case 1:
CMatrix
m1, m2, res;
cout<<"\n\n Integer Matrix:"; cout<<"\n\t1.ADD\n\t2.SUB\n\t3.Main Menu"; cout<<"\n Choice...."; cin>>op;
switch(op)
{
case 1:
m1.Input();
m2.Input();
k=m1.check(m2);
if(k==1)
{
res.AddMat(m1, m2);
cout<<"\n Addition of marix::"; res.Display(); } else cout<<"\n ROWS AND COLOUNMS OF MAT ARE NOT EUQAL"; break; case 2: m1.Input(); m2.Input(); k=m1.check(m2); if(k==1) { res.SubMat(m1, m2); cout<<"\n The subtraction of the two matrices is"; res.Display(); } else cout<<"\nROWS AND COLOUNMS OF MAT ARE NOT EUQAL"; break; case 3: break; } break; case 2: CMatrix
fm1, fm2, fres;
cout<<"\n\n Float Matrix:"; cout<<"\n\t1.ADD\n\t2.SUB\n\t3.Main Menu"; cout<<"\n Choice...."; cin>>r;
switch(r)
{
case 1:
fm1.Input();
fm2.Input();
k=fm1.check(fm2);
if(k==1)
{
fres.AddMat(fm1, fm2);
cout<<"\nThe addition of the two matrices is"; fres.Display(); } else cout<<"\nROWS AND COLOUNMS OF MAT ARE NOT EUQAL"; break; case 2: fm1.Input(); fm2.Input(); k=fm1.check(fm2); if(k==1) { fres.SubMat(fm1, fm2); cout<<"\nThe subtraction of the two matrices is"; fres.Display(); } else cout<<"\nROWS AND COLOUNMS OF MAT ARE NOT EUQAL"; break; case 3: break; } } } while(ch!=3); getch(); }

Inheritence(C++ program)

/*Design a base class with name , date of birth, blood group another base
class consisting of the data members such as height and weight. Design one
more base class conisting of the insurance policy number and contact address.
The derived class contains the data members telephone numbers and driving
lisence number. Coded for KoDes4U.blogspot.com*/


#include
#include

#include

class base1
{
public:
char name[50],date_of_birth[10],blood_group[3];
};
class base2
{
public:
int no;
char height[5],weight[5];
};
class base3
{
public:
char in_pol_no[10],cont_add[15];
};
class d:public base1,public base2,public base3
{
char ph_no[10],drvng_lic_no[10];
public:
void get_data()
{
cout<<"\nEnter number : "; cin>>no;
cout<<"\nEnter name : "; cin>>name;
cout<<"\nEnter date of birth : "; cin>>date_of_birth;
cout<<"\nEnter blood group : "; cin>>blood_group;
cout<<"\nenter height(in cms) : "; cin>>height;
cout<<"\nEnter weight(in kgs) : "; cin>>weight;
cout<<"\nEnter insurance policy no : "; cin>>in_pol_no;
cout<<"\nEnter contact address : "; cin>>cont_add;
cout<<"\nEnter phone number(please include STD code) : "; cin>>ph_no;
cout<<"\nEnter driving licence number : "; cin>>drvng_lic_no;
}
void disp_info()
{
cout<<"\nSerial no : "<
>ch;
switch(ch)
{
case 1:
cout<<"\nEnter no.of records : "; cin>>x;
for(i=0;i
>j;
for(i=0;i
>j;
for(i=0;i
obj[i].search(obj[i].no,j);
cout<<"\nResult : ";
obj[j-1].disp_info();
break;
}
}while(ch!=7);
file.close();
}

Static member functions, friend class, this pointer, inline code and dynamic memory allocation(C++ program)

/*Develop an object oriented program in C++ to create a database of the
personal information system containing the following information:
Name, Date of Birth, Blood group, Height, Weight, Insurance policy number,
Contact address, driving licence no. etc. Construct the database with
suitable member functions for initializing and destroying the data viz
constructor,default constructor, copy constructor ,destructor, static member
functions, friend clas, this pointer,inline code and dynamic memory allocation
operators-new and delete. coded for KoDes4U.blogspot.com*/


#include
#include
#include
class database
{
char name[50],date_of_birth[11],cont_add[50],tel_no[10];
public:
database() //constructor
{
strcpy(name,"blank_name");
strcpy(date_of_birth,"00/00/00");
strcpy(cont_add,"-----");
strcpy(tel_no,"999");
}
~database() //destructor
{
}
void get_info()
{
cout<<"Enter the name:"; cin>>name;
cout<<"\nEnter the Date of Birth:"; cin>>date_of_birth;
cout<<"\nEnter the contact address:"; cin>>cont_add;
cout<<"\nEnter the telephone no(please mention your STD code also):"; cin>>tel_no;
}
friend class extra;
friend void show(database a,extra b);
};
class extra
{
int height,weight;
char blood_group[3],in_pol_no[10],drvng_lic_no[10];
public:
extra()
{
height=180;
weight=60;
strcpy(blood_group,"---");
strcpy(in_pol_no,"---");
strcpy(drvng_lic_no,"----");
}
void get_extra_info()
{
cout<<"Enter the height(in cms):"; cin>>height;
cout<<"\nEnter the weight(in kg):"; cin>>weight;
cout<<"\nEnter the blood group:"; cin>>blood_group;
cout<<"\nEnter the insurance policy number:"; cin>>in_pol_no;
cout<<"\nEnter the driving license number(may be alphanumeric:"; cin>>drvng_lic_no;
}
~extra()
{
}
friend void show(database a,extra b);
};
void main()
{
int cnt,num;
clrscr();
cout<<"Enter the no of entries:"; cin>>num;
database *d[10];
extra *e[10];
for(cnt=0;cntget_info();
e[cnt]=new extra;
e[cnt]->get_extra_info();
}
for(cnt=0;cnt cout<<"\n"< }

Tuesday, March 8, 2011

operator overlaoding for string (C++ program)

/*Write a C++ program to perform String operations
1. = Equality
2. == String compare
3. +Concatenation
4. << To display a string
5. >> To reverse a string
6. Function to determine whether a string is pallindrome......................................coded for KoDes4U.blogspot.com*/

#include
#include
#include
class string
{
public:
char str1[20],str2[20];
void operator +(string s) //concatenation
{
strcat(s.str1,s.str2);
cout<<"\nConncatenated string is:"< }
int operator=(string s)
{
int i;
i=strcmp(s.str1,s.str2);
if(i==0)
cout<<"\nBoth are same:";
else if(i>0)
cout<<"\n1st string is greater";
else
cout<<"\n2nd string is greater";
return 0;
}
void operator==(string s)
{
strcpy(s.str1,s.str2);
cout<<"\noriginal string is:"< cout<<"\ncopied string is :"< }
void operator <<(string s)
{
cout<<"1st string is:"< cout<<"\n2nd Strring is:"< }
void operator >>(string s)
{
cout<<"Original string is:"< strrev(s.str1);
cout<<"\nReversed string is:"< cout<<"\nOriginal string is:"< strrev(s.str2);
cout<<"\nReversed string is:"< }
void pallindrome(string s);
};
void string::pallindrome(string s)
{
char c[20],d[20];
int result;
cout<<"First string is ";
strcpy(c,s.str1);
strrev(c);
result=strcmp(s.str1,c);
if(result==0)
cout<<"Pallindrome";
else
cout<<"not a pallindrome";
cout<<"\nSecond string is ";
strcpy(d,s.str2);
strrev(d);
result=strcmp(s.str2,d);
if(result==0)
cout<<"Pallindrome:";
else
cout<<"Not a paliindrome";
}
void main()
{
string s;
int op;
clrscr();
cout<<"Enter the two strings:";
cin>>s.str1>>s.str2;
do
{
cout<<"\n1)Concat\n2)compare\n3)copy\n4)reverse\n5)pallindrome\n6)Exit";
cout<<"\nEnter your choice:";
cin>>op;
switch(op)
{
case 1:s+(s);break;
case 2:s=(s);
break;
case 3:s==(s);break;
case 4:s>>(s);break;
case 5:s.pallindrome(s);break;
default:cout<<"Press any key to exit:";break;
}
}
while(op!=6);
getch();
}

Saturday, March 5, 2011

operator overlaoding (C++ program)

/*Design a class Complex with data members for real and imaginary part.
Providedefault parameterized constructors. Write a program to perform
arithmetic operations of two complex numbers using operator overloading
(using either member functions or friend functions).
Coded for KoDes4U.blogspot.com*/


#include
#include

class complex
{
int x1,y1,x2,y2,x,y,a;
float x3,y3;
public:
void operator +(complex *c)
{
cout<<"Original Complex:"<
=0)
cout<<"\n1)"<
x1<<"+"<y1<<"i"<x1<y1<<"i"<=0)
cout<<"\n2)"<
x2<<"+"<y2<<"i"<x2<y2<<"i"<x=c->x1+c->x2;
c->y=c->y1+c->y2;
cout<
x<<"+"<<"i"<y;
}
void operator -(complex *c)
{
cout<<"Original Complex:"<
=0)
cout<<"\n1)"<
x1<<"+"<y1<<"i"<x1<y1<<"i"<=0)
cout<<"\n2)"<
x2<<"+"<y2<<"i"<x2<y2<<"i"<x=c->x1-c->x2;
c->y=c->y1-c->y2;
cout<<"\nAfter subtraction:"; if(y>0)
cout<<"\nResulted Complex:"<
x<<"+"<y<<"i"; else if(y==0) { cout<<"n\Resulted Complex:"<x<<"+"<<"0i"; cout<<"\nMore specifically:"<x;
}
else if(y<0) cout<<"\nResulted Complex is:"<
x<y<<"i"; else if(x==0&&y==0) { cout<<"\nResulted Complex is:"<x<<"+"<<"0i"; cout<<"\nMore specifically:"<<"0"; } } void operator *(complex *c) { cout<<"Original Complex:"<=0)
cout<<"\n1)"<
x1<<"+"<y1<<"i"<x1<y1<<"i"<=0)
cout<<"\n2)"<
x2<<"+"<y2<<"i"<x2<y2<<"i"<x=(c->x1*c->x2);
c->y=(c->y1*c->y2);
cout<
x<<"+"<y<<"i"; } else { c->x=(c->x1*c->x2)-(c->y1*c->y2);
c->y=(c->x1*c->y2)+(c->y1*c->x2);
cout<
x<<"+"<y<<"i"; } } void operator =(complex *c) { cout<<"Entering of two complex nos:"; cout<<"\nEnter the 1st complex number, real part first and then imaginary part:"; cin>>c->x1>>c->y1;
cout<<"\nEnter the 2nd complex number, real part first and then imaginary part:"; cin>>c->x2>>c->y2;
}
void operator /(complex *c)
{

cout<<"Original Complex:"<
=0)
cout<<"\n1)"<
x1<<"+"<y1<<"i"<x1<y1<<"i"<=0)
cout<<"\n2)"<
x2<<"+"<y2<<"i"<x2<y2<<"i"<x2*c->x2)+(c->y2*c->y2);
c->x3=(float)(c->x1*c->x2)/a+(c->y1*c->y2)/a;
c->y3=(float)(c->y1*c->x2)/a-(c->y2*c->x1)/a;
cout<<"\nAfter division:\n"; if(c->y3>=0.0)
cout<
x3<<"+"<y3<<"i"<y3<0.0) cout<x3<y3<<"i"<>ch;
switch(ch)
{
case 1:c=(&c);
break;
case 2:c+(&c);
break;
case 3:c-(&c);
break;
case 4:c*(&c);
break;
case 5:c/(&c);
default:cout<<"Press any key to exit:";break;
}
}
while(ch!=6);
getch();
}

Friday, March 4, 2011

Constructor and Destructor(C++ Program)

/*Create a class named weather report that holds a daily weather report with
data members day_of_month,hightemp,lowtemp,amount_rain and amount_snow. The
constructor initializes the fields with default values: 99for day of month,
999 for hightemp, -999 for lowtemp,00 for amount_rain and amount_snow.
Include a function that prompts the user and sets the values for each field
so that you can override the default values. Write a program that creates a
monthly report.
Coded for KoDes4U.blogspot.com*/

#include

#include

class weather_report
{
public:
//char month[25];
int date,days,amount_rain,amount_snow;
float hightemp,lowtemp ;
weather_report() //constructor defining
{
days=99;
hightemp=999;
lowtemp=-999;
amount_rain=0;
amount_snow=0;
}
~weather_report() //destructor defining
{
}
void set_data();
void display_report();
};

void weather_report::set_data()
{
cout<<"\nEnter the day of month :"; cin>>date;
cout<<"\nEnter the high temp:"; cin>>hightemp;
cout<<"\nEnter the low temp:"; cin>>lowtemp;
cout<<"\nEnter the amount of rainfall:"; cin>>amount_rain;
cout<<"\nEnter the amount of snowfall:"; cin>>amount_snow;
}

void weather_report::display_report()
{
cout<<"\t"<
>month;
cout<<"\nEnter the number of days you want:"; cin>>days;
for(cnt=0;cnt
{
weather[cnt].set_data();
}
cout<<"\n\n**********WEATHER REPORT**********\n\n"; cout<<"MONTH\tDATE\tHIGHTEMP\tLOWTEMP\tRAIN\tSNOW\n"; cout<<"\n------------------------------------------------"; cout<<"\n\n"; for(cnt=0;cnt
{
cout<<"\n"<
weather[cnt].display_report();
}
getch();
}

Thursday, March 3, 2011

program to print ascii characters



/*Write a C program to print all the ASCII characters available in C
with an interval of 10 ASCII values at each enter hit.
coded by KoDes4U.blogspot.com*/


#include<stdio.h>
#include<conio.h>
#include<dos.h>
void main()
  {
    int i;
    clrscr();
    for(i=1;i<=255;i++)
      {
        if(i%10==0)
          {
            getch();
            printf("\n");
          }
        else
          printf("%d=%c\t",i,i);
          delay(1500);
      }
    getch();
  }

accepting numbers and storing it on an array

/*Write a C program to accept n numbers from user and store these numbers in
an array. Find the min and max numbers....................................
Coded by KoDes4U.blogspot.com*/






#include<stdio.h>
#include<conio.h>
void main()
{
int num[5],i,min,max;
clrscr();
printf("Accepting of 5 numbers from user:\n");
for(i=0;i<5;i++)
{
printf("Enter the number num[%d]:",i);
scanf("%d",&num[i]);
}
printf("\nDisplay of numbers that was stored in the array num[]\n");
for(i=0;i<5;i++)
{
printf("%d  ",num[i]);
}
max=num[0];
for(i=0;i<5;i++)
{
if(max<num[i])
max=num[i];
}
printf("\nMaximum number is %d",max);
min=num[0];
for(i=0;i<5;i++)
{
if(min>num[i])
min=num[i];
}
printf("\nMinimum number is %d",min);
getch();
}

Even Number printig Code(C program)

/*Write a program to find the even numbers from 1 to 100
Coded for KoDes4U.blogspot.com*/
#include
#include
int main()
{
int i;
clrscr();
printf("Even numbers from 1 to 20\n");
for(i=1;i<=20;i++)
{
if(i%2==0)
printf("%d\n",i);
}
getch();
return 0;
}

Use of math.h library functions(C program)

/*Write a program to find cosine of 0 to 180 degree.
Coded for KoDes4U.blogspot.com*/
#include
#include
#include
int main()
{
int degree;
float pi=3.14,x,y;
clrscr();
printf("Degree\tCosine\n");
for(degree=0;degree<=180;degree=degree+10)
{
x=(pi/180)*degree;
y=cos(x);
printf("%d\t%f",degree,y);
printf("\n");
}
getch();
return(0);
}

Quadratic Equation Code(C program)

/*Write a program to find a quadratic equation ax2+bx+c=0.

Coded for KoDes4U.blogspot.com*/

#include

#include

#include

void main()

{

int a,b,c,disc;

float alpha,beta;

clrscr();

printf("\nEnter the value of a,b and c:");

scanf("%d%d%d",&a,&b,&c);

disc=(b*b)-(4*a*c);

if(disc<0)

printf("\nRoots are imaginary.");

else

{

alpha=(-b+sqrt(disc))/(2*a);

beta=(-b-sqrt(disc))/(2*a);

printf("\n1st Root=%f\t2nd Root=%f",alpha,beta);

}

getch();

}

Sunday, February 27, 2011

decimal to binary conversion



/*Write a C program to convert given decimal into its euivalent binary.
Coded by KoDes4u.blogspot.com*/


#include<stdio.h>
#include<conio.h>
#define MAX 10
void convert(int dec_num,int bin_num[]);
void main()
{
 int dec_num,bin_num[MAX];
 clrscr();
 printf("\nEnter a decimal number:");
 scanf("%d",&dec_num);
 convert(dec_num,bin_num);
 getch();
}
void convert(int dec_num,int bin_num[])
{
int i=0,j;
 while(dec_num!=0)
 {
  bin_num[i]=dec_num%2;
  dec_num=dec_num/2;
  i++;
 }
 printf("\nThe Binary equivalent is:");
 for(j=i-1;j>=0;j--)
 printf("%d",bin_num[j]);
}

Friday, February 25, 2011

string operation without using library function(c programs)



/*Write a program on string operations without using library functions
   coded by KoDes4U.blogspot.com*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
int length(char str[]);
void copy(char str[]);
int compare(char str1[],char str2[]);
void reverse(char str[]);
int pallindrme(char str[]);
void concat(char str1[],char str2[]);
int search(char str1[],char str2[]);
void count(char str[]);


void main()
{
 char str1[50],str2[50];
 int res,op;
 clrscr();
  do
 {
  printf("\n-------------------------------MENU-----------------------------");
  printf("\n1)Length\n2)Copy\n3)Compare\n4)Reverse\n5)Pallindrome");
  printf("\n6)Concatenate\n7)Search\n8)Count\n9)Exit");
  printf("\nEnter your choice:");
  scanf("%d",&op);
  flushall();
  switch(op)
  {
   case 1:printf("\nEnter any string:");
 gets(str1);
 res=length(str1);
 printf("\nLength of the string is: %d",res);break;
   case 2:printf("\nEnter any string:");
 gets(str1);
 copy(str1);
 break;
   case 3:printf("\nEnter 1st string:");
 gets(str1);
 printf("\nEnter 2nd string:");
 gets(str2);
 res=compare(str1,str2);
 if(res>0)
 printf("\n1st string is greater.");
 else if(res<0)
 printf("\n2nd string is greater.");
 else
 printf("\nBoth are same.");
 break;
   case 4:printf("\nEntre any string:");
 gets(str2);
 reverse(str1);
 break;
   case 5:printf("\nEnter any string:");
 gets(str1);
 res=pallindrome(str1);
 if(res==0)
 printf("\nPallindrome.");
 else
 printf("\nNot a pallindrome.");
 break;
   case 6:printf("\nEnter main string:");
 gets(str1);
 printf("\nEnter substring:");
 gets(str2);
 concat(str1,str2);break;
   case 7:printf("\nEnter the main string:");
 gets(str1);
 printf("\nEnter substrring:");
 gets(str2);
 res=search(str1,str2);
 printf("\nFound at location:%d",res);
 break;
   case 8:count(str1);
 break;
   case 9:break;
  }
 }              while(op!=9);
}


int length(char str[])
{
 int i=0;
 while(str[i]!='\0')
 i++;
 return(i);
}
void copy(char str[])
{
 char a[50];
 int i=0;
  printf("\nOriginal string is.%s",str);
 while(str[i]!='\0')
 {
 a[i]=str[i];
 i++;
 }
 printf("\nCopied string is:%s",a);
}
int compare(char str1[],char str2[])
{
 int i=0;
 while(str1[i]!='\0')
 {
  if(str1[i]>str2[i])
  return(1);
  if(str1[i]<str2[i])
  return(-1);
  i++;
 }
 return(0);
}


void reverse(char str[])
{
 int i=0,j=0;
 char temp[50];
 while(str[i]!='\0')
 i++;
 i--;
// n=i;
 //i=0;
 printf("\nOriginal string is:%s",str);
 //i=0;
 while(i>=0)
 {
  temp[j]=str[i];
  i--;
  j++;
 }
  printf("\nReversed string is:%s",temp);
}


void concat(char str1[],char str2[])
{
 int i=0,j=0;
 while(str1[i]!='\0')
 i++;
 printf("\nBefore concatenation:");
 printf("\n1st string is:%s",str1);
 printf("\n2nd string is:%s",str2);
 while(str2[j]!='\0')
 {
  str1[i]=str2[j];
  i++;j++;
 }
 printf("\nConcatenated string is:%s",str1);
}


int pallindrome(char str[])
{
 int i=0,j=0;
 while(str[j]!='\0')
 j++;
 j--;
 while(j>i)
 {
  if(str[i]!=str[j])
  return(1);
  i++;
  j--;
 }
  return(0);
}
int search(char str1[],char str2[])
{
 int i,j,lstr1,lstr2;
 lstr1=length(str1);
 lstr2=length(str2);
 for(i=0;i<=lstr1-lstr2+1;i++)
  for(j=0;str1[i+j]==str2[j]&&str2[j]!='\0';j++)
   if(str2[j]=='\0')
   return(i+1);
   return(0);
}
void count(char str[])
{
 int spcnt=0,chcnt=0,i;
 for(i=0;str[i]!='\0';i++)
 {
  if(str[i]==' ')
  spcnt++;
  chcnt++;
 }
 printf("\nNo. of spaces=%d",spcnt);
 printf("\nNo. of words=%d",spcnt+1);
 printf("\nNo. of characters=%s",chcnt);
}

matrix operation using array(c program)



/*write a program on matrix operation using array
 coded by KoDes4U.com*/




// list of include files
#include<stdio.h>
#include<conio.h>
#define SIZE 10


//Function definition for reading the matrix


void ReadMatrix(int mat[SIZE][SIZE],int *row,int *col)
{
 int i,j;
  for(i=0;i<*row;i++)
for(j=0;j<*col;j++)
 {
printf("\nEnter the element no:[%d][%]:",i,j);
  scanf("%d",&mat[i][j]);
 }
}
//Function definition for printing the matrix


void PrintMatrix(int mat[SIZE][SIZE],int row,int col)
{
 int i,j;
   for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
  printf("  %d",mat[i][j]);
  printf("\n\a");
}
}


//Function definition for addition of matrix


int AddMatrix(int mat1[SIZE][SIZE],int mat2[SIZE][SIZE],int mat3[SIZE][SIZE],int row1,int row2,int col1,int col2,int *row3,int *col3)
{
  int i,j;
  if(row1==row2&&col1==col2)   //size should be same
   {
for(i=0;i<row1;i++)
for(j=0;j<col1;j++)
 mat3[i][j]=mat1[i][j]+mat2[i][j];
*row3=row1;
  *col3=col1;
return(1);
   }
else
 {
printf("\nAddition is no possible");
return(-1);
 }
}


//Function of multiplication


int MulMatrix(int mat1[SIZE][SIZE],int mat2[SIZE][SIZE],int mat3[SIZE][SIZE],int row1,int row2,int col1,int col2,int *row3,int *col3)
{
 int i,j,k;
  if(col1==row2)
{
  for(i=0;i<row1;i++)
for(j=0;j<col2;j++)
{
  mat3[i][j]=0;
  for(k=0;k<col1;k++)
mat3[i][j]=mat3[i][i]+mat1[i][k]*mat2[k][j];
}
*row3=row1;
*col3=col2;
return(1);
}
  else
{
 printf("\nMultiplication is not possible:");
 return(-1);
}
}


//Function definition for transpose of the matrix
void Transpose(int mat1[SIZE][SIZE],int row1,int col1,int mat3[SIZE][SIZE],int *row3,int *col3)
{
  int i,j;
   for(i=0;i<col1;i++)
{
 for(j=0;j<row1;j++)
  {
mat3[i][j]=mat1[j][i];
  }
}
*row3=col1;
*col3=row1;
}


//Main Function definition
 void main()
 {
  int mat1[SIZE][SIZE],mat2[SIZE][SIZE],mat3[SIZE][SIZE];
int row1,row2,row3,col1,col2,col3;
  int choice,val;
clrscr();
  printf("\nEnter the rows and columns of 1st matrix:");
scanf("%d %d",&row1,&col1);
printf("\nRead matrix 1");
ReadMatrix(mat1,&row1,&col1);
printf("\nPrint matrix 1");
  PrintMatrix(mat1,row1,col1);
 printf("\nEnter the rows and columns of 2nd matrix:");
   scanf("%d %d",&row2,&col2);
 printf("\nRead matrix 2:");
ReadMatrix(mat2,&row2,&col2);
printf("\nprint matrix 2:");
PrintMatrix(mat2,row2,col2);
do
 {
  printf("\n1)ADD\n2)Multiplication\n3)Transpose\n4)Exit");
  printf("\nEnter your choice:");
  scanf("%d",&choice);
switch(choice)
{
 case 1:val=AddMatrix(mat1,mat2,mat3,row1,row2,col1,col2,&row3,&col3);
if(val!=-1)
 {
printf("Addition matrix is:");
PrintMatrix(mat3,row3,col3);
 }
break;
 case 2:val=MulMatrix(mat1,mat2,mat3,row1,row2,col1,col2,&row3,&col3);
  if(val!=-1)
  {
printf("\nMultiplication of matrices is:");
PrintMatrix(mat3,row3,col3);
  }
break;
 case 3:Transpose(mat1,row1,col1,mat3,&row3,&col3);
printf("\ntranspose matrix is:");
 PrintMatrix(mat3,row3,col3);
 break;
 case 4:break;
}


 }
while(choice!=4);
 }