Sunday, 17 November 2013

C++ program to find last prime number that occurs before enter number

#include<iostream>
int main()
{
 int num,count=0;
 cout<<"Enter number to find last prime number occurs before it: ";
 cin>>num;
  for( int a=num-1;a>=1;a--)
  {
   for(int b=2;b<a;b++)
      {
       if(a%b==0)
       count++;
       }
       if(count==0)
       {
      if(a==1)
       {
       cout<<"no prime number less than 2";
       break;
     }
      cout<<a<<" is the last prime number before entered number";
       break;
       }
       count=0;
}
return 0;
}

C++ code to swap two variables without using third variable

#include<iostream>


int main()

{

int var1, var2;

 cout<<"Enter value for first integer:  ";

 cin>>var1;

 cout<<"Enter value for second integer:  ";

 cin>>var2;

 cout<<" Values Before swapping:  "<<endl;

 cout<<"First Integer ="<<var1<<endl;

 cout<<"Second Interger ="<<var2<<endl;

              var1=var1+var2;

              var2=var1-var2;

              var1=var1-var2;

 cout<<" Values After swapping:  "<<endl;

 cout<<"First Integer ="<<var1<<endl;

 cout<<"Second Interger ="<<var2<<endl;

    return 0;


}

C++ program code to swap the values of two integer:

#include<iostream>

using namespace std;

int main()

{

int var1, var2, swap;

 cout<<"Enter value for first integer:  ";

 cin>>var1;

 cout<<"Enter value for second integer:  ";

 cin>>var2;

 cout<<" Values Before swapping:  "<<endl;

 cout<<"First Integer ="<<var1<<endl;

 cout<<"Second Interger ="<<var2<<endl;

              swap=var1;

              var1=var2;

              var2=swap;

 cout<<" Values After swapping:  "<<endl;

 cout<<"First Integer ="<<var1<<endl;

 cout<<"Second Interger ="<<var2<<endl;

 return 0;

}

C++ program to find the Area and Perimeter of a Rectangle:

C++ program to find the Area and Perimeter of a Rectangle:


#include<iostream>


int main()

{

    int width,height,area,perimeter;

    cout<<"Enter  Width of Rectangle = ";

    cin>>width;

    cout<<"Enter  Height of Rectangle = ";

    cin>>height;

    area=height*width;

    cout<<"Area of Rectangle ="<<area<<endl;

    perimeter=2*(height+width);

cout<<" Perimeter of rectangle are = "<<perimeter<<endl;

return 0;

}

C++ Program to find the area of any triangle using Heron's Formula



#include<iostream>

#include<math.h>

using namespace std;

int main()

{

    float first,second,third;

    float s,area;

    cout<<"Enter size of each sides of triangle"<<endl;

    cout<<"Enter size for First Side =";

    cin>>first;

    cout<<"Enter size for Second Side =";

    cin>>second;

    cout<<"Enter size for Third Side =";

    cin>>third;

    s = (first+second+third)/2;

    area = sqrt(s*(s-first)*(s-second)*(s-third));

    cout<<"Area of Triangle= "<<area<<endl;

    return 0;

}

C++ Program to Find the Binary Value of Decimal Number Using for loop

#include<iostream.h>
#include <stdio.h>
int main()
{
  int n,x,a, c, k;

  cout<<"Enter an integer in decimal number system";
  cin>>x;
  n=x;
  cout<<"Binary Value OF Given Number Is: ";

 for( a=1;n!=0;a++)

  {
     n=n/2;

  }

a=a-2;
  for (c = a; c >= 0; c--)
  {
    k = x >> c;

    if (k & 1)
      cout<<"1";
    else
      cout<<"0";
  }
 return 0;
}

Program to find factorial of Number in C++

#include<iostream>
int main()
{

    int num,factorial=1;

    cout<<" Enter Number To Find Its Factorial:  ";

    cin>>num;

    for(int a=1;a<=num;a++)

    {

        factorial=factorial*a;

    }

cout<<"Factorial of Given Number is ="<<factorial<<endl;

    return 0;
}

TO FIND THE PERFECT NUMBER IN C++


#include<iostream.h>
#include<conio.h>
void main()                 //Start of main
{
  clrscr();
   int i=1, u=1, sum=0;
   while(i<=500)
 {                                  // start of first loop.

   while(u<=500)
   {                               //start of second loop.
     if(u<i)
     {
      if(i%u==0 )
      sum=sum+u;
     }                          //End of if statement
    
     u++;
   }                           //End of second loop

   if(sum==i)
   {
    cout<<i<<" is a perfect number."<<"\n";
   }

   i++;
   u=1;  sum=0;
 }                             //End of First loop
   getch();
 }                            //End of main

C++ Program To Find Prime Numbers



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

void main()

{

clrscr();

int number,count=0;

cout<<"ENTER NUMBER TO CHECK IT IS PRIME OR NOT ";

cin>>number;

for(int a=1;a<=number;a++)

{

if(number%a==0)

{
count++;
}

if(count==2)
{
cout<<" PRIME NUMBER \n";

}

else

{

cout<<" NOT A PRIME NUMBER \n";

}
getch();
}

Friday, 15 November 2013

6. Write a C++ program to create a class called STUDENT with data members USN,Name and Age. Using inheritance, create the classes UGSTUDENT and PGSTUDENT having fields as Semester,Fees and Stipend. Enter the data for at least 5 students. Find the semester wise average age for all UG and PG students separately.

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

 public:  int reg,age; 
            char name[20]; 
            void read_data(); 
}; 

class ugstudent:public student 

 public:  int stipend,sem; 
            float fees; 
           void read_data(); 
}; 


class pgstudent:public student 

 public:  int stipend,sem; 
            float fees; 
           void read_data(); 
}; 

/* function to read student details*/ 
void student::read_data() 

 cout<<"\n Enter name:"; 
 cin>>name; 
 cout<<"\n Enter Reg.no."; 
 cin>>reg; 
 cout<<"\n Enter age:"; 
 cin>>age;  
 }
void ugstudent::read_data() 

 student::read_data(); 
 cout<<"\nEnter the sem:"; 
 cin>>sem; 
 cout<<"\nEnter the fees:"; 
 cin>>fees; 
 cout<<"\nEnter the stipend:"; 
 cin>>stipend; 


/* function to read additional details for pgstudents*/ 
void pgstudent::read_data() 

 student::read_data(); 
 cout<<"\nEnter the sem:"; 
 cin>>sem; 
 cout<<"\nEnter the fees:"; 
 cin>>fees; 
 cout<<"\nEnter the stipend:"; 
 cin>>stipend; 


/* main function */ 
void main() 

 ugstudent ug[20]; 
 pgstudent pg[20]; 
 int i,n,m; 
 float average; clrscr(); 
 cout<<"\nEnter the no. of entries in the ugstudent class:"; 
 cin>>n; 
 for(i=1;i<=n;i++) 
 ug[i].read_data(); 
 for(int sem=1;sem<=8;sem++) 
 { 
 float sum=0; 
 int found=0,count=0; 
 for(i=1;i<=n;i++) 
 { 
 if(ug[i].sem==sem) 
 { 
 sum=sum+ug[i].age; 
 found=1;
   count++; 
 } 
 } 
 if(found==1) 
 { 
 average=sum/count; 
 cout<<"\nAverage of age of sem "<<sem<<" is "<<average; 

 } 
 } 
 cout<<"\nEnter the no. of entries of pgstudent class:"; 
 cin>>n; 
 for(i=1;i<=n;i++) 
 pg[i].read_data(); 
 for(sem=1;sem<=8;sem++) 
 { 
 float sum=0; 
 int found=0,count=0; 
 for(i=1;i<=n;i++) 
 { 
 if(pg[i].sem==sem) 
 { 
 sum=sum+pg[i].age; 
 found=1; 
 count++; 
 } 
 } 
 if(found==1) 
 { 
 average=sum/count; 
 cout<<"\nAverage of age of sem "<<sem<<" is "<<average; 
 } 
 } 
 getch(); 


OUTPUT 

Enter the no. of entries in the ugstudent class:3 
Enter name:Anagha 
Enter Reg.no.3 
Enter age:19 
Enter the sem:4 
Enter the fees:12000 
Enter the stipend:3489 
Enter name:Madhulika 
Enter Reg.no.17 
Enter age:20 
Enter the sem:4 
Enter the fees:15000 
Enter the stipend:3245 
Enter name:Ankitha 
Enter Reg.no.4 
Enter age:21 
Enter the sem:4 
Enter the fees:20000 
Enter the stipend:34278 
Average of age of sem 4 is 20 
Enter the no. of entries of pgstudent class:4 
Enter name:SnowWhite 
Enter Reg.no.6 
Enter age:19 
Enter the sem:4 
Enter the fees:78346 
Enter the stipend:478 
Enter name:Cindy 
Enter Reg.no.7 
Enter age:20 
Enter the sem:4 
Enter the fees:76479 
Enter the stipend:8734 
Enter name:Della 
Enter Reg.no.8 
Enter age:21 
Enter the sem:5 
Enter the fees:78578 
Enter the stipend:478 
Enter name:Stella 
Enter Reg.no.9 
Enter age:22 
Enter the sem:5 
Enter the fees:63278
Enter the stipend:748 
Average of age of sem 4 is 19.5 
Average of age of sem 5 is 21.5        

5. Write a C++ program to create a class called DATE. Accept two valid dates in the form dd/mm/yy. Implement the following operations by overloading the + and - operators. After every operation display the results by overloading the operator <<. i)no_of_days=d1-d2; where d1 and d2 are date objects, d1>=d2 and no_of_days is an integer. ii)d2=d1+no_of_days; where d1 is a DATE object and no_of_days is an integer

#include<iostream.h>
#include<process.h>  
class date
{

 private:     int mm,dd,yy;

 public:       void getdate();
                    int operator -(date);
                   date operator +(int);
};

void date::getdate()
{
     cout<<"enter a valid date(dd mm yy)\n";
     START
     cin>>dd>>mm>>yy;
     if((mm==2) && (dd>29))
{

 cout<<"wrong input!!!\n";
 cout<<"\n enter the date again....\n";
 goto START;

 }

 if((mm>12)||(dd>31))
 {

 cout<<"wrong input!!!\n";
 cout<<"\n enter the date again....\n";
 goto START;

}

 if((mm==4||mm==6||mm==9||mm==11) && (dd>30))
 {

 cout<<"wrong input!!!\n";
 cout<<"\nenter the date again....\n";
 goto START;

 }

 if((yy%4)!=0 && (mm==2) && (dd>28))
 {

 cout<<"wrong input!!!\n";
 cout<<"\nenter the date again....\n";
 goto START;

 }
}

// operator overloaded function to find the no. of days between two dates
int date::operator -(date d2)
{

 int i,nod1,nod2,nody,lc,no_of_days;
 nod1=nod2=lc=0;

  for(i=1;i<mm;i++)
 {
     if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
     nod1+=31;
     else if(i==2)
     nod1+=28;
     else
     nod1+=30;
 }

 nod1+=dd;

 for(i=1;i<d2.mm;i++)
 {
         if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
         nod2+=31;
        else if(i==2)
        nod2+=28;
        else
        nod2+=30;
 }

 nod2+=d2.dd;
nody=(yy-d2.yy)*365;

 for(i=d2.yy;i<yy;i++)
 if((i%4)==0)
 lc++;

 int y4=yy-d2.yy;
 while(y4>400)
 {
 lc++;
 y4-=400;
 }

 if((mm>2) && (yy%4)==0)
 lc++;
 if((d2.mm>2) && (d2.yy%4)==0)
 lc--;

 no_of_days=nody+nod1-nod2+lc;
 if(no_of_days>0)
 {
 cout<<"total number of days between these dates is=";
 return(no_of_days);
 }
 else
 return(no_of_days);
}

/* operator overloaded function to find the new date when no. of days are added to a
particular date.*/

date date::operator +(int nd)
{
 date dd3;

 while(nd>365)
 {
 yy++;
 nd-=365;
 }

 while(nd>30)
 {
 if(mm==1||mm==3||mm==5||mm==7||mm==8||mm==10||mm==12)
 {
 nd-=31;
 mm++;
 }
 else if(mm==2)
 {
 nd-=28;
 mm++;
 }
 else
 {
 nd-=30;
 mm++;
 }

 if(mm>12)
 {
 yy++;
 mm=1;
 }
 }

 dd=dd+nd;
 if(dd>30)
 {
 if(mm==4||mm==6||mm==9||mm==11)
 {
 mm++;
 dd-=30;
 }
 else if(mm==2)
 {
 mm++;
 dd-=28;
 }
 else if(dd>31)
 {
 mm++;
 dd-=31;
 }
 if(mm>12)
 {
 yy++;
 mm=1;
 }
 }

 dd3.mm=mm;
 dd3.dd=dd;
 dd3.yy=yy;
 cout<<"new date is:";
 cout<<dd<<"-"<<mm<<"-"<<yy<<endl;
 return(dd3);
}

//main function
void main()
{
 int res,num;
 date dd1,dd2;

 BEGIN:
 dd1.getdate();
 dd2.getdate();
 res=dd1-dd2;

 if(res<0)
 {
 cout<<"\n the first date should be greater than the second date\n";
 cout<<"so enter the dates again\n";
 goto BEGIN;
 }

 cout<<res;
 cout<<"\n enter the no. of days to be added to the FIRST date:";
 cin>>num;
 dd2=dd1+num;
}


OUTPUT

RUN-1
enter a valid date(dd mm yy)
30 2 2004
wrong input!!!

enter the date again....
29 2 2004
enter a valid date(dd mm yy)
12 6 2003
total number of days between these dates is=262
enter the no. of days to be added to the FIRST date:
new date is:24-3-2004
Press any key to continue

RUN-2
enter a valid date(dd mm yy)
31 12 2004
enter a valid date(dd mm yy)
31 12 2005
 the first date should be greater than the second date
so enter the dates again
enter a valid date(dd mm yy)
31 12 2004
enter a valid date(dd mm yy)
31 12 2003
total number of days between these dates is=366
enter the no. of days to be added to the FIRST date:13
new date is:13-1-2005
Press any key to continue