Q1
Q1
Q1
SOURCE CODE:
#include<iostream>
if(x<=100)
{
charge=x*0.60;
}
else if(x>100 && x<=300)
{
charge= x*0.80;
}
else
{
charge=x*0.90;
}
if(charge<=300)
{
charge=charge+50;
}
else
{
charge=charge+(charge*0.15)+50;
}
cout<<"Name :"<<ch<<endl;
cout<<"Charges: "<<charge<<endl;
char choice;
cout<<"Do you want to add another element :(y/n)";
cin>>choice;
if(choice=='n')
{
break;
}
}
return 0;
}
OUTPUT:
Q2.Construct a C++ program that removes a specific character from a given
strin Typical g and return the updated string.
Input:
computer
science is the future
Typical Output: compuer science is he fuure
SOURCE CODE:
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
string str;
char x;
int i=0;
string ans;
cout<<"Enter the string:"<< endl;
cin.ignore();
getline(cin,str);
while(str[i]!='\0')
{
if(str[i]!=x)
{
ans=ans+str[i];
}
i++;
}
cout<<"The final string is : "<<ans <<endl;
return 0;
OUTPUT:
Q3.Implement a C++ program to find the non-repeating characters in string.
Tyoical input:graphic era university
SOURCE CODE:
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
string str;
int i,j;
cout<<"Enter the string"<< endl;
cin.ignore();
getline(cin,str);
int len=str.length();
cout<<"Non_repeating characters are :"<<endl;
for(i=0;i<len;i++)
{
char ch=str[i];
bool is_repeating=false;
for(j=1;j<len;j++)
{
if(i!=j && ch==str[j])
{
is_repeating=true;
break;
}
}
if(!is_repeating)
{
cout<<ch<<endl;
}
return 0;
}
OUTPUT:
Q4.You are given an array of elements. Now you need to choose the best index
of this array. An index of the array is called best if the special sum of this
index is maximum across the special sum of all the other indices. To
calculate the special sum for any index you pick the first element that is and
add it to your sum. Now you pick next two elements i.e., and and add both
of them to your sum. Now you will pick the next elements, and this continues
till the index for which it is possible to pick the elements. Find the best index
and in the output print its corresponding special sum. Note that there may
be more than one best index, but you need to only print the maximum special
sum.
Input
First line contains an integer as input. Next line contains space separated
integers denoting the elements of the array Output
In the output you have to print an integer that denotes the maximum special
Sum
SOURCE CODE:
#include<iostream>
using namespace std;
}
if(current_sum>max_sum)
{
max_sum=current_sum;
}
}
int main()
{
int n,i;
cout<<"Enter the size of and array :"<<endl;
cin>>n;
int arr[n];
cout<<"Enter the elements in an array :"<<endl;
for(i=0;i<n;i++)
{
cin>>arr[i];
}
best_index(arr,n);
}
OUTPUT:
Q5.Implement a C++ program to demonstrate the concept of data abstraction
using the concept of Class and Objects .
SOURCE CODE:
#include<iostream>
using namespace std;
class student
{
private:
string name;
string course;
char sem;
string section;
int rollno;
public:
void details()
{
cout<<"Enter name:"<<endl;
cin.ignore();
getline(cin,name);
cout<<"Enter course:"<<endl;
cin.ignore();
getline(cin,course);
cout<<"Enter semester:"<<endl;
cin>>sem;
cout<<"Enter section:"<<endl;
cin>>section;
cout<<"Enter roll number:"<<endl;
cin>>rollno;
void display()
{
cout<<"Name : \t"<<name<<endl;
cout<<"Course :\t"<<course<<endl;
cout<<"Semester :\t"<<sem<<endl;
cout<<"Section :\t"<<section<<endl;
cout<<"Roll No. :\t"<<rollno<<endl;
}
};
int main()
{
student s1;
char choice;
while(true)
{
s1.details();
s1.display();
cout<<"Continue or exit:(y/n)"<<endl;
cin>>choice;
if(choice=='n')
{
break;
}
}
}
OUTPUT:
Q6.Define a class Hotel in C++ with the following specifications
Private members
* Rno Data member to store room number
* Name Data member to store customer name
* Tariff Data member to store per day charges
* NOD Data member to store number of days of stay
* CALC() Function to calculate and return amount as NOD*Tariff ,and if the value of days*
Tariff >10000, then total amount is 1.05* days*Tariff. Public members
* Checkin() Function to enter the content Rno, Name, Tariff and NOD
* Checkout() Function to display Rno, Name, Tariff, NOD and Amount (amount to be
displayed by calling function) CALC()
SOURCE CODE:
#include<iostream>
using namespace std;
class hotel
{
private:
int Rno;
string name;
int tariff;
int NOD;
float amt;
float Calc()
{
amt=NOD*tariff;
if(amt>10000)
{
amt=amt*1.05;
}
return amt;
}
public:
void Checkin()
{
cout<<"Enter Room number:";
cin>>Rno;
cout<<"Enter Customer Name:";
cin.ignore();
getline(cin,name);
cout<<"Enter per day charges:";
cin>>tariff;
cout<<"Enter number of days of stay:";
cin>>NOD;
void Checkout()
{
cout<<"Rno:"<<Rno<<endl;
cout<<"Tariff:"<<tariff<<endl;
cout<<"NOD:"<<NOD<<endl;
cout<<"Amount:"<<Calc();
}
};
int main()
{
hotel h;
h.Checkin();
h.Checkout();
return 0;
}
OUTPUT:
Q7. Implement a Program in C++ by defining a class to represent a bank account.
Include the following:
Data Members
• Name of the depositor
• Account number
• Type of account (Saving, Current etc.)
• Balance amount in the account
Member Functions
• To assign initial values
• To deposit an amount
To withdraw an amount after checking the balance
To display name and balance
SOURCE CODE:
#include<iostream>
using namespace std;
class bank
{
string name;
int accNum;
string typeAcc;
int balance;
int deposit_amt;
int withdraw_amt;
public:
void set_value();
void deposit();
void withdraw();
void display();
};
}
else
{
balance=balance-withdraw_amt;
cout<<"Withdrawal is successful."<<endl;
}
}
}
int main()
{
bank c;
c.set_value();
c.deposit();
c.withdraw();
c.display();
}
OUTPUT:
Q8. Anna is a contender for valedictorian of her high school. She wants to know
how many students (if any) have scored higher than her in the exams given
during this semester.
Create a class named Student with the following specifications:
An instance variable named scores holds a student's 5 exam scores.
A void input () function reads 5 integers and saves them to scores.
An int calculateTotalScore() function that returns the sum of the
student's scores.
Input Format
In the void Student::input() function, you must read 5 scores from standard
input and save them to your scores instance variable.
Output Format
In the int Student::calculateTotalScore() function, you must return the
student's total grade (the sum of the values in scores).
The code in the editor will determine how many scores are larger than
Anna’s and print that number to the console.
SOURCE CODE:
#include<iostream>
using namespace std;
class Student
{
int score[5];
public:
void input();
int calc_TotalScore();
};
for(int i=0;i<5;i++)
{
cout<<"Score in subject "<<i+1<<" is :";
cin>>score[i];
}
}
int main()
{
int n,count=0;
cout<<"Enter number of students :";
cin>>n;
Student s[n];
cout<<"Enter Anna's marks "<<endl;
s[0].input();
int Anna=s[0].calc_TotalScore();
cout<<"Marks of other students "<<endl;
for(int i=1;i<n;i++)
{
cout<<"Student "<<i<<endl;
s[n].input();
s[n].calc_TotalScore();
if(Anna<s[n].calc_TotalScore())
{
count ++;
}
OUTPUT:
Q9. Construct a Program in C++ to show the working of function
overloading(compile time polymorphism) by using a function named
calculate Area () to calculate area of square, rectangle and triangle using
different signatures as required.
SOURCE CODE:
#include <iostream>
using namespace std;
return 0;
}
OUTPUT: