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();

}