Search This Blog

Showing posts with label additionalpracticallist. Show all posts
Showing posts with label additionalpracticallist. Show all posts

Thursday, 28 January 2016

Write a C++ program using class Rectangle which contains length and width as data members and two member functions – getdata and area. The getdata function takes length and width as an input and the area function ccalculates the area and display it on the screen. Use Scope Resolution Operator.

#include<iostream>
using namespace std;

class Rectangle
{
int length,width;

public:

void getdata();

void area();

};

void Rectangle::getdata()
{
cout<<"Enter length:";
cin>>length;
cout<<"Enter width:";
cin>>width;
}
void Rectangle::area()
{
cout<<"Area is:"<<length*width;
}
int main()
{
Rectangle r;
r.getdata();
r.area();
return 0;
}

Writa a C++ program using class which contains two member functions getdata and putdata to read the information(name and age) of the person and disply it on the screen respectively. Use Scope Resolution Operator

#include<iostream>
using namespace std;

class Person
{
      int age;
      char name[10];

      public:

      void getdata();

      void putdata();
};

void Person::getdata()
{
cout<<"\nEnter name:";
cin>>name;
cout<<"Enter age:";
cin>>age;
}
void Person::putdata()
{
cout<<"\nName:"<<name<<endl;
cout<<"Age :"<<age<<endl;
}
int main()
{
Person p;
p.getdata();
p.putdata();
return 0;
}

Write a program to demonstrate the use of Scope Resolution Operator:: with variable name.

Without Class Example:

#include <iostream.h>

char n = 'k';   // A global variable

int main() {

   char n = 'g';   // A local variable
   cout  << ::n << endl;  // Print the global variable: k
   cout  << n   << endl;  // Print the local variable: g
}




With Class Example

#include <iostream.h>

class gajjar {
public:
  void output();
};

void gajjar::output() {
  cout << "Outside the class.\n";
}

int main() {
  gajjar x;
  x.output();

  return 0;

}

Assuming there are 7.481 gallons in a cubic foot, write a program that asks the user to enter a number of gallons, and then displays the equivalent in cubic feet.

#include<iostream.h>

using namespace std;
class gallon
{
float g;
public:

void data()
{
cout<<"\nEnter number of gallon:";
cin>>g;
}

void feet()
{
cout<<"\nEquivalent cubic feet is:"<<g/7.481;
}
};
int main()
{
gallon ans;
ans.data();
ans.feet();

return 0;
}

Write a C++ program, that will ask a temperature in Fahrenheit and display in Celsius using a class called temp and member functions.

#include<iostream>

using namespace std;

class temp
{
float feren,celc;
public:

void data()
{
cout<<"Enter Temp in fahrenheit:";
cin>>feren;
}

void convert()
{
celc=(0.56)*(feren-32);
cout<<"Temp in celsius is:"<<celc;
}
};

int main()
{
temp t;
t.data();
t.convert();
return 0;
}

Write a C++ program that will ask a temperature in Fahrenheit and display in Celsius without using class

#include<iostream>

using namespace std;

int main()
{
float feren,celc;

cout<<"Enter Temp in fahrenheit:";
cin>>feren;

celc=(0.56)*(feren-32);

cout<<"Temp in celsius is:"<<celc;

return 0;
}

An election is contested by five candidates. The candidates are numbered 1 to 5 and the voting is done by marking the candidate number on the ballot paper. Write a C++ program to read the ballots and count the votes cast for each candidate using an array variable count. In case, a number read is outside the range 1 to 5, the ballot should be considered as a 'spolit ballot', and the program should also count the number of spoilt ballots.

#include<iostream>

using namespace std;

int main()
{
int n,x=0,i,ch,one=0,two=0,three=0,four=0,five=0;
cout<<"Enter total num of students";
cin>>n;

for(i=0;i<n;i++)
{
cout<<"\nEnter votes for \n1 \n2 \n3 \n4 \n5\nEnter vote for:";
cin>>ch;

switch(ch)
{
case 1:one++;
break;

case 2:two++;
break;

case 3:three++;
break;

case 4:four++;
break;

case 5:five++;
break;

default:x++;
}

}


cout<<"\nvotes given to person 1 is:"<<one<<endl;
cout<<"\nvotes given to person 2 is:"<<two<<endl;
cout<<"\nvotes given to person 3 is:"<<three<<endl;
cout<<"\nvotes given to person 4 is:"<<four<<endl;
cout<<"\nspoiled vote are:"<<x;

return 0;
}

Write a program to read the values of a, b and c and display the value of x, where x = a/(b-c)

#include<iostream>

using namespace std;

int main()
{
int a,b,c,x;
cout<<"Enter a b & c for x=a/(b-c)"<<endl;
cin>>a>>b>>c;
if(b-c==0)
{
cout<<"Division not possible";
}
else
{
x=a/(b-c);
cout<<"x is:"<<x;
}
return 0;
}

Writa a C++ program to input an integer value from the keyboard and display the message "Good Morning" that many times on screen.

#include<iostream>

using namespace std;

int main()
{
int n;
cout<<"Enter Num:";
cin>>n;
int i;
for(i=0;i<n;i++)
{
cout<<"Good Morning!"<<endl;
}
return 0;
}