Saturday 20 October 2012

C++ programs for Computer Science XII th (BAMU)



C++ programs for Computer Science XII th (BAMU)
rohini.shinde.obad@gmail.com
By-Miss Rohini Shinde
Lecturer at Shri Bhosale Jr College,Osmanabad

To my dear Students....ALL THE BEST

//program to find factorial of given number

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i,f=1;
cout<<"Enter the number"<<endl;
cin>>n;
for(i=1;i<=n;i++)
f=f*i;
cout<<"Factorial  of given no is="<<f;
getch();
}
/* OUTPUT:
Enter the number:5
Factorial of given no is:120
*/






//Program for sum of digits

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
long int num,x,sum=0;
cout<<"Enter a number:\n ";
cin>>num;
while(num>0)
{
  x=num%10;
  sum=sum+x;
  num=num/10;
}
cout<<"Sum of digits of a number = "<<sum;
getch();
}
/*OUTPUT:
Enter a number:123
Sum of digits of a number:6
*/


//program to find area of triangle

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
float a,b,c,p,ar;
cout<<"Enter 3 sides :"<<endl;
cin>>a>>b>>c;
p=a+b+c;
ar=sqrt(p*(p-a)*(p-b)*(p-c));
cout<<"Area :"<<ar;
getch();
}
/*
OUTPUT:
Enter 3 sides: 5 7 9
Area:237.58
*/




//prg to find sum of even no <100

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,s=0;
for(i=2;i<=100;i=i+2)
s=s+i;
cout<<"Sum ="<<s;
getch();
}

/*
OUTPUT:
Sum=2550
*/







//program to display Fibonacci series

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,x=0,y=1,s;
cout<<x<<endl<<y<<endl;
for(i=3;i<=10;i++)
{
s=x+y;
cout<<s<<endl;
x=y;
y=s;
}
getch();
}
/*
OUTPUT:
0 1 1 2 3 5 8 13 21 34
*/


//prg to find sum of odd no <100

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,s=0;
for(i=1;i<=100;i=i+2)
s=s+i;
cout<<"Sum ="<<s;
getch();
}

/*
OUTPUT:
Sum=2550
*/







//Program of given no is prime or not
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int p=0,i,n;
cout<<"Enter the no :"<<endl;
cin>>n;
for(i=2;i<=(n/2);i++)
{
if(n%i==0)
p=1;
}
if(p==0)
cout<<"Prime";
else if (p==1)
cout<<"Not Prime";
getch();
}
/*
OUTPUT:
Enter the number:17
Prime
*/
//Program to find GCM

#include <iostream.h>
#include<conio.h>

void main()
{
int a;
int b;
int temp;
cout << "Enter the 1st integer : ";
cin >> a;
cout << "Enter the 2nd integer : ";
cin >> b;
while( b!= 0) {
temp = a % b;
a = b;
b = temp;
}
cout << a << endl;
getch();
}

OUTPUT:
ENTER 1st NO : 20
ENTER 2nd NO. :40
GCD :20











//Program to Ratio & reciprocal
#include<iostream.h>
#include<conio.h>
class ratio
{
public:
void assign(int,int);
double convert();
void invert();
void print();
private:
            int num,den;
};
void ratio::assign(int n,int d)
{num=n;
den=d;
}
double ratio::convert()
{
return double((num)/(den));
}
void ratio::invert()
{
int temp=num;
num=den;
den=temp;
}
void ratio::print()
{
cout<<num<<"/"den;
}
void main()
{
ratio x;
x.assign(22,7);
cout<<"x=";
x.print();
cout<<"="<<x.convert()<<endl;
x.invert();
cout<<"1/x";
x.print();
cout<<endl;
}
/*
OUTPUT:
X=22/7=3
1/x=7/22
*/


//Program to Swap th given numbers using Call by reference
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x,y;
void swap(int &x,int &y);//swap fun declaration
cout<<"ENter No to be swap"<<endl;
cin>>x>>y;
cout<<"\nNumber Before swap"<<endl;
cout<<"\nx="<<x<<"\ny="<<y<<endl;
swap(x,y);//function call
cout<<"\nx="<<x<<"\ny="<<y<<endl;
getch();
}
void swap(int &x,int &y)
{
int temp;
temp=x;
x=y;
y=temp;
cout<<"\nNumber Before swap"<<endl;
cout<<"\nx="<<x<<"\ny="<<y<<endl;
}
/* OUTPUT:
Enter No to be swap: 11 12
Number before swap: 11 12
Number after swap: 12 11
*/


//Program to Swap th given numbers using Call by reference
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x,y;
void swap(int,int);//swap fun declaration
cout<<"ENter No to be swap"<<endl;
cin>>x>>y;
cout<<"\nNumber Before swap"<<endl;
cout<<"\nx="<<x<<"\ny="<<y<<endl;
swap(x,y);//function call
cout<<"\nx="<<x<<"\ny="<<y<<endl;
getch();
}
void swap(int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;
cout<<"\nNumber Before swap"<<endl;
cout<<"\nx="<<x<<"\ny="<<y<<endl;
}
/* OUTPUT:
Enter No to be swap: 11 12
Number before swap: 11 12
Number after swap: 12 11
*/













//Program to sort real integer no in ascending order by using bubble sort method
#include <iostream.h>
#include<conio.h>
void bubbleSort(int *array,int length)//Bubble sort function
{
    int i,j;
    for(i=0;i<10;i++)
    {
        for(j=0;j<i;j++)
        {
                if(array[i]<array[j])
            {
                int temp=array[i]; //swap
                array[i]=array[j];
                array[j]=temp;
            }
        }
    }
}
void printElements(int *array,int length) //print array elements
{
    int i=0;
    for(i=0;i<10;i++)
    cout<<array[i]<<endl;
}
void main()
{
    clrscr();
    int a[]={10,1,9,2,8,3,7,4,6,5};   // array to sort
    bubbleSort(a,10);                 //call to bubble sort
    printElements(a,10);               // print elements
    getch();
}
/*
OUTPUT:
1 2 3 4 5 6 7 8 9 10
*/

//Program to sort real integer no in descending order by using bubble sort method
#include <iostream.h>
#include<conio.h>
void bubbleSort(int *array,int length)//Bubble sort function
{
    int i,j;
    for(i=0;i<10;i++)
    {
        for(j=0;j<i;j++)
        {
                if(array[i]>array[j])
            {
                int temp=array[i]; //swap
                array[i]=array[j];
                array[j]=temp;
            }
        }
    }
}
void printElements(int *array,int length) //print array elements
{
    int i=0;
    for(i=0;i<10;i++)
    cout<<array[i]<<endl;
}
void main()
{
    clrscr();
    int a[]={10,1,9,2,8,3,7,4,6,5};   // array to sort
    bubbleSort(a,10);                 //call to bubble sort
    printElements(a,10);               // print elements
    getch();
}
/*
OUTPUT:
10 9 8 7 6 5 4 3 2 1
*/
//Program to implement a circle class, storing radius & x, y coordinates of center in float,
Calculate area & circumference

#include<iostream.h>
#include<conio.h>
class circle
{
private:
float x,y,r;
float area1,circum;

public:
void assign();
void area();
void circumf();
void print();
};

void circle::assign()
{
cout<<"type the x & yco-ordinater of center\n";
cin>>x>>y;
cout<<"type the radius";
cin>>r;
}

void circle::area()
{
area1=3.14*r*r;
}

void circle::circumf()
{
circum=2*3.14*r;
}

void circle::print()
{
cout<<"x,y co-ordinates="<<x<<"\t"<<y<<endl;
cout<<"Radius is="<<r<<endl;
cout<<"Area is"<<area1<<endl;
cout<<"Circumference is"<<circum<<endl;
}

void main()
{
clrscr();
circle c1;
c1.assign();
c1.area();
c1.circumf();
c1.print();
getch();
}

/*OUTPUT:
Type the x & y coordinates of center
2
1
Type the radius:2
X,y coordinates=2 1
The radius is 2
area is 12.56
circumference is 12.56
*/










//program of binary search technique

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
clrscr();
float a[10],p;
int i,top,bot,mid;
cout<<"Type the no in ascending order"<<"\n";
for(i=0;i<10;i++)
{
cin>>a[i];
}
top=0;
bot=9;
cout<<"type the no u want to search \n";
cin>>p;
mid=(top+bot)/2;
while((top<=bot)&&(a[mid]!=p))
{
            if(p<a[mid])
            bot=mid-1;
            else
            top=mid+1;
            mid=(top+bot)/2;
}
            if(a[mid]==p)
            {
            cout<<"The no is at position"<<(mid+1)<<"\n";
            }
            else
            cout<<"the no is not found\n";
getch();
}
/*OUTPUT:
Type the number in ascending order
1 2 3 4 5 6 7 8 9 10
Type the no you want to search
7
The number is at 7th position
*/







//program to addition of complex no

#include<iostream.h>
#include<conio.h>
class complex
{
float x,y;
public:
complex(){};
complex(float real,float img)
{
x=real;
y=img;
}
complex operator+(complex c);
void display(void);
};

complex complex::operator+(complex c)
{
complex t;
t.x=x+c.x;t.y=y+c.y;
return(t);
};

void complex::display(void)
{
cout<<x<<"+i"<<y<<"\n";
}
void main()
{
clrscr();
complex c1,c2,c3;
c1=complex(2.5,3.5);
c2=complex(1.6,2.7);
c3=c1+c2;
cout<<"c1";
c1.display();
cout<<"c2";
c2.display();
cout<<"c3";
c3.display();
getch();
}

/*OUTPUT
C1=2.5+i3.5
C2=1.6+i2.7
C3=4.1+i6.2
*/
//program of name of country by using file handeling
#include  <iostream.h>
#include  <conio.h>
#include<fstream.h>
main()
{
clrscr();
ofstream fout;
fout.open("country");
fout<<"India"<<endl;
fout<<"USA"<<endl;
fout<<"South Afrika"<<endl;
fout<<"China"<<endl;
fout<<"Shrilanka"<<endl;
fout.close();
const n=80;
char line[80];
ifstream fin;
fin.open("country");
cout<<"Name of country"<<endl;
while(fin)
{
fin.getline(line,n);
cout<<line<<endl;
}
fin.close();
fin.open("capital");
cout<<"Name of capital "<<endl;
while(fin)
{
fin.getline(line,n);
cout<<line<<endl;
}
fin.close();
getch();
return 0;
}













//program of node

#include<iostream.h>
#include<conio.h>
class node
{
public:node(int d,node*p=0):data(d),next(p){}
int data;
node*next;
};
void main()
{
clrscr();
int n;
node*p;
node*q;
cout<<"\n create the linked list"<<endl;
while(cin>>n)
{
p=new node(n,q);
q=p;
}
cout<<"\n The traversed linked list in reverse order"<<endl;
for(;p->next;p=p->next)
{
cout<<p->data<<"->";
}
cout<<"*\n";
getch();
}

//program to display object is born
#include<iostream.h>
#include<conio.h>
class ratio
{
public:
ratio()
{
cout<<"Object is born"<<endl;
}
~ratio()
{
cout<<"Object dies"<<endl;
}
private:
int num,den;
};

void main()
{
clrscr();
ratio x;
cout<<"now x is alive \n";
cout<<"at the end of program\n";
getch();
}

/*OUTPUT:
Object is born
Now x is alive
At end of program
*/

No comments:

Post a Comment