All Coding
All Coding
Attempt : 1
Total Mark : 200
Marks Obtained : 200
Section 1 : CODING
1. Problem Statement:
Welcome Message
Nikhil, the founder of this company wished to take the company’s services
to the next step and decided to design an Event Management System that
would let its Customers plan and host events seamlessly via an online
platform. As a part of this requirement, Nikhil wanted to write a piece of
code for his company’s Examly Event Management System that will
welcome all the Customers who are using it. Help Nikhil on the task.
Answer
#include<iostream>
using namespace std;
int main()
{
cout<<"Welcome to Examly Event Management System";
return 0;
}
2. Problem Statement:
Number of events
Answer
#include<iostream>
using namespace std;
int main()
{
int s;
cin>>s;
cout<<"Number of events hosted in Codissia is "<<s;
}
Event Details
Write a program that will get the input of the event details like the name of
the event, type of the event, number of people expected, a string value (Y/
N) telling whether the event is going to be a paid entry, and the projected
expenses (in lakhs) for the event. The program should then display the
input values as formatted output.
Answer
#include<iostream>
using namespace std;
int main()
{
string a,b;
char c;
int d;
float e;
getline(cin,a);
cin>>b>>d>>c>>e;
cout<<"Event Name :"<<a;
cout<<"\nEvent Type :"<<b;
cout<<"\nExpected Count :"<<d;
cout<<"\nPaid Entry :"<<c;
cout<<"\nProjected Expense :"<<e<<"L";
}
4. Problem Statement :
Naming Ceremony
Hermoine and Ron were blessed with a baby. They need to name their child
and the priest doesn't know the actual spelling of it. So, help them to get
and display the child's name.
Answer
// You are using GCC
#include<iostream>
using namespace std;
int main()
{
string name;
getline(cin,name);
cout<<name;
}
5. Problem Statement :
Round Off
Write a program to get a float value from the user and display it in the
below-mentioned format. HINT: Use ceil() and floor() functions from
header file cmath.
Answer
// You are using GCC
#include <iostream>
#include<cmath>
using namespace std;
int main()
{
float a;
cin>>a;
cout<<(int)a<<endl;
cout<<ceil(a)<<endl;
cout<<floor(a)<<endl;
}
6. Problem Statement :
Answer
// You are using GCC
#include<iostream>
using namespace std;
int main()
{
char a;
cin>>a;
cout<<int(a);
}
7. Problem Statement :
In Japan, there was a very huge Tsunami. Millions and millions worth of
buildings and property were destroyed. Many people lost their lives while
some of them were injured and few were safe. A news reporter arrives at
the spot to take the current survey regarding the situation of the people
alive, dead and injured. He wanted to publish it in the newspaper and reach
out to other countries asking to help the affected people. Can you please
help him in this noble cause by writing a program to generate the
newspaper report?
Answer
// You are using GCC
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cin>>a>>b>>c;
cout<<"1)Dead : "<<a<<endl;
cout<<"\n2)Injured : "<<b<<endl;
cout<<"\n3)Safe : "<<c<<endl;
cout<<"\nPlease help the people who are suffering!!!";
return 0;
}
8. Problem Statement :
Answer
// You are using GCC
#include<iostream>
using namespace std;
int main()
{
int a,b;
cin>>a>>b;
cout<<a+b<<"\n"<<a-b<<"\n"<<a*b<<"\n"<<a/b;
return 0;
}
9. Problem Statement :
Cricket Stadium
There was a large ground in the center of the city which is rectangular in
shape. The Corporation decides to build a Cricket stadium in the area for
school and college students, But the area was used as a car parking zone.
In order to protect the land from using as an unauthorized parking zone,
the corporation wanted to protect the stadium by building a fence. In order
to help the workers to build a fence, they planned to place a thick rope
around the ground. They wanted to buy only the exact length of the rope
that is needed. They also wanted to cover the entire ground with a carpet
during the rainy season. They wanted to buy only the exact quantity of
carpet that is needed. They requested your help. Can you please help them
by writing a program to find the exact length of the rope and the exact
quantity of carpet that is required?
Answer
// You are using GCC
#include<iostream>
using namespace std;
int area(int x,int y)
{
int c;
c=x*y;
return c;
}
int per(int a,int b)
{
int d;
d=2*(a+b);
return d;
}
int main()
{
int x,y,f,u;
cin>>x>>y;
f=per(x,y);
u=area(x,y);
cout<<f<<"\n"<<u;
}
Average Calculation
A teacher wants to compute the average of 5 students in her class. Write a
program to help her to find the average. The average is the sum of all the
numbers, then divided by the total numbers.
Answer
// You are using GCC
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
int i;
float num[5],sum=0,avg;
for(i=0;i<5;i++)
{
cin>>num[i];
sum+=num[i];
}
avg=sum/5;
cout<<fixed<<setprecision(2)<<avg;
}
Training for sports day has begun and the physical education teacher has
decided to conduct some team games. The teacher wants to split the
students in higher secondary into equal-sized teams. In some cases, there
may be some students who are left out from the teams and he wanted to
use the left out, students, to assist him in conducting the team games. For
instance, if there are 50 students in a class and if the class has to be
divided into 7 equal-sized teams, 7 students will be there in each team and
1 student will be left out. That 1 student will assist the PET. With this idea
in mind, the PET wants your help to automate this team splitting task. Can
you please help him out?
Answer
// You are using GCC
#include<iostream>
using namespace std;
int main()
{
int a,b;
cin>>a>>b;
cout<<a/b<<"\n"<<a%b<<endl;
}
Pranav, an enthusiastic kid visited the "Fun Fair 2017" along with his family.
His father wanted him to purchase entry tickets from the counter for his
family members. Being a little kid, he is just learning to understand units of
money. Pranav has paid some amount of money for the tickets but he
wants your help to give him back the change of Rs. N using the minimum
number of rupee notes.
Answer
// You are using GCC
#include<iostream>
using namespace std;
int main()
{
int N,x;
cin>>N;
x=N/100;
int y=N%100;
int z=y/50;
int a=y%50;
int b=a/10;
int c=a%10;
int d=c/5;
int e=c%5;
int f=e/2;
int g=e%2;
cout<<x+z+b+d+f+g<<endl;
}
Answer
#include<bits/stdc++.h>
using namespace std;
int main()
{
float a,b,c,x,y,d,r,i;
cin>>a>>b>>c;
d = pow(b,2) - 4*a*c;
if(a==0 && b==0 && c==0)
{
cout<<"Root1: -nan\nRoot2: -nan"<<endl;
}
else
{
if(d>0)
{
x=(-b+sqrt(d))/(2*a);
y=(-b-sqrt(d))/(2*a);
cout<<"Root1: "<<fixed<<setprecision(4)<<x<<endl;
cout<<"Root2: "<<fixed<<setprecision(4)<<y<<endl;
}
else if(d==0)
{
cout<<"Root1: "<<fixed<<setprecision(4)<<-b/(2*a)<<endl;
cout<<"Root2: "<<fixed<<setprecision(4)<<-b/(2*a)<<endl;
}
else
{
r=-b/(2*a);
i=sqrt(-d)/(2*a);
cout<<"Root1: "<<fixed<<setprecision(4)<<r<<" +
"<<fixed<<setprecision(4)<<i<<"i"<<endl;
cout<<"Root2: "<<fixed<<setprecision(4)<<r<<" -
"<<fixed<<setprecision(4)<<i<<"i"<<endl;
}
}}
Evaluation of expression:
Write the program and trace the output for the following expressions:
Answer
// You are using GCC
#include<iostream>
using namespace std;
int main()
{
int a,b,c,u,v,i,j,k;
float x,y,z;
cin>>a>>b>>c>>u>>v>>i>>j>>k;
x = a+b*5/4+c%3*5;
y = u>v?u:v;
z = ++i&&++j&&++k;
cout<<"x = "<<x<<endl<<"y = "<<y<<endl<<"z = "<<z<<endl;
return 0;
}
ASCII Values - II
Answer
// You are using GCC
#include<iostream>
using namespace std;
int main()
{
int s;
cin>>s;
cout<<char(s);
}
Answer
// You are using GCC
#include<iostream>
using namespace std;
int main()
{
int w,x,y;
cin>>w>>x>>y;
cout<<(w*(x-y))-100<<endl;
}
Four musketeers
Dhoni joined the group of 3 Musketeers and now their group is called four
Musketeers. Meanwhile, Dhoni also moved to a new house in the same
locality nearby to the other three. Currently, the houses of Sachin, Dravid
and Ganguly are located in the shape of a triangle. When the three
musketeers asked Dhoni about the location of his house, he said that his
house is equidistant from the houses of the other 3. Can you please help
them find out the location of the house? Given the 3 locations {(a1,b1),
(a2,b2) and (a3,b3)} of a triangle, write a program to determine the point
which is equidistant from all the 3 points.
Answer
// You are using GCC
#include<iostream>
using namespace std;
int main()
{
int a,b,c,d,e,f;
cin>>a>>d>>b>>e>>c>>f;
cout<<(float)(a+b+c)/3<<endl<<(float)(d+e+f)/3<<endl;
}
Answer
// You are using GCC
#include<iostream>
using namespace std;
int main()
{
float a,b,c;
cin>>a>>b>>c;
cout<<(a+b)<<endl;
cout<<(a+b)-(((a+b)*c)/100)<<endl;
cout<<(((a+b)*c)/100);
}
Area Calculation
Sheela has three things in her bag. She wants to compute the area of 3
things but 3 things are in different shapes. The three things are a square
shape, rectangular shape, and circular shape respectively. Write a program
to help Sheela to calculate the area of different shapes.
Answer
// You are using GCC
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int s,l,b;
float r;
cin>>s>>l>>b>>r;
cout<<s*s<<endl;
cout<<l*b<<endl;
cout<<fixed<<setprecision(2)<<3.14*r*r;
}
IRC_CPP_COD_CONTROL STATEMENTS
Attempt : 1
Total Mark : 200
Marks Obtained : 200
Section 1 : CODING
1. Problem Statement :
Answer
#include<iostream>
using namespace std;
int main()
{
int marks;
cin>>marks;
if(marks>100 || marks<0)
{
cout<<"Invalid Input";
}
else
{
switch(marks/10){
case 10:
cout<<"S";
break;
case 9:
cout<<"A";
break;
case 8:
cout<<"B";
break;
case 7:
cout<<"C";
break;
case 6:
cout<<"D";
break;
case 5:
cout<<"E";
break;
default:
cout<<"F";
}
}
}
2. Problem Statement :
Profit or loss
Answer
// You are using GCC
#include<iostream>
using namespace std;
int main()
{
float buy,sell;
cin>>buy>>sell;
if((sell*12)<buy)
cout<<"Loss : Rs."<<buy-(sell*12);
else
cout<<"Profit : Rs."<<(sell*12)-buy;
}
3. Problem Statement :
Write a program to calculate the hotel tariff. The room rent is 20% high
during peak seasons [April, May, June, November, December]. Note: Use
the switch construct.
Answer
// You are using GCC
#include<iostream>
using namespace std;
int main()
{
int month,rent,days;
float tarrif;
cin>>month>>rent>>days;
if(month==4 || month==5 || month==6 || month==11 ||month==12)
{
tarrif=(rent*days)+((rent/5)*days);
cout<<tarrif<<endl;
}
else if(month<1 || month>12)
cout<<"Invalid Input";
else
cout<<rent*days;
}
4. Problem Statement:
Answer
// You are using GCC
#include<iostream>
using namespace std;
int main()
{
float a,b,c;
cin>>a>>b>>c;
if(a==b&&b==c&&c==a)
cout<<"Equilateral Triangle"<<endl;
else if(a==b||b==c||c==a)
cout<<"Isosceles Triangle"<<endl;
else
cout<<"Scalene Triangle"<<endl;
}
5. Problem Statement:
Answer
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int num_1,num_2;
char op;
cin>>num_1>>num_2>>op;
switch(op)
{
case '+' :
printf("%.2f",float(num_1+num_2));
break;
case '-':
printf("%.2f",float(num_1-num_2));
break;
case '*' :
printf("%.2f",float(num_1*num_2));
break;
case '/' :
if(num_2==0)
{
cout<<"Invalid Input"<<endl;
break;
}
printf("%.2f",float(num_1)/float(num_2));
break;
case '%' :
printf("%.2f",float(num_1%num_2));
break;
default:
cout<<"Invalid Input"<<endl;
}
}
6. Problem Statement :
Amoeba Multiplication
Answer
// You are using GCC
#include<iostream>
using namespace std;
int main()
{
int n,t1=0,t2=1,p;
cin>>n;
if(n==1)
cout<<t1;
else if(n==2)
cout<<t2;
else
{
for(int i=3;i<=n;i++)
{
p=t1+t2;
t1=t2;
t2=p;
}
cout<<p;
}
}
Status : Correct Marks : 10/10
7. Problem Statement:-
Write a program to generate the first 'n' terms of the following series 121,
225, 361,...
Answer
// You are using GCC
#include<iostream>
using namespace std;
int main(){
int N=11,n;
cin>>n;
while(n--)
{
cout<<N*N<<" ";
N+=4;
}
}
8. Problem Statement:
Team Event
Super Quiz Bee is a famous quiz Competition that tests students on a wide
variety of academic subjects. This week’s competition was a Team event
and students who register for the event will be given a unique registration
code N. The participants are teamed into 10 teams and the team to which
a participant is assigned to depends on the absolute difference between
the first and last digit in the registration code.
Answer
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,len,ten=1,f,l;
cin>>n;
int temp=n;
while(temp!=0)
{
temp/=10;
if(temp!=0)
{ten*=10;}
}
if(n>=10)
{
f=n/ten;
l=n%10;
cout<<abs(f-l);
}
else
{
cout<<"Invalid Input";
}
return 0;
}
9. Problem Statement:
Lucky Pairs
Richie and Riya are participating in a game called "Lucky pairs" at the
Annual Game Fair in their Company. As per the rules of the contest, two
members form a team and Richie initially has the number A and Riya has
the number B.
There are a total of N turns in the game, and Richie and Riya alternatively
take turns. In each turn, the player whose turn it is multiplies his or her
number by 2. Richie has the first turn. Suppose after the entire N turns,
Richie’s number has become C and Riya’s number has become D. The final
score of the team will be the sum of the scores (C+D) of both the players
after N turns.
Write a program to facilitate the quiz organizers to find the final scores of
the teams
Answer
// You are using GCC
#include<iostream>
using namespace std;
int main()
{
int a,b,n,i;
cin>>a>>b>>n;
if(n%2==0)
{
for(i=0;i<(n/2);i++)
{
a*=2;
b*=2;
}
cout<<a+b;
}
else
{
for(i=0;i<((n/2)+1);i++)
a*=2;
for(i=0;i<(n/2);i++)
b*=2;
cout<<a+b;
}
}
Status : Correct Marks : 10/10
AES Numbers
Answer
// You are using GCC
#include<iostream>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
int i,j,k,a,b=0;
for(i=n;i<=m;++i)
{
a=0;
for(j=1;j<=i;++j)
{
k=i%j;
if(k==0)
a+=1;
}
if(a==4)
b+=1;
}
cout<<b;
}
Checking alphabets
Answer
// You are using GCC
#include<iostream>
using namespace std;
int main()
{
char n;
cin>>n;
if(n=='a'||n=='e'||n=='i'||n=='o'||n=='u'||n=='A'||n=='E'||n=='I'||n=='O'||n=='U')
cout<<"Vowel";
else
cout<<"Consonant";
}
Age Detector
Ask a user for their birth year encoded as two digits (like "62") and for the
current year, also encoded as two digits (like "99"). Write a program to find
the user's current age in years.
Answer
// You are using GCC
#include<iostream>
using namespace std;
int main()
{
int n,m,j;
cin>>n>>m;
if(n>m){
j=(m-n)+100;
cout<<j;}
else{
j=m-n;
cout<<j;}
Lab Allocation I
There are 3 labs in the CSE department(L1, L2, and L3) with a seating
capacity of x, y, and z respectively. Find the lab which has minimal seating
capacity.
Answer
// You are using GCC
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cin>>a>>b>>c;
if(a<b&&b<c)
cout<<"L1";
else if(b<a&&b<c)
cout<<"L2";
else
cout<<"L3";
}
Answer
// You are using GCC
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int i,a;
for(i=1;i<=n;i++)
{
if(i%2==0)
{
a=(i*i)-2;
cout<<a<<" ";
}
else
{
a=(i*i)-1;
cout<<a<<" ";
}
}
}
Write a program to generate the first 'n' terms of the following series 1, 2, 3,
6, 9, 18, 27,...
Answer
// You are using GCC
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n;
cin>>n;
int i,a1=1,a2=2;
if(n>=1)
cout<<a1<<" ";
if(n>=2)
cout<<a2<<" ";
if(n>3)
{
for(i=3;i<=n;++i)
{if(i%2==0){
a2*=3;
cout<<a2<<" ";
}
else
{
a1*=3;
cout<<a1<<" ";
}
}
}}
Pattern 1
Write a program to print the given pattern.
Answer
// You are using GCC
#include<iostream>
using namespace std;
int main(){
int n;
cin>>n;
int a;
a=(2*n)-1;
int i,j;
for(i=1;i<=n;++i)
{for(j=1;j<=a;++j)
cout<<j<<" ";
a-=2;
cout<<endl;
}
}
Pattern 2
Answer
// You are using GCC
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,temp=1;
cin>>n;
if(n%2==0)
{
for(int i=1;i<=n;)
{
for(int j=0;j<2;j++)
{
for(int k=0;k<temp;k++)
{
if(i%2==0)
cout<<"0 ";
else
cout<<"1 ";
}
i++;
cout<<endl;
}
temp++;
}
}
else
{
if(n>=1)
cout<<"0 "<<endl;
if(n>1){
for(int i=2;i<=n;i++)
{
for(int j=1;j<=temp+1;j++)
{
if(j%2==1)
cout<<"1 ";
else
cout<<"0 ";
}
++temp;
cout<<endl;
}
}
}
}
Answer
// You are using GCC
#include<iostream>
using namespace std;
int main(){
int m=0, n;
cin>>n;
cout<<n<<endl;
for(;n!=1;m++)
{
if(n%2==1)
{
n=(3*n)+1;
cout<<n<<endl;
}
else
{
n/=2;
cout<<n<<endl;
}
}
cout<<m;
Comparing 2 integers
Get two integers x and y from the user and write a program to relate 2
integers as equal to, less than or greater than.
Answer
// You are using GCC
#include<iostream>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
if(n==m)
cout<<n<<" and "<<m<<" are equal";
else if(n>m)
cout<<n<<" greater than "<<m;
else
cout<<n<<" less than "<<m;
}
Ask the customer's age and for the time on a 24-hour clock (where noon is
12.00 and 4:30 PM is 16.30). The show timings are 10.15, 13.30, 18.00 and
22.00. The normal adult ticket price is $\$$8.00, however, the adult
matinee price is $\$$5.00. Adults are those over 13 years. The normal
children's ticket price is $\$$4.00, however, the children's matinee price is $
\$$2.00. Write a program that determines the price of a movie ticket
Answer
// You are using GCC
#include<iostream>
using namespace std;
int main()
{
int a;
float s;
cin>>a>>s;
if(a>=13 && s<=12.00)
cout<<"$8.00";
else if(a>=13 && s>12.00)
cout<<"$5.00";
else if(a<13 && s<=12.00)
cout<<"$4.00";
else
cout<<"$2.00";
}
IRC_CPP_COD_ARRAYS
Attempt : 1
Total Mark : 150
Marks Obtained : 150
Section 1 : CODING
1. Problem Statement :
Array deletion
Answer
#include<iostream>
using namespace std;
int main()
{
int n,x,i;
cin>>n;
int arr[n];
for(i=0;i<n;i++)
cin>>arr[i];
cin>>x;
cout<<"Array after deletion:"<<endl;
for(i=0;i<n;i++)
{
if(arr[i]==x)
{
++i;
if(i>=n)
break;
}
cout<<arr[i]<<endl;
}
return 0;
2. Problem Statement :
Array median
Write a program to find the median of the elements in the array. The
Median is the middle value in the sorted list. If there are an even number of
elements in the list, the median is the mean of the 2 middle values.
Answer
// You are using GCC
#include<iostream>
#include<iomanip>
#include<algorithm>
using namespace std;
void mean(int arr[],int n)
{ double sum;
if(n%2==0)
{
sum=arr[n/2]+arr[(n/2)-1];
sum/=2;
}
else
sum=arr[n/2];
cout<<"Median = "<<fixed<<setprecision(1)<<sum;
}
int main()
{
int n,x,i;
cin>>n;
int arr[n];
for(i=0;i<n;i++)
cin>>arr[i];
sort(arr,arr+n);
mean(arr,n);
}
3. Problem Statement :
Answer
// You are using GCC
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int a,x=0;
cin>>a;
int arr[a];
for(int i=0;i<a;i++)
cin>>arr[i];
sort(arr,arr+a);
for(int i=0;i<a;i++)
{
if(arr[i]==arr[i+1]);
else
x+=1;
}
cout<<"There are "<<x<<" distinct element in the array.";
}
4. Problem Statement:
LucarnosFilm Festival is an annual film festival and is also known for being
a prestigious platform for art house films. This time at the Lucarnos Film
festival there are N movies screened, each of different genres ranging from
drama movies to comedy ones and teen movies to horror ones. Lucy is a
huge fan of movies and visited the film festival, but she's not sure which
movie she should watch.
Each movie can be characterized by two integers Li and Ri, denoting the
length and the rating of the corresponding movie. Lucy wants to watch
exactly one movie with the maximal value of Li × Ri. If there are several
such movies, she would pick one with the maximal Ri among them. If there
is still a tie, she would pick the one with the minimal index among them.
Write a program to help Lucy pick a movie to watch at the film festival.
Answer
#include<iostream>
using namespace std;
int main()
{
int n,ind=0,tem1=0;
cin>>n;
int a[2][n];
for(int i=0;i<2;i++)
for(int j=0;j<n;j++)
cin>>a[i][j];
for(int i=0;i<n;i++)
{
int temp=a[0][i];
if(tem1<(a[1][i])*temp)
{
tem1=a[1][i]*temp;
ind=i;
}
else if(tem1==(a[1][i])*temp)
{
if(a[1][i]==a[1][ind])
ind=i<ind?i:ind;
else if(a[1][i]>a[1][ind])
ind=i;
}
}
cout<<ind+1;
return 0;
}
5. Problem Statement:
A source file can either be or not be in any of these two sequences. Your
task is to calculate two values: the number of source files of the project,
that are both tracked and ignored, and the number of source files of the
project, that are both untracked and unignored.
Answer
#include<iostream>
using namespace std;
int main()
{
int n,m,k,zero=0;
cin>>n>>m>>k;
int a[m],a1[n],b[k],b1[n];
for(int i=0;i<m;i++) cin>>a[i];
for(int i=0;i<k;i++) cin>>b[i];
for(int i=1;i<=n;i++)
{
int check=0;
for(int j=0;j<m;j++)
{
if(a[j]==i) check++;
if(j==m-1&&check==0)
{
a1[zero]=i;
zero+=1;
}
}
}
int zero1=0;
for(int i=1;i<=n;i++)
{
int check=0;
for(int j=0;j<k;j++)
{
if(b[j]==i) check++;
if(j==k-1&&check==0)
{
b1[zero1]=i;
zero1+=1;
}
}
}
int check1=0,check2=0;
for(int i=0;i<m;i++) for(int j=0;j<k;j++) if(a[i]==b[j]) check1++;
for(int i=0;i<zero;i++) for(int j=0;j<zero1;j++) if(a1[i]==b1[j]) check2++;
cout<<check1<<" "<<check2;
return 0;
}
6. Problem Statement :
Sum of Zig-Zag
Rickon Stark is not able to figure out the method to calculate the sum of
the Zig-Zag pattern in the Matrix. Can you help Rickon to write a C++
program to find the sum of the Zig-Zag pattern in a given matrix?
Answer
#include<iostream>
using namespace std;
int main()
{
int r,c,sum=0,i,j;
cin>>r>>c;
int a[r][c];
for(i=0;i<r;i++)
for(j=0;j<c;j++)
cin>>a[i][j];
for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
if(i==0||i==r-1||i==j)
sum+=a[i][j];
}
cout<<"Sum of Zig-Zag pattern is "<<sum<<endl;
}
7. Problem Statement :
There are some students in the class. The class Adviser needs a
recruitment process for above 60% of students. In each and every row
there was a student above 60%. Help your adviser to find the students in
each row.
Answer
// You are using GCC
#include<iostream>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
int a[n][m];
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
cin>>a[i][j];
for(int i=0;i<n;i++)
{
int max=0;
for(int j=0;j<m-1;j++)
max=a[i][j]>a[i][j+1]?a[i][j]:a[i][j+1];
cout<<max<<" ";
}
return 0;
}
8. Problem Statement :
Transpose Matrix
Answer
#include<iostream>
using namespace std;
int main()
{
int r,c,i,j;
cin>>r;
c=r;
int arr[r][c];
for(i=0;i<r;i++)
for(j=0;j<c;j++)
cin>>arr[i][j];
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cout<<arr[i][j]<<" ";
cout<<endl;
}
cout<<"Transpose matrix is:"<<endl;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cout<<arr[j][i]<<" ";
}
cout<<endl;
}
9. Problem Statement:
Full Islands:
Answer
#include<iostream>
using namespace std;
int main()
{
int n,i1,j1;
cin>>n;
int a[n][n];
for(int i=0;i<n;i++) for(int j=0;j<n;j++) cin>>a[i][j];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(a[i][j]==1)
{
if(i==0)
{
if(a[i+1][j]!=1&&a[i][j+1]!=1) a[i+1][j]=0;
}
else a[i-1][j]=0;
if(i==n-1) a[i-1][j]=0;
else
{
if(a[i+1][j]!=1&&a[i][j+1]!=1) a[i+1][j]=0;
}
if(j==n-1) a[i][j-1]=0;
else
{
if(a[i][j+1]!=1) a[i][j+1]=0;
}
if(j==0)
{
if(a[i][j+1]!=1) a[i][j+1]=0;
}
else a[i][j-1]=0;
}
}
}
cout<<endl;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++) cout<<a[i][j]<<" ";
cout<<endl;
}
return 0;
}
Answer
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int a[n][n];
for(int i=0;i<n;i++) for(int j=0;j<n;j++) cin>>a[i][j];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(a[i][j]>0&&a[i][j]<20)
{
if((a[i+2][j]<20&&a[i+2][j]>0)&&i+2<n) a[i+1][j]=0;
if((a[i-2][j]<20&&a[i-2][j]>0)&&i-2>=0) a[i-1][j]=0;
if((a[i][j+2]<20&&a[i][j+2]>0)&&j+2<n) a[i][j+1]=0;
if((a[i][j-2]<20&&a[i][j-2]>0)&&j-2>=0) a[i][j-1]=0;
if((a[i+1][j+1]<20&&a[i+1][j+1]>0)&&(((i+1)<n)&&((j+1)<n)))
{
a[i+1][j]=0;
a[i][j+1]=0;
}
if((a[i+1][j-1]<10&&a[i+1][j-1]>0)&&(((i+1)<n)&&((j-1)>=0)))
{
a[i][j-1]=0;
a[i+1][j]=0;
}
}
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++) cout<<a[i][j]<<" ";
cout<<endl;
}
return 0;
}
You are playing the PUBG game and you entered into the Bootcamp. There
you viewed the dropbox which was filled with guns. you have to choose the
biggest gun in the dropbox. Find the biggest gun and you will get the
chicken dinner.
Answer
#include<iostream>
using namespace std;
int main()
{
int n,i,j,max;
cin>>n;
int arr[16];
for(i=0;i<n;i++)
cin>>arr[i];
max=arr[0];
for(i=0;i<n;i++)
{
if(arr[i]>max)
{
max=arr[i];
}
}
cout<<max<<" is the maximum element in the array.";
}
Sum of 2 arrays
Ramu has some number of Apples and he arranges that in a matrix format.
Raju has another number of Apples. He also wants to arrange that in a
matrix format. Ragul wants to calculate the total number of apples. Help
him to find out the total number of apples.
Answer
#include<iostream>
using namespace std;
int main()
{
int i,n,a[16],b[16];
cin>>n;
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
cin>>b[i];
for(i=0;i<n;i++)
cout<<a[i]+b[i]<<endl;
}
Kuty and Pappu are playing a coding game. Kuty gives a set of numbers to
Pappu to find the even numbers in the set and sum them. Write a program
to help Pappu to solve the game and to win the dairy milk.
Answer
#include<iostream>
using namespace std;
int main()
{
int n,i,sum,arr[16];
cin>>n;
for(i=0;i<n;i++)
cin>>arr[i];
for(i=0;i<n;i++)
{
if(arr[i]%2==0)
sum+=arr[i];
}
cout<<"The sum of even numbers in the array is "<<sum;
}
In a family, the people are arranged in rows and columns. Male persons in
the families are arranged in a row and females are arranged in a column.
Find the biggest women in each column. Help me to find the biggest
woman. Write a program to find the maximum element in each column of
the matrix.
Answer
// You are using GCC//
#include<iostream>
using namespace std;
int main()
{
int i,j,r,c,max=0,a[11][11];
cin>>r>>c;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
cin>>a[i][j];
for(j=0;j<c;j++)
{
max=0;
for(i=0;i<r;i++)
{
if(a[i][j]>max)
max=a[i][j];
}
cout<<max<<" ";
}
}
Matrix Sum
Shobha and Siddhesh were playing puzzles. They are having two puzzles in
their hand. They need to join the puzzle to get the correct puzzle. Help
them to find the puzzle.
Answer
// You are using GCC
#include<iostream>
using namespace std;
int main()
{
int arr[11][11],m,n,i,j,sum=0;
cin>>m>>n;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>arr[i][j];
for(i=0;i<m;i++)
for(j=0;j<n;j++)
sum+=arr[i][j];
cout<<sum;
}
IRC_CPP_COD_STRINGS
Attempt : 1
Total Mark : 150
Marks Obtained : 150
Section 1 : CODING
1. Problem statement :
String - Copy
Write a program to copy a string from one variable to another using string
library functions:
Answer
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char str[12];
cin>>str;
int i,l,temp;
l=strlen(str);
for(i=0;i<1/2;i++)
{
temp=str[i];
str[i]=str[l-i-1];
str[l-i-1]=temp;
}
cout<<str;
return 0;
}
2. Problem statement :
String - Reverse
Write a program to find the reverse of the given without string using string
library functions:
Answer
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char str[12];
cin>>str;
int i,l,temp;
l=strlen(str);
for(i=0;i<l/2;i++)
{
temp=str[i];
str[i]=str[l-i-1];
str[l-i-1]=temp;
}
cout<<str;
return 0;
}
3. Problem statement :
String - Compare
Write a program to find whether the given two strings are the same or not
using string library functions:
Answer
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char str1[20],str2[20];
cin>>str1>>str2;
int n;
n=strcmp(str1,str2);
if(n==0)
{
cout<<"Strings are same";
}
else
{
cout<<"Strings are not same";
}
return 0;
}
4. Problem statement :
Delete Vowels
Write a program to delete all vowels present in a string.
Answer
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char str[200];
int i;
cin>>str;
while(str[i]!='\0')
{
if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'||str[i]=='A'||str[i]=='E'||
str[i]=='I'||str[i]=='O'||str[i]=='U')
{
}
else
{
cout<<str[i];
}
i++;
}
return 0;
}
5. Problem statement :
String palindrome
Write a program to find whether the given string is a palindrome or not
without using string library functions.
Answer
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
char s[100];
cin>>s;
int l=strlen(s);
int flag=1;
for(int i=0,j=l-1;i<l/2;i++,j--)
{
if(s[i]!=s[j])
{
flag=0;
break;
}
}
if(flag==1)
cout<<"Palindrome";
else
cout<<"Not a Palindrome";
return 0;
}
6. Problem Statement:
Alternating Code
It is IPL season and the first league match of Dhilip’s favorite team,
"Chennai Super Kings". The CSK team is playing at the IPL after 2 years
and like all Dhoni lovers, Dhilip is also eagerly awaiting to see Dhoni back in
action.
After waiting in long queues, Dhilip succeeded in getting the tickets for the
big match. On the ticket, there is a letter code that can be represented as a
string of upper-case Latin letters.
Dhilip believes that the CSK Team will win the match in case exactly two
different letters in the code alternate. Otherwise, he believes that the team
might lose. Please see the note section for the formal definition of
alternating code.
You are given a ticket code. Please determine, whether CSK Team will win
the match or not based on Dhilip’sconviction. Print "YES" or "NO" (without
quotes) corresponding to the situation.
Note:
Two letters x, y where x != y are said to be alternating in a code if the code
is of the form "xyxyxy...".
Answer
// You are using GCC
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
int l=0,f=0;
getline(cin,s);
for(int i=0;s[i]!='\0';i++)
{
l++;
}
for(int i=0;i<l-2;i++)
{
if(s[i]!=s[i+2])
{
f=1;
break;
}
}
if(f==0)
{
cout<<"Yes";
}
else{
cout<<"No";
}
return 0;
}
7. Problem Statement:
Caption Contest
Exeter Caption Contest is a competition open to all writers worldwide. The
entrants will have one day to compose and submit a caption that will be
based on the theme posted on the competition page.
Robin, a creative writer had penned two captions for the contest but he
unknowingly misplaced them. After searching long, he managed to locate
his captions, but some letters in them have become unreadable. The
captions were on two very old sheets of paper, each of which originally
contained a string of lowercase English letters. The strings on both sheets
have equal lengths.
Robin would like to estimate the difference between these strings. Let's
assume that the first string is named S1, and the second S2. The
unreadable symbols are specified with the question mark symbol '?'. The
difference between the strings equals to the number of positions i, such
that S1i is not equal to S2i, where S1i and S2i denote the symbol at i th
position in S1 and S2, respectively.
Robin would like to know the minimal and the maximal difference between
the two strings if he changes all unreadable symbols to lowercase English
letters. Robin is not an expert in programming and so he needs your help
solving this problem!
Answer
// You are using GCC
#include<bits/stdc++.h>
using namespace std;
int main()
{
char s1[50],s2[50];
int max=0,min=0;
[Link](s1,50);
[Link](s2,50);
int l1=strlen(s1);
int l2=strlen(s2);
int l=l1;
for(int i=0;i<l;i++)
{
if(s1[i]!=s2[i]&&s1[i]!='?' && s2[i]!='?')
{
min++;
max++;
}
else if(s1[i]=='?'&& s2[i]=='?')
{
max++;
}
else if(s1[i]=='?'||s2[i]=='?')
{
max++;
}
}
cout<<min<<" "<<max;
return 0;
}
8. Problem Statement:
Little Authors
"Little Authors" Slogan Writing Competition was organized for School
students at Orchids Senior School. Any student who is creative and
effective in communicating ideas in short, yet powerful about any instant
topic can participate in the competition. The important guideline for the
contest is that the Slogan should contain a string where the number of
occurrences of some character in it is equal to the sum of the numbers of
occurrences of other characters in the string.
Answer
// You are using GCC
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
int c=0;
cin>>s;
int l;
l=[Link]();
for(int i=0;i<l;i++)
{
int c1=0;
for(int j=0;j<l;j++)
{
if(s[i]==s[j])
{
c1++;
}
}
if(c<c1)
{
c=c1;
}
}
if(c==(l-c))
{
cout<<s<<" :Yes";
}
else
{
cout<<s<<" :No";
}
return 0;
9. Problem Statement:
Balls for Challenge
The Circoloco Children Carnival is the City’s largest and most successful
event dedicated to children and families. The main focus at the carnival is
the workshop arena where kids can participate in engaging and
educational activities.
Charlie, a little boy accompanied by his Mom visited the fair, where he
participated in the "Balls for Challenge" activity. He was given many balls
of white and black colors. During the play, he arranged the balls into two
rows both consisting of N number of balls. These two rows of balls are
given to you in the form of strings X, Y. Both these strings consist of 'W'
and 'B', where 'W' denotes a white-colored ball and 'B' a black colored.
Other than these two rows of balls, Charlie has an infinite supply of extra
balls of each color. He wants to create another row of N balls, Z in such a
way that the sum of Hamming distance between X and Z, and hamming
distance between Y and Z is maximized.
Answer
// You are using GCC
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
string a,b;
cin>>a>>b;
int c=0;
int z=[Link]();
for(int i=0;i<z;i++)
{
if(a[i]==b[i])
{
if (a[i]=='W')
cout<<"B";
else
cout<<"W";
}
else
{
if (c%2==0)
cout<<a[i];
else
cout<<b[i];
c++;
}
}
return 0;
}
Peter found some statistics of matches which described who won the
points in order. A game shall be won by the player first scoring 11 points
except in the case when both players have 10 points each, then the game
shall be won by the first player subsequently gaining a lead of 2 points.
Could you please help Peter find out who the winner was from the given
statistics? (It is guaranteed that statistics represent always a valid, finished
match.)
Answer
// You are using GCC
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
int l,win=0,lose=0;
cin>>s;
l=[Link]();
for(int i=0;i<l;i++)
{
if(s[i]=='1')
{
++win;
}
else
{
++lose;
}
}
if(win>lose)
{
cout<<"Win";
}
else
{
cout<<"Lose";
}
return 0;
}
Answer
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char s[100];
[Link](s,100);
int i,l;
l=strlen(s);
for(i=0;i<l;i++)
{
if(s[i]>='a' && s[i]<='z')
s[i]=s[i]-32;
}
cout<<s;
return 0;
}
Answer
#include<iostream>
#include<string.h>
#include<cctype>
using namespace std;
int main()
{
char str[100];
[Link](str,100);
int i,len;
char asci;
len=strlen(str);
cout<<"String with lowercase is ";
for(i=0;i<len;i++)
{
asci=(int) str[i]+32;
cout<<asci;
}
}
Mike was given a string D of numbers containing only digits 0's and 1's. His
challenge was to make the number have all the digits the same. For that,
he should change exactly one digit, i.e. from 0 to 1 or from 1 to 0. If it is
possible to make all digits equal (either all 0's or all 1's) by flipping exactly
1 digit then he has to output "Yes", else print "No" (without quotes).
Answer
// You are using GCC
#include<bits/stdc++.h>
using namespace std;
int main()
{
char ch[50];
int i,len=0,c=0,cn=0;
cin>>ch;
for(i=0;ch[i];i++)
{
len++;
}
for(i=0;ch[i];i++)
{
if(ch[i]=='1')
{
c++;
}
else if(ch[i]=='0')
{
cn++;
}
}
if(len-c==1||len-cn==1)
{
cout<<"Yes";
}
else
{
cout<<"No";
}
return 0;
}
Answer
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1,s2;
cin>>s1>>s2;
int i,min=0;
for(i=0;s1[i];i++)
{
if(s1[i]!='?'&&s2[i]!='?')
{
if(s1[i]!=s2[i])
{
min++;
}
}
}
if(min==0)
{
cout<<"Yes";
}
else
{
cout<<"No";
}
}
Answer
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char str[100];
int i,vow=0;
[Link](str,100);
for(i=0;i<str[i];i++)
{
if(str[i]=='a'||str[i]=='A'||str[i]=='e'||str[i]=='E'||str[i]=='i'||str[i]=='I'||str[i]=='o'||
str[i]=='O'||str[i]=='u'||str[i]=='U')
{
vow++;
}
}
cout<<"Number of vowels:"<<vow;
return 0;
}
IRC_CPP_COD_POINTERS
Attempt : 1
Total Mark : 100
Marks Obtained : 100
Section 1 : CODING
1. Problem Statement:
Write a C++ program that fetches a word and prints the same using
pointers.
Answer
#include<bits/stdc++.h>
using namespace std;
int main()
{
char a[50];
cin>>a;
char *p=a;
cout<<p<<"\n";
}
2. Problem Statement:
Write a C++ program to find the modulus of two numbers using pointers.
Answer
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b;
cin>>a>>b;
int*c=&a;
int *d=&b;
int e=a%b;
cout<<a<<" "<<b<<" "<<e;
3. Problem Statement:
Write a C++ program to print the sum of array elements using pointers.
Answer
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,sum=0,*m,i;
cin>>n;
int a[n];
if(n>0 && n<=10)
{
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
m=∑
cout<<*m;
}
else
{
cout<<"Invalid";
}
4. Problem Statement:
Write a C++ program to fetch five integers in an array and print them in
reverse order using pointers.
Answer
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a[5],i,*ptr;
for(i=0;i<5;i++)
{
cin>>a[i];
}
ptr=&a[5-1];
if([Link]())
{
cout<<"Invalid";
}
else
{
for(i=0;i<5;i++)
{
cout<<" "<<*ptr--;
}
}
}
Answer
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,*p,i;
cin>>n;
int a[n];
for(i=0;i<n;i++)
{
cin>>a[i];
}
sort(a,a+n);
for(i=0;i<n;i++)
{
p=&a[i];
cout<<*p<<" ";
}
}
6. Problem Statement:
Maximum Element in an Array
Write a program to find the maximum element in an array using pointers.
Note:
Refer to the problem requirements
Answer
#include<bits/stdc++.h>
using namespace std;
int main()
{
int i,n;
cin>>n;
int a[n];
for(i=0;i<n;i++)
{
cin>>a[i];
}
sort(a,a+n);
int*e=&a[n-1];
cout<<*e<<" is the maximum element";
}
7. Problem Statement:
Write a program to count the number of vowels and consonants in a given
string using pointers
Answer
#include<bits/stdc++.h>
using namespace std;
int main()
{
char a[150];
int i,vow=0,cons=0,*m,*n;
cin>>a;
for(i=0;a[i];i++)
{
if(a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o'||a[i]=='u'||a[i]=='A'||a[i]=='E'||a[i]=='I'||
a[i]=='O'||a[i]=='U')
{
vow++;
}
else if(a[i]>='a'&&a[i]<='z'||a[i]>='A'&&a[i]=='Z')
{
cons++;
}
}
m=&vow;
n=&cons;
cout<<*m<<" "<<*n;
}
8. Problem Statement:
Write a C++ program to concatenate two arrays using pointers.
Answer
#include<iostream>
using namespace std;
int main()
{
int n1,i,j,*p,*q;
cin>>n1;
int a[n1];
for(i=0;i<n1;i++)
{
cin>>a[i];
}
int n2;
cin>>n2;
int b[n2];
for(i=0;i<n2;i++)
{
cin>>b[i];
}
p=a;
q=b;
for(i=0,j=n1;i<n2;i++,j++)
{
*(p+j) = *(q+i);
}
for(i=0;i<j;i++)
{
cout<<a[i]<<" ";
}
return 0;
}
Status : Correct Marks : 10/10
9. Problem Statement:
Write a program to replace a character with a given character in a string.
Note: Replace 'a' by '*'.
Answer
#include<bits/stdc++.h>
using namespace std;
int main()
{
char a[150],*m;
int i;
cin>>a;
for(i=0;a[i];i++)
{
if(a[i]=='a')
{
a[i]='*';
}
}
for(i=0;a[i];i++)
{
m=&a[i];
cout<<*m;
}
Answer
#include<bits/stdc++.h>
using namespace std;
int search(int,int*,int);
int main()
{
int n,m;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
cin>>m;
search(n,a,m);
return 0;
}
int search(int n,int*a,int m)
{
int l,i;
for(i=0;i<n;i++)
if(m==a[i])
{
l=1;
break;
}
if(l==1)
cout<<m<<"is found at index"<<i;
else
cout<<"Not found";
}
IRC_CPP_COD_FUNCTIONS
Attempt : 1
Total Mark : 100
Marks Obtained : 100
Section 1 : CODING
1. Problem Statement:
Expression Evaluation
where the first argument corresponds to the value of x and the second
corresponds to n respectively.
Answer
// You are using GCC
// You are using GCC
#include<iostream>
#include<cmath>
using namespace std;
int calculate(int x,int n)
{ int res=0;
for(int i=1;i<=n;i++)
{
res=res+pow(x,i);
}
return res;
}
int main()
{
int x,n;
cin>>x>>n;
cout<<calculate(x,n);
return 0;
}
2. Problem statement:
Answer
// You are using GCC
// You are using GCC
#include<iostream>
using namespace std;
void swap(int *a,int *b,int *c)
{
int t1;
t1=*a;
*a=*b;
*b=*c;
*c=t1;
}
int main()
{
int a,b,c;
cin>>a>>b>>c;
swap(&a,&b,&c);
cout<<"a = "<<a<<"\nb = "<<b<<"\nc = "<<c;
return 0;
}
3. Problem statement:
Function Requirements:
Answer
// You are using GCC
// You are using GCC
#include<iostream>
using namespace std;
void iseven(int x);
void ismultiple(int x);
int main()
{
int n;
cin>>n;
iseven(n);
return 0;
}
void iseven(int x)
{
if(x%2==0)
{
cout<<x<<" is Even number"<<endl;
ismultiple(x);
}
else
cout<<x<<" is Not even";
}
void ismultiple(int x)
{
if(x%10==0)
cout<<"Multiple of 10";
else
cout<<"Not a multiple of 10";
}
4. Problem statement:
Function Specifications:
Use the function name, return type, and the argument type as:
int find(int)
The function must return 1 if you can make a right triangle using N dots,
else return 0.
Note:
The main function is already provided and well defined. The function
mentioned above is to be defined by you to solve this problem
Answer
// You are using GCC
// You are using GCC
#include<iostream>
using namespace std;
int find(int);
int main()
{
int n;
cin>>n;
int f=find(n);
if(f==1)
cout<<"We can create Right Triagle of dots with "<<n<<" dots";
else
cout<<"We can't create Right Triagle of dots with "<<n<<" dots";
return 0;
}
int find(int n)
{
int sum=0,i=0;
while(sum<n)
{
sum=sum+i;
i++;
}
if(sum==n)
return 1;
else
return 0;
}
5. Problem Statement:
Addition Challenge
Charlie set off with great zeal to the "Kracker Jack Fun Fair 2017". There
were numerous activities in the fair, though Charlie being a Math expert,
liked to participate in the Addition Challenge.
The Challenge given to him was to find S, where S=20 +21+22+...........+2N.
He would succeed in his challenge if he manages to tell the answer for the
problem before others. He requests your help to solve the problem in a
flash.
Help Charlie to find S and win over the challenge.
In the Main function, obtain input from the user in the console and display
S by calling the findSum method
Answer
// You are using GCC
#include<iostream>
#include<cmath>
using namespace std;
int findSum(int);
int findSum(int n)
{ int S=0;
for(int i=0;i<=n;i++)
S=S+pow(2,i);
return S;
}
int main()
{
int n;
cin>>n;
cout<<findSum(n);
return 0;
}
6. Problem statement :
Factorial
Write a program to compute the factorial of a number using recursion.
Answer
// You are using GCC
// You are using GCC
#include<iostream>
using namespace std;
int fact(int n);
int main()
{
int n;
cin>>n;
cout<<"The factorial of "<<n<<" is "<<fact(n);
return 0;
}
int fact(int n)
{
if(n==0)
return 1;
else
return n*fact(n-1);
}
7. Problem statement :
GCD of 2 Numbers
Write a program to compute the GCD of 2 numbers using recursion.
Answer
#include<bits/stdc++.h>
using namespace std;
int gcd(int n,int m)
{
if(n%m==0)
return m;
else
gcd(m,(n%m));
}
int main()
{
int n,m,result;
cin>>n>>m;
result=gcd(max(m,n),min(m,n));
cout<<result;
return 0;
}
Status : Correct Marks : 10/10
8. Problem statement :
Sum of elements in Array
Write a program to compute the sum of elements in an array using
recursion.
Answer
#include<bits/stdc++.h>
using namespace std;
int sum(int a[],int n);
int main()
{
int n,i;
cin>>n;
int a[n];
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<sum(a,n);
}
int sum(int a[],int n)
{
if(n<=0)
return 0;
return sum(a,n-1)+a[n-1];
}
9. Problem Statement:
Auditions
In the Main function, obtain input from the user in the console and display
the position(original queue) of that person who is left by calling the
findPos method
Answer
#include<bits/stdc++.h>
using namespace std;
int findPos(int);
int findPos(int n)
{
if(n==1)
{
return 1;
}
return 2*findPos(int (n/2));
}
int main()
{
int n;
cin>>n;
cout<<findPos(n);
}
"Success Academy" has arranged for a entrance test for High School
students from rural villages. Those successful students of the test will be
awarded the scholarship for their IIT/JEE preparations at Success
Academy. Sunil, the co-coordinator and founder of the academy has given
one problem for the first stage of the test. The problem goes like this:
Given two integers x and n, find the number of ways to express x as sum
of n-th powers of unique natural numbers. It is given that 1 <= n <= 20.
Sunil himself has not computed the solution of the problem. Write a
recursive method to help him find the answer for the same to evaluate the
students.
In the Main function, obtain input from the user in the console and display
the number of ways to express x as sum of n-th powers of unique natural
numbers by calling the countWays method
Answer
// You are using GCC
#include<iostream>
#include<cmath>
using namespace std;
int countWays(int,int);
int checkrecursive(int x,int n,int check)
{
int val=x-pow(check,n);
if(val==0) return 1;
if(val<0) return 0;
return checkrecursive(val,n,check+1)+checkrecursive(x,n,check+1);
}
int main()
{
int x,n;
cin>>x>>n;
cout<<countWays(x,n);
return 0;
}
int countWays(int x,int n)
{
return checkrecursive(x,n,1);
}
Attempt : 1
Total Mark : 120
Marks Obtained : 120
Section 1 : CODING
1. Problem statement :
Student structure
Create a structure student with the following members
Roll Number
Five subject marks
Average
Grade
Given the five subject marks, Calculate the average and grade.
GRADE CALCULATION:
1)if avg>70 the grade will be 1
2)if avg 50 to 70 the grade will be 2
3)if avg is below 50 the grade will be 3 (Note: rn- Roll Number, s-Subjects,
avg- Average)
Answer
// You are using GCC
#include<bits/stdc++.h>
using namespace std;
struct student{
int rn;
int s[5];
int avg;
};
int main()
{
int n,sum=0,avg,gr;
cin>>n;
struct student s1[n];
int i,j;
for(i=0;i<n;i++)
{
cin>>s1[i].rn;
for(j=0;j<5;j++)
{
cin>>s1[i].s[j];
}
}
for(i=0;i<n;i++)
{
int sum=0;
for(j=0;j<5;j++)
{
sum+=s1[i].s[j];
}
avg=sum/5;
if(avg>70)
gr=1;
else if(avg>=50 && avg<=70)
gr=2;
else if(avg<50)
gr=3;
cout<<s1[i].rn<<" ";
for(j=0;j<5;j++)
{
cout<<s1[i].s[j]<<" ";
}
cout<<avg<<" "<<gr<<endl;
}
}
2. Problem Statement:
Write a program to add two distances (in inch-feet) system using
structures.
Answer
// You are using GCC
#include<bits/stdc++.h>
using namespace std;
struct distance{
int feet;
float inches;
};
int main()
{
struct distance d[2];
float sum1,sum2;
for(int i=0;i<2;i++)
{
cin>>d[i].feet>>d[i].inches;
}
for(int i=0;i<2;i++)
{
sum1+=d[i].feet;
sum2+=d[i].inches;
}
if(sum2>12)
{
sum1++;
sum2=sum2-12;
}
cout<<" Sum: "<<sum1<<" feet, "<<sum2<<" inches. ";
}
Answer
// You are using GCC
#include<bits/stdc++.h>
using namespace std;
struct time{
int hour;
int min;
int sec;
};
void computeTime(struct time, struct time, struct time*);
int main()
{
struct time t1,t2,difference;
cin>>[Link]>>[Link]>>[Link];
cin>>[Link]>>[Link]>>[Link];
if([Link]>23|| [Link]>23||[Link]>=60|| [Link]>=60||[Link]>=60||[Link]>=60
||[Link]<0||[Link]<0||[Link]<0||[Link]<0||[Link]<0||[Link]<0)
{
cout<<"-1";
return 0;
}
computeTime( t1,t2,&difference);
cout<<"Difference:
"<<[Link]<<":"<<[Link]<<":"<<[Link]<<"-"<<[Link]<<":"<<[Link]<<":"<<[Link]<<"="
<< [Link]<<":"<<[Link]<<":"<<[Link];
return 0;
}
void computeTime(struct time t1, struct time t2, struct time*difference)
{
if([Link]>[Link])
{
--[Link];
[Link] = [Link]+60;
}
difference->sec=[Link];
if([Link]>[Link])
{
--[Link];
[Link]= [Link]+60;
}
difference->min= [Link];
difference->hour=[Link];
}
4. Problem Statement:
Write a program having a structure employee with members Employee
Name, Employee number, Designation, Department, BasicSalary, DA, HRA,
PF, and totalSalary.
Prompt the user to enter employee details for n employees and calculate
totalSalary as
Finally print the Salaryslip for an employee based on the given employee
number
Answer
// You are using GCC
#include<bits/stdc++.h>
using namespace std;
struct employee{
string name;
int num;
string designation;
string department;
int bs;
int da;
int hra;
int pf;
int total;
};
int main()
{
struct employee a[10];
for(int i=0;i<10;i++)
{
cin>>a[i].name;
cin>>a[i].num;
cin>>a[i].designation;
cin>>a[i].department;
cin>>a[i].bs;
cin>>a[i].da;
cin>>a[i].hra;
cin>>a[i].pf;
a[i].total=a[i].bs+a[i].da+a[i].hra+a[i].pf;
}
int number;
cin>>number;
for(int i=0;i<10;i++)
{
if(a[i].num==number)
{
cout<<"SALARY SLIP"<<endl;
cout<<"Employee Name : "<<a[i].name<<endl;
cout<<"Employee Total Salary :"<<a[i].total;
return 0;
}
}
cout<<"No Records Found for given Employee Number!";
}
5. Problem Statement:
Create a structure named DEPT with the following fields: Name, emp-id,
years_of_experience, and Basic salary. Define an array of structures for ‘n’
employees and check the following constraints and print the results.
- Increase 10% to the salaries of those employees who have worked for
10 years or more
- Increase 5% to the salaries of those employees who have experienced
between 5 to 9 years.
- Increase 2% to the salaries of those employees who have experienced
between 1 to 4 years.
Answer
// You are using GCC
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
struct DEPT{
string name;
int emp_id;
int yoe;
int basic_salary;
};
int main()
{
int n;
cin>>n;
struct DEPT a[n];
for(int i=0;i<n;i++)
{
cin>>a[i].name;
cin>>a[i].emp_id;
cin>>a[i].yoe;
cin>>a[i].basic_salary;
if(a[i].yoe>=10)
a[i].basic_salary+=a[i].basic_salary*10/100;
else if(a[i].yoe>=5&&a[i].yoe<=9)
a[i].basic_salary+=a[i].basic_salary*0.05;
else if(a[i].yoe>=1&&a[i].yoe<=4)
a[i].basic_salary+=a[i].basic_salary*0.02;
}
for(int i=0;i<n;i++)
{
cout<<"Employee Name: "<<a[i].name<<endl;
cout<<"Employee Id : "<<a[i].emp_id<<endl;
cout<<"years of experience : "<<a[i].yoe<<endl;
cout<<" salary : " <<a[i].basic_salary<<endl;
cout<<endl;
}
}
6. Problem statement:
Write a program to demonstrate union to store and display employee
details such as Employee ID, Employee Name, Date of birth, Date of
Joining, and Current salary.
Answer
// You are using GCC
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
struct data{
int id;
string name;
string dob;
string doj;
float salary;
};
int main()
{
struct data a;
cin>>[Link];
cin>>[Link];
cin>>[Link];
cin>>[Link];
cin>>[Link];
cout<<"ID : "<<[Link]<<endl;
cout<<"Name : "<<[Link]<<endl;
cout<<"DOB : "<<[Link]<<endl;
cout<<"DOJ : "<<[Link]<<endl;
cout<<"Salary : "<<[Link]<<endl;
return 0;
}
Status : Correct Marks : 10/10
7. Problem statement :
Book
Create a union named book with the following members
Name
Price
In the main method, get the inputs from the user and print them.
Answer
// You are using GCC
#include<iostream>
#include<string.h>
using namespace std;
union sub{
char name[100];
int n;
};
int main()
{
union sub s,g;
cin>>[Link];
cin>>g.n;
cout<<[Link]<<" "<<g.n;
}
8. Problem Statement:
In an online grocery shop, customers want to purchase multiple items.
Create a structure to store the Item code, Brand name, Item Name,
Quantity, Price of the product. Generate the Bill number, Display the
purchased product, name, amount and quantity, and the total bill amount.
a. Write a function MESSAGE() to alert the customer with the product
name if the rate of a product is more than Rs.1000.
b. Write a function VOUCHER() to generate the voucher for Rs.200 if the bill
amount is greater than Rs.10000.
Answer
// You are using GCC
#include<iostream>
using namespace std;
struct store{
int billno;
string bname;
string iname;
int quantity;
int price;
int h;
};
int main()
{
int n,total=0;
cin>>n;
struct store a[n];
for(int i=0;i<n;i++)
{
cin>>a[i].billno>>a[i].bname>>a[i].iname>>a[i].quantity>>a[i].price;
}
for(int i=0;i<n;i++)
{
a[i].h = a[i].quantity * a[i].price;
if(a[i].h>1000)
cout<<a[i].iname<<" costs more than 1000"<<endl;
total=total+a[i].h;
}
cout<<total<<endl;
if(total>10000)
cout<<"You have won a voucher of Rs.200"<<endl;
else
cout<<"No voucher"<<endl;
return 0;
}
Rule:
The file name should be [Link].
Answer
#include <iostream>
#include <fstream>
using std::ofstream;
using std::ifstream;
using namespace std;
int main()
{
fstream file;
// You are using GCC
[Link]("[Link]",ios::out);
string str="Today C++ is the most widely used System Programming Language.
\nMost of the state of the art software have been implemented using C++.
\nEasy to learn\nStructured language\nIt produces efficient programs.\nIt can
handle low-level activities.\nIt can be compiled on a variety of computers.";
ofstream fout;
[Link]("[Link]");
fout<<str<<endl;
[Link]();
cout<<"File content:\n";
while(![Link]())
{
file>>str;
cout<<str;
}
[Link]();
//[Link]();
return 0 ;
}
Answer
#include <iostream>
#include <fstream>
using std::ofstream;
using namespace std;
int main()
{
// You are using GCC
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int max=0,min=a[0];
for(int i=0;i<n;i++)
{
if(max<a[i])
max=a[i];
}
for(int i=0;i<n;i++)
{
if(a[i]<min)
{
min=a[i];
}
}
fstream file;
[Link]("[Link]",ios::out);
ofstream out;
[Link]("[Link]");
out<<min<<max;
[Link]();
while(file)
{
file>>max>>min;
cout<<max<<" "<<min;
}
[Link]();
return 0;
}
Write a program to read the content from the file([Link]) and count the
number of uppercase and lowercase letters.
Rules:
The input file should be named "[Link]".The input file should be named
"[Link]".
Answer
#include<iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream fout;
[Link]("[Link]");
// You are using GCC
char str[300]="Time is a Great Teacher BUT Unfirtunately It Kilss All Its Pupils\n";
fout<<str;
[Link]();
ifstream fin;
[Link]("[Link]");
int ucount=0,lcount=0;
char ch;
while(![Link]())
{
[Link](ch);
if(ch>='A'&&ch<='Z')
ucount++;
if(ch>='a'&&ch<='z')
lcount++;
}
cout<<"Number of uppercase letters in file are"<<ucount<<endl;
cout<<"Number of lowercase letters in file are"<<lcount<<endl;
[Link]();
return 0;
}
Rule:
The file name should be [Link].
Answer
#include <iostream>
#include <fstream>
using std::ofstream;
using std::ifstream;
using namespace std;
int main()
{
fstream file;
// You are using GCC
[Link]("[Link]",ios::out);
string str="Today C++ is the most widely used System Programming Language.
\nMost of the state of the art software have been implemented using C++.
\nEasy to learn\nStructured language\nIt produces efficient programs.\nIt can
handle low-level activities.\nIt can be compiled on a variety of computers.\n";
ofstream fout;
[Link]("[Link]");
fout<<str<<endl;
[Link]();
cout<<"File content:\n";
while(![Link]())
{
file>>str;
cout<<str;
}
ifstream ifile;
int count;
[Link]("[Link]");
while(getline(ifile,str))
{
count++;
}
cout<<"Numbers of lines in the file : "<<count-1;
[Link]();
[Link]();
return 0 ;
}
Status : Correct Marks : 10/10
Sri Krishna College of Technology
Attempt : 1
Total Mark : 100
Marks Obtained : 100
Section 1 : CODING
1. Problem Statement:
Create a class named Input with two data members - One string and One
integer.
In the main method, create an object for the class. Get the inputs from the
user and print them.
Answer
#include<bits/stdc++.h>
using namespace std;
class obj
{
public:
string name;
int num;
void get()
{
cin>>name>>num;
}
void put()
{
cout<<num<<":"<<name;
}
};
int main()
{
obj n;
[Link]();
[Link]();
}
2. Problem Statement:
Calendar
Create a class DateTime with the following member functions.
Hours, Minutes, Date, Month, and Year - Integer
Define a member function init() - to get the class variables
Define a member function display() - to print the class variables.
In the main method, create an object for the class and call the necessary
methods.
Answer
#include<bits/stdc++.h>
using namespace std;
class func
{
public:
int hours,mins,date,mon,year;
void get()
{
cin>>hours>>mins>>date>>mon>>year;
}
void put()
{
if(date<0||date>31||mon<0||mon>12||year<0||year>9999||hours>24||mins>60||
mins<0)
{
cout<<"Invalid";
}
else
{
cout<<"Date:"<<date<<"-"<<mon<<"-"<<year<<endl;
cout<<"Time:"<<hours<<" hrs"<<mins<<" mins"<<endl;
cout<<"Total mins:"<<(hours*60+mins);
}
}
};
int main()
{
func n;
[Link]();
[Link]();
}
3. Problem Statement:
Create a class named Demo with two static integer variables as its private
attributes and the following member functions.
get() - to get the values of x and y
fun() - to print the value of x and y
Default values of x and y are 10 and 20.
In the main method, create an object for the Demo class and call the
methods.
Answer
#include<bits/stdc++.h>
using namespace std;
class demo
{
public:
static int X,Y;
void get()
{
cin>>X>>Y;
}
void put()
{
cout<<"value of X: "<<X<<endl;
cout<<"value of Y: "<<Y<<endl;
}
};
int demo::X=10;
int demo::Y=20;
int main()
{
demo n;
[Link]();
[Link]();
[Link]();
}
4. Problem Statement:
Create a class named Box with length as its private attribute.
Create a constructor and assign the length as 0.
Create a member function get() - get the value of length as input.
Include a Friend Function
int printVolume(Box) - calculate the volume of the box and return it.
In the main method, create an object for Box and call the necessary
methods.
Answer
#include<bits/stdc++.h>
using namespace std;
class box
{
public:
int length;
void get()
{
cin>>length;
}
void put()
{
cout<<length*length*length;
}
};
int main()
{
box n;
[Link]();
[Link]();
}
5. Problem Statement:
Write a program to find the area of the wall. Create a class Wall with the
following private attributes
length - double
height - double
Include parameterized constructor Wall(double length, double height) and
a method calculateArea() which returns the area of the wall. In the main
method get the length and breadth of two walls from the user.
Answer
#include<bits/stdc++.h>
using namespace std;
class wall
{
private:
double l,h;
double lenght ,height;
public:
wall(double l,double h)
{
lenght=l;
height=h;
}
void calculatearea(int i)
{
cout<<"Area of Wall "<<i<<": "<<lenght*height<<endl;
}
};
int main()
{
double lenght,height;
for(int i=1;i<3;i++)
{
cin>>lenght>>height;
wall a(lenght,height);
[Link](i);
}
return 0;
6. Problem Statement:
Write a program to get the employee id, name, and salary of N professors
and print the details of the professors whose salary is greater or equal to
20000.
Create a class Professor with the following public attributes,
id - int
name - string
salary - int
Include default constructor Professor() and parameterized constructor
Professor (int id, string name, int salary) and a method display() which
prints the details.
In the main method create N objects for the class Professor and call the
necessary functions.
Answer
#include<iostream>
using namespace std;
class professor
{
public:
int id;
string name;
int salary;
int a,c,n;
string b;
professor()
{
salary=0;
id=0;
}
professor(int id,string nae,int salary)
{
a=id;
b=name;
c=salary;
}
void display()
{
cout<<id<<" "<<name<<" "<<salary<<endl;
}
};
int main()
{
int n,i;
cin>>n;
professor N[n];
for(int i=0;i<n;i++)
{
cin>>N[i].id>>N[i].name>>N[i].salary;
}
for(int i=0;i<n;i++)
{
if(N[i].salary>=20000)
{
N[i].display();
}
}
}
7. Problem Statement:
Constructor Overloading:
Create a class Overloading with the following private attributes,
name - string
Day - string
temp - int
Include default constructor Overloading() with default values
name="Argentina" and temp=29 and Parameterized constructor1
Overloading(string name, int temp) and parameterized constructor2
Overloading(string name, string day, int temp). In the main method call the
overloaded constructors with appropriate values.
Answer
// You are using GCC
#include<iostream>
using namespace std;
class Overloading
{
public:
string name,day;
int temp;
char c;
Overloading()
{
name="Argentina";
temp=29;
cout<<name<<" Yesterday Temperature: "<<temp<<"°"<<endl;
}
Overloading(string name,int temp)
{
cout<<name<<" Today Temperature: "<<temp<<"°"<<endl;
}
Overloading(string name,string day,int temp)
{
cout<<name<<" "<<day<<"Temperature: "<<temp<<"°"<<endl;
}
};
int main()
{
Overloading o;
Overloading o1();
string name,day;
int temp;
cin>>name>>temp;
Overloading o2(name,temp);
cin>>name>>day>>temp;
Overloading o3(name,day,temp);
8. Problem Statement:
Constructor Overloading:
Create a class Box with the following public attributes
double width
double height
double depth
Include default constructor Box()[default value is 0], parameterized
constructor1 Box(double w, double h, double d), Parameterized
constructor2 Box(double len) and a method double volume() which return
the volume
Answer
// You are using GCC
#include<iostream>
#include<cmath>
using namespace std;
class Box
{
public:
double width,height,depth;
Box()
{
//width=height=depth=0;
}
void get()
{
cin>>width>>height>>depth;
}
Box(double w,double h,double d)
{
/*width=w;
height=h;
depth=d;*/
}
Box(double len)
{
cout<<"Volume of mycube is "<<pow(len,3);
}
double volume()
{
return width*height*depth;
}
};
int main()
{
double len;
Box b;
[Link]();
cin>>len;
9. Problem Statement:
A Multiplication Game
John and Michael play the game of multiplication by multiplying an integer
p by one of the numbers 2 to 9. John always starts with p = 1 and
multiplies it by 1, and passes the result to Michael. Then, Michael
multiplies the number by 2 and sends the result to John, then John
multiplies by 3, and so on. Before a game starts, they draw an integer N
and the winner is the one who first reaches p ≥ n.
Create a class that has two functions:
1) A function to perform the multiplication operation
2) The main()
Answer
// You are using GCC
#include<iostream>
using namespace std;
class game
{
public:
int p=1,q=1;
int n;
void mul()
{
while(p<n)
{
p=p*q;
q++;
}
if(q%2==0)
cout<<n<<" John wins";
else
cout<<n<<" Michael wins";
}
};
int main()
{
game g;
cin>>g.n;
[Link]();
}
Answer
// You are using GCC
#include<iostream>
using namespace std;
class Distance
{
public:
int speed,time;
void get()
{
cin>>speed>>time;
}
float calcDistance()
{
return speed*time;
}
};
int main()
{
Distance s;
[Link]();
cout<<[Link]()<<".00";
}
Attempt : 1
Total Mark : 50
Marks Obtained : 50
Section 1 : CODING
1. Problem Statement:
Coding Contest
Sunrise Basket founder has decided to organize a fun event at your
college. The event coordinator has announced a coding contest for
creating the application for the Contest. The Best application would be
used for the fair and the developer gets a cash prize. You are a well-versed
and aspiring Programmer in your college. Many programmers have
enrolled themselves for the contest and you are one of them. Every
contestant is provided with a Schema diagram of the Fair. Get yourself
acquainted with Schema and brace yourself for the challenge!!!.
As a part of this, the Application requires a user prompt to create a new
Item type. Hence create an ItemType class with the following attributes.
name (String)deposit(double)costPerDay(double)Include appropriate
Getters and Setters for the class and also include a method "void display()"
to display the output shown in the sample output. In the main method get
input from the user and display it.
Note: Bind all the data members and member functions inside the class.
Answer
#include<iostream>
using namespace std;
class itemtype
{
public:
string name;
double dep,cpd;
void get()
{
cin>>name>>dep>>cpd;
}
void display()
{
cout<<"Name : "<<name<<endl;
cout<<"Deposit Amount : "<<dep<<endl;
cout<<"Cost per day : "<<cpd<<endl;
}
};
int main()
{
itemtype i;
[Link]();
[Link]();
return 0;
2. Problem Statement:
Student Mark Calculation
Write a program to create a Student class with the following attributes:
int rollno,
int mark1,
int mark2,
int mark3.
Create an array of objects for the above class. In the main class calculate
and print the following.
Total marks obtained by each [Link] highest mark in each subject
with the roll number of the student who scored [Link] student who
obtained the highest total [Link]: Bind all the data members and
member functions inside the class.
Answer
// You are using GCC
#include<iostream>
using namespace std;
class Student
{
private:
int rollno,m1,m2,m3;
public:
void studentmark(int ar[][5],int n)
{
int r,i,j,t=0,t1,r1;
for(i=1;i<4;i++)
{
t=0;
for(j=0;j<n;j++)
{
if(ar[j][i]>t)
{
t=ar[j][i];
r=ar[j][0];
}
}
cout<<r<<" "<<t<<endl;
}
t1=0;
for(i=0;i<n;i++)
{
if(ar[i][4]>t1)
{
t1=ar[i][4];
r1=ar[i][0];
}
}
cout<<r1<<" "<<t1;
}
};
struct stdt
{
int r,ma1,ma2,ma3;
};
int main()
{
int n,i;
cin>>n;
int a[n][5];
stdt s[n];
Student m;
for(i=0;i<n;i++)
{
cin>>s[i].r>>s[i].ma1>>s[i].ma2>>s[i].ma3;
a[i][0]=s[i].r;
a[i][1]=s[i].ma1;
a[i][2]=s[i].ma2;
a[i][3]=s[i].ma3;
a[i][4]=s[i].ma1+s[i].ma2+s[i].ma3;
cout<<a[i][4]<<endl;
}
[Link](a,n);
return 0;
}
3. Problem Statement:
Write a program to find that whether the given number(x) is even or odd if
it is even then print the cube(x)+square(x) else print cube(x)-square(x).
Create a base class with a virtual function void print(). print the result by
implementing this virtual function in the derived class.
Answer
#include<iostream>
using namespace std;
class number
{
public:
int n;
void get()
{
cin>>n;
}
void put()
{
if(n%2==0)
{
cout<<(n*n*n)+(n*n);
}
else
{
cout<<(n*n*n)-(n*n);
}
}
};
int main()
{
number n;
[Link]();
[Link]();
}
4. Problem Statement:
Alphabetics Game:
You have to enter four letters for each uppercase letter you will get 10
marks and for each lowercase letter, you will get -5 marks.
Write a program to calculate the total score.
Create a base class with a virtual method void game(). Define this method
in the Derived class and calculate the total score.
Answer
#include<iostream>
using namespace std;
class base
{
public:
virtual void game()=0;
};
class derived:public base
{
public:
char a,b,c,d;
int s=0;
void game()
{
if(a>='A'&&a<='Z')
{
s=s+10;}
else
{
s=s-5;
}
if(b>='A'&&b<='Z')
{
s=s+10;
}
else
{
s=s-5;
}
if(c>='A'&&c<='Z')
{
s=s+10;
}
else
{
s=s-5;
}
if(d>='A'&&d<='Z')
{
s=s+10;
}
else
{
s=s-5;
}
cout<<"Score : "<<s;}
};
int main()
{
derived dd;
cin>>dd.a>>dd.b>>dd.c>>dd.d;
[Link]();
return 0;
}
5. Problem Statement:
Create a base class named operationsBase with the following four virtual
functions
void addition()
void subtraction()
void multiplication()
void division()
Create a derived class named operationsDerived that extends
operationsBase with a and b as its private attributes and override the
virtual functions.
Answer
#include<iostream>
using namespace std;
class base
{
public:
int n,m;
void get()
{
cin>>n>>m;
}
void add()
{
cout<<n+m<<" " ;
}
void sub()
{
cout<<n-m<<" " ;
}
void mul()
{
cout<<n*m<<" ";
}
void divi()
{
cout<<n/m<<" " ;
}
};
int main()
{
base b;
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
IRC_CPP_COD_INHERITANCE 1
Attempt : 1
Total Mark : 50
Marks Obtained : 50
Section 1 : CODING
1. Problem Statement:
Write a program by creating a class Bicycle as a base class with a number
of gears and speed of bicycle as integer attributes and create a class
called MountainBike, a derived class that extends Bicycle class with an
attribute seat height as an integer. In the main method, Obtain the inputs
and display the same.
Answer
#include<iostream>
using namespace std;
class bicycle
{
public:
int gears,speed,heigth;
void get()
{
cin>>gears>>speed>>heigth;
}
void put()
{
cout<<gears<<speed<<heigth;
}
};
int main()
{
bicycle b;
[Link]();
[Link]();
}
2. Problem Statement:
Create a class named Rectangle with the following protected attributes.
width as Integerheight as IntegerCreate a display() method, to print the
width and height of the rectangle separated by space.
Answer
#include<iostream>
using namespace std;
class rectangle
{
public:
int width,height;
void get()
{
cin>>width>>height;
cout<<width<<height;
}
void put()
{
cout<<width*height;
}
};
int main()
{
rectangle r;
[Link]();
[Link]();
}
3. Problem Statement:
Write a program with two classes Vehicle as the base class and Car as the
derived class, which inherits the properties from the Vehicle class for
calculating the speed. The base class fetches the input as a float value
whereas the derived class calculates and prints the output as a float value.
Note: Use public inheritance.
Answer
#include<iostream>
using namespace std;
class vehicle
{
public:
float dis,tim;
void get()
{
cin>>dis>>tim;
}
void put()
{
cout<<"Speed of car: "<<dis/tim <<" km/hr";
}
};
int main()
{
vehicle v;
[Link]();
[Link]();
}
Status : Correct Marks : 10/10
4. Problem Statement:
Create a base class Shape with two integer data members width and
height. Create int getArea() as virtual function and setWidth(int w),
setHeight(int h) as method.
Derive Rectangle and Triangle class from Shape class. Implement
getArea() function in derived class to calculate the area.
Part of the main function is implemented. Complete the remaining code.
Answer
#include<iostream>
using namespace std;
class shape
{
public:
int width,height;
void get()
{
cin>>width>>height;
}
void put()
{
cout<<"Total Rectangle area: "<<width*height<<endl;
cout<<"Total Triangle area: "<<width*height/2<<endl;
}
};
int main()
{
shape s;
[Link]();
[Link]();
}
5. Problem Statement:
You are given three classes A, B and C. All three classes implement their
own version of func.
In class A, func multiplies the value passed as a parameter by 2
In class B, func multiplies the value passed as a parameter by 3
In class C, func multiplies the value passed as a parameter by 5
Implement class D:
You need to modify the class D and implement the function update_val
which sets D's val to new_val by manipulating the value by only calling the
func defined in classes A, B and C.
It is guaranteed that new_val has only 2,3 and 5 as its prime factors.
Answer
// You are using GCC
// You are using GCC
#include<iostream>
using namespace std;
class A
{
public:
int func1(int x)
{
if(x%2==0) return x/2;
else return x;
}
};
class B
{
public:
int func2(int x)
{
if(x%3==0) return x/3;
else return x;
}
};
class C
{
public:
int func3(int x)
{
if(x%5==0) return x/5;
else return x;
}
};
class D :public A,B,C
{
public:
int val=val,check1=0,check2=0,check3=0,temp=0;
void fun_main(int n)
{
val=n;
while(val!=1)
{
temp=val;
val=func1(val);
if(temp!=val) check1++;
if(val==1) break;
temp=val;
val=func2(val);
if(temp!=val) check2++;
if(val==1) break;
temp=val;
val=func3(val);
if(temp!=val) check3++;
}
}
void display(int n)
{
cout << "Value = " << n << endl;
cout << "A's func called " << check1 <<" times" << endl;
cout << "B's func called " << check2 << " times" << endl;
cout << "C's func called " << check3 << " times"<< endl;
}
};
int main()
{
int n;
cin>>n;
D d;
d.fun_main(n);
[Link](n);
return 0;
}
IRC_CPP_COD_POLYMORPHISM 1
Attempt : 1
Total Mark : 50
Marks Obtained : 50
Section 1 : CODING
1. Problem Statement:
Create two functions with the same name "add"
int add(int, int) - that takes two integer inputs as parameters and adds
them.
float add(float, float) - that takes two float values as parameters and adds
them.
In the main method, get the inputs and pass them to the functions.
Answer
#include<iostream>
#include<iomanip>
using namespace std;
class calculate
{
public:
int add(int a,int b)
{
return a+b;
}
float add(float i,float j)
{
return i+j;
}
};
int main()
{
calculate c;
int a,b;
cin>>a>>b;
cout<<"Integer:"<<[Link](a,b)<<endl;
float i,j;
cin>>i>>j;
cout<<"Float:"<<fixed<<setprecision(2)<<[Link](i,j)<<endl;
}
2. Problem Statement:
Create a Parent class that consists of two methods m1 and m2.
m1 doesn't take any arguments and it just prints from a parent.
m2 takes an integer value as a parameter and prints the same.
Create a child class that extends the parent class and overrides its
methods.
m1 doesn't take any arguments and it just prints from the child.
m2 takes an integer value as a parameter and prints the same.
In the main class, create objects for the above classes and call the
corresponding methods.
Answer
#include<iostream>
using namespace std;
class Parent
{
public:
void m1()
{
cout<<"From parent m1()"<<endl;
}
void m2(int a)
{
cout<<a<<endl;
}
};
class Child:public Parent
{
public:
void m1()
{
cout<<"From child m1()"<<endl;
}
void m2(int b)
{
cout<<b<<endl;
}
};
int main()
{
int a,b;
cin>>a>>b;
Parent obj1;
Child obj2;
obj1.m1();
obj1.m2(a);
obj2.m1();
obj2.m2(b);
}
3. Problem Statement:
Function Overloading
Write a program to implement function overloading.
Ram is given two or three inputs as an integer, if he has two integers then
add the two numbers. If he has three inputs, then multiply the three
numbers.
Function Header:
public void fun1(int a,int b,int c)
public void fun1(int a,int b)
Answer
#include<iostream>
using namespace std;
class calculate
{
public:
void fun1(int a,int b, int c)
{
cout<<a*b*c;
}
void fun1(int a,int b)
{
cout<<a+b;
}
void fun1()
{
cout<<"Invalid Input";
}
};
int main()
{
int n;
cin>>n;
int z[n];
for(int i=0;i<n;i++)
{
cin>>z[i];
}
calculate s;
if(n>=4)
{
s.fun1();
}
else if(n==3)
{
s.fun1(z[0],z[1],z[2]);
}
else if(n==2)
{
s.fun1(z[0],z[1]);
}
return 0;
4. Problem Statement:
Operator Overloading:
C++ provides an option to overload operators. When placed between
integer operands, a single operator ‘+’ adds them and concatenates them
when placed between string operands.
Write a program to overload the operator('+') to compute the sum of
complex numbers.
Answer
#include<iostream>
using namespace std;
class complex
{
private:
int real,imag;
public:
complex(int r=0,int i=0)
{
real = r; imag =i;
}
complex operator + (complex const &obj)
{
complex res;
[Link] = real + [Link];
[Link] = imag + [Link];
return res;
}
void print()
{
cout<<real<<" + i"<<imag<<endl;
}
};
int main()
{
int a,b,c,d;
cin>>a>>b>>c>>d;
complex c1(a,b),c2(c,d);
complex c3=c1+c2;
[Link]();
return 0;
}
5. Problem Statement:
Write a program to illustrate dynamic polymorphism, and create two
classes Vehicle and Motorbike. Motorbike inherits the Vehicle class.
Create a method move() in the base class that takes a string as input and
prints them.
Override the method move() in the derived class that also takes a string as
input and prints them (Overrides the method in base class).
Answer
#include<iostream>
using namespace std;
class vehicle
{
public:
string s1,s2;
void get()
{
cin>>s1>>s2;
}
void put()
{
cout<<"Best "<<s1<<endl;
cout<<"Good "<<s2<<endl;
}
};
int main()
{
vehicle c;
[Link]();
[Link]();
}
IRC_CPP_COD_EXCEPTION HANDLING
Attempt : 1
Total Mark : 80
Marks Obtained : 80
Section 1 : CODING
1. Problem Statement:
Divide by Zero Error:
Answer
#include<iostream>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
try
{
if(m==0)
{
throw "Divide by Zero Error";
}
else
{
cout<<n/m;
}
}
catch(const char*ch)
{
cout<<ch;
}
}
2. Problem Statement:
Driving License
Check whether the person is eligible for a driving license or not. If the age
is less than 18, throw an exception and print "Invalid Age" and quit. If the
score is less than 40, throw an exception and print "You should get at least
40 marks" and quit. Else print "Passed".
Answer
#include <iostream>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
cout<<n<<" "<<m<<endl;
try
{
if(n<18)
{
throw "Invalid age";
}
else if (m<40)
{
throw "You should get atleast 40 marks";
}
else
throw "Passed";
}
catch (const char*ch)
{
cout<<ch;
}
}
3. Problem Statement:
Data Type Exception
Get an integer input,
If the input is 1, Throw an Integer exception.
If the input is 2, Throw a Character exception.
If the input is 3, Throw a double exception.
Answer
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
try
{
if(n==1)
{
throw "Integer exception caught.";
}
else if(n==2)
{
throw "Character exception caught.";
}
else if(n==3)
{
throw "Double exception caught.";
}
}
catch(const char*ch)
{
cout<<ch;
}
}
4. Problem Statement:
Write a program to validate the given mail id using Exception handling.
Answer
#include<iostream>
using namespace std;
int main()
{
string mail;
cin>>mail;
string t=".com";
try
{
if([Link](t)==-1)
{
throw "Exception Caught...\nInvalid Email Id!!!";
}
else
{
throw "Email Id is Valid.";
}
}
catch(const char*ch)
{
cout<<ch;
}
}
Answer
#include<iostream>
using namespace std;
int main()
{
string mb;
cin>>mb;
cout<<mb<<endl;
try
{
if([Link]()==10)
{
throw "The entered mobile number is valid";
}
else
{
throw "The mobile number should have 10 digits";
}
}
catch(const char*ch)
{
cout<<ch;
}
}
6. Problem Statement:
Arithmetic Exception
An exception is an unwanted or unexpected event, which occurs during the
execution of a program i.e at runtime, it disrupts the normal flow of the
program. For example, there are 10 statements in your program and there
occurs an exception at statement 5, rest of the code will not be executed
i.e. statement 6 to 10 will not run. If we perform exception handling, rest of
the statement will be executed. That is why we use exception handling.
For practice in exception handling, obtain the cost for 'n' days of an item
and n as input and calculate the cost per day for the item. In case, zero is
given as input for n, an arithmetic exception is thrown, handle the
exception and prompt the user accordingly (Refer sample I/O).
In the Main method, obtain input from the user and store the values in int
type. Handle exception if one occurs.
Answer
#include<iostream>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
try
{
if(m==0)
{
throw "Cannot divide by zero";
}
else
{
cout<< n/m;
}
}
catch(const char*ch)
{
cout <<ch;
}
}
7. Problem Statement:
Input Mismatch Exception
Input Mismatch exception occurs when an input of a different data type is
given other than the required. In common practice, it occurs when a String
or double datatype is given for an int datatype. Let's handle this exception
for practice. Obtain int-type input from the user. Display the obtained input
if no exception occurs. If an exception occurs, prompt the user as
specified in Sample I/O. In the Main method, obtain integer input from the
user and perform actions as specified above
Answer
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
try
{
if(n=n)
{
cout<<n;
}
else
{
throw "Input mismatch Exception occured while reading the value";
}
}
catch(const char*c)
{
cout<<c;
}
}
8. Problem Statement:
Write a program to display the marks obtained by a student in Physics,
Chemistry, and Mathematics for calculating cut-off marks. The maximum
mark a student can score in a subject is 100. Handle the exception if the
mark entered by the student is above 100 or below 0. Obtain inputs from
the user and display the output in the console.
Answer
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
string m;
cin>>m;
cin>>a>>b>>c;
try
{
if(a<=100&&b<=100&&c<=100)
{
cout<<a<<endl;
cout<<b<<endl;
cout<<c<<endl;
}
else
{
throw "Invalid Marks";
}
}
catch(const char*c)
{
cout<<c;
}
}
IRC_CPP_COD_STL 1
Attempt : 1
Total Mark : 60
Marks Obtained : 60
Section 1 : CODING
1. Problem Statement:
Write a program that calculates the sum of unique elements of an integer
STL List.
Answer
#include<bits/stdc++.h>
using namespace std;
int main()
{
list <int>v;
int element;
int n,i,sum=0;
cin>>n;
if(n>15)
{
cout<<"-1";
}
else
{
for(i=0;i<n;i++)
{
cin>>element;
v.push_back(element);
}
[Link]();
[Link]();
for(auto x:v)
{
sum+=x;
}
cout<<"Sum of unique elements:"<<sum;
}
return 0;
}
2. Problem Statement:
Using the sort algorithm of STL, write a program that sorts a user-defined
character array in ascending order.
Answer
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector<char>v;
char element;
int n,i;
cin>>n;
for(i=0;i<n;i++)
{
cin>>element;
v.push_back(element);
}
cout<<"Before sorting: ";
for(auto x:v)
{
cout<<x<<" ";
}
cout<<endl;
sort([Link](),[Link]());
cout<<"After sorting: ";
for(auto x:v)
{
cout<<x<<" ";
}
3. Problem Statement:
Write a program using vectors that sort numbers in descending order.
Answer
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector<int >v;
int element;
int n,i;
cin>>n;
for(i=0;i<n;i++)
{
cin>>element;
v.push_back(element);
}
cout<<"Sorted:"<<endl;
sort([Link](),[Link](),greater<>());
for(auto x:v)
cout<<x<<" ";
return 0;
4. Problem Statement:
Write a program that creates two pairs each having a string and int object.
Concatenate the string objects and find the product of the integer objects
in the two pairs.
Answer
#include<bits/stdc++.h>
using namespace std;
int main()
{
pair<string,int>p1,p2;
int p1no,p2no;
string p1name,p2name;
cin>>p1name>>p1no>>p2name>>p2no;
if([Link]())
{
cout<<"-1";
}
else
{
cout<<p1name<<p2name<<endl;
cout<<p1no*p2no;
}
}
5. Problem Statement:
Write a code that prints the number of characters except for the input
character without using loops.
Hint: Use count_if function template.
Answer
#include<bits/stdc++.h>
using namespace std;
int main()
{
char c[50],ch;
cin>>c>>ch;
int n=strlen(c);
cout<<count_if(c,c+n,not1(bind1st(equal_to<char>(),ch)));
}
6. Problem Statement:
Write a code check if a set of vector elements are even or odd using
random access iterator.
Answer
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector<int >v;
int i,n,element,a=0,b=0;
cin>>n;
for(i=0;i<n;i++)
{
cin>>element;
v.push_back(element);
}
for(auto x:v)
{
cout<<x<<" ";
}
for(auto x:v)
{
if(x%2==0)
{
a++;
}
else
{
b++;
}
}
if(a!=0 && b!=0)
{
cout<<endl<<"Mixed";
}
else if(a!=0)
{
cout<<endl<<"Even";
}
else if(b!=0)
{
cout<<endl<<"Odd";
}
}
Attempt : 1
Total Mark : 60
Marks Obtained : 60
Section 1 : CODING
1. Problem Statement:
Write a program to find the maximum of two variables using function
templates.
Answer
// You are using GCC
#include<iostream>
using namespace std;
template <typename t>
t l(t n1,t n2)
{
if(n1>n2)
return n1;
else
return n2;
}
int main()
{
int a,b;
float c,d;
char x,y;
cin>>a>>b>>c>>d>>x>>y;
cout<<l<int>(a,b)<<" is larger."<<endl;
cout<<l<float>(c,d)<<" is larger."<<endl;
cout<<l<char>(x,y)<<" has larger ASCII value."<<endl;
}
2. Problem Statement:
Write a program to sort an array using function templates
Note: Use bubble sort
Answer
// You are using GCC
#include<iostream>
using namespace std;
template <typename t>
t bubble(t a[],int n)
{
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
swap(a[j],a[j+1]);
}
}
}
for(int i=0;i<n;i++)
{
cout<< a[i]<<" ";
}
cout<<endl;
}
int main()
{
int n;
cin>>n;
int a[20];
float b[20];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(int i=0;i<n;i++)
{
cin>>b[i];
}
bubble<int>(a,n);
bubble<float>(b,n);
3. Problem Statement:
Write a program to print the Array elements which are present at odd
positions using function templates.
Answer
// You are using GCC
// You are using GCC
#include<bits/stdc++.h>
using namespace std;
template<typename T>
T position(T a[],int n)
{
for(int i=0;i<n;i++)
{
if(i%2==0)
{
cout<<a[i]<<" ";
}
}
cout<<endl;
}
int main()
{
int n;
cin>>n;
int a[20];
string b[20];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(int i=0;i<n;i++)
{
cin>>b[i];
}
position<int>(a,n);
position<string>(b,n);
}
4. Problem Statement:
Write a program to find the maximum and minimum of two numbers using
class templates.
Answer
// You are using GCC
// You are using GCC
#include<iostream>
using namespace std;
template<class T>
T maxmin(T a,T b)
{
if(a>b)
{
cout<<a<<" "<<b<<endl;
}
else
{
cout<<b<<" "<<a<<endl;
}
}
int main()
{
int a,b;
float c,d;
cin>>a>>b>>c>>d;
maxmin<int>(a,b);
maxmin<float>(c,d);
}
5. Problem statement:
Write a program to find the sum of two integers using the inline lambda
expression,
Answer
// You are using GCC
#include<iostream>
using namespace std;
int main()
{
auto sum=[](int a,int b)
{
return a+b;
};
int a,b;
cin>>a>>b;
cout<<sum(a,b)<<endl;
}
6. Problem statement:
Write a program to sort the integer vector elements in descending order
using lambda expressions.
Answer
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
cin>>a[i];
cout <<endl;
sort(a,a+n,greater<int>());
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
}
Attempt : 1
Total Mark : 50
Marks Obtained : 50
Section 1 : CODING
1. Problem statement:
Answer
// You are using GCC
#include<bits/stdc++.h>
using namespace std;
class node
{
public:
int data;
node *link;
node(int input)
{
data=input;
link=NULL;
}
}*first,*last;
void add_at_first(int input)
{
node *newnode=new node(input);
newnode->link=first;
first=newnode;
}
void display()
{
node *temp;
temp=first;
while(temp!=NULL)
{
cout<<temp->data<<endl;
temp=temp ->link;
}
}
int main()
{
first =last =NULL;
int input;
while(true)
{
cin>>input;
if(input == -1)
break;
add_at_first(input);
}
display();
return 0;
}
2. Problem statement:
Insert at the position - After
Write a c++ program to insert the given value after the given position.
Answer
// You are using GCC
#include<bits/stdc++.h>
using namespace std;
class node
{
public:
int data;
node *link;
node(int input)
{
data=input;
link=NULL;
}
}*first,*last,*temp;
void create(int input)
{
node*newnode=new node(input);
if(first==NULL)
first=newnode;
else
last->link=newnode;
last=newnode;
}
void insert(int position,int input)
{
node*newnode=new node(input);
temp=first;
for(int i=1;i<position;i++)
{
temp=temp->link;
}
newnode->link=temp->link;
temp->link=newnode;
}
void display()
{
temp=first;
while(temp!=NULL)
{
cout<<temp->data<<"\n";
temp=temp->link;
}
}
int main()
{
first=last=NULL;
int input,position;
while(true)
{
cin>>input;
if(input==-1)
break;
create(input);
}
display();
cin>>position>>input;
insert(position,input);
display();
return 0;
}
3. Problem statement:
Delete at the beginning
Write a c++ program to delete the given list from the beginning.
Answer
#include<iostream>
#include<stdlib.h>
using namespace std;
class Node
{
public:
int data;
Node*next;
};
4. Problem statement:
Insertion at the end :
Write a c++ program to insert the value at the beginning of the list. Get the
value continuously from the user until the user enters a negative value.
Answer
// You are using GCC
#include<bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node *link;
Node(int input)
{
data=input;
link=NULL;
}
}*first,*last;
void add_last(int input)
{
Node *newNode= new Node(input);
if(first==NULL)
{
first=newNode;
}
else
{
last->link=newNode;
}
last=newNode;
}
void display()
{
Node *temp;
temp=first;
while(temp!=NULL)
{
cout<<temp->data<<"->";
temp=temp->link;
}
}
int main()
{
first=last=NULL;
int input,i,n;
cin>>n;
for(i=0;i<n;i++)
{
cin>>input;
if(input==-1)
{
break;
}
add_last(input);
}
display();
cout<<"NULL";
}
5. Problem statement:
Linked list - Searching an element :
Write a c++ Program to search for an element from the list.
Answer
#include<iostream>
using namespace std;
class Node
{
public:
int data;
Node*next;
};
void append(Node** head, int newdata)
{
Node*newnode= new Node();
Node*last=*head;
newnode->data = newdata;
newnode->next=NULL;
if(*head==NULL)
{
*head=newnode;
return;
}
while(last->next!=NULL)
last = last->next;
last->next=newnode;
return;
}
int List(Node *node,int a)
{
while(node!=NULL)
{
if(node->data==a)
return 1;
node=node->next;
}
return 0;
}
int main()
{
Node* head = NULL;
int a;
do
{
cin>>a;
if(a<0)
break;
append(&head,a);
}while(1);
cin>>a;
cout<<a;
a=List (head,a);
if(a)
cout<<" is present in the list";
else
cout<<" is not present in the list";
return 0;
}
Attempt : 1
Total Mark : 50
Marks Obtained : 50
Section 1 : CODING
1. Problem statement:
Find the top most element in the stack
Write a C++ program to find the topmost element in the stack. If the first
value of the stack is greater than the second one, then insert the greatest
one into the new stack.
Answer
// You are using GCC
#include<iostream>
using namespace std;
class Node
{
public:
int data;
Node*next;
Node(int input)
{
data=input;
next=NULL;
}
}*first,*temp,*flag,*temp1;
void add_at_first(int input)
{
Node *new_node=new Node(input);
new_node->next=first;
first=new_node;
}
void display()
{
temp=first;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
}
void sort()
{
static int x=0,y=0,z=0;
int a,b;
temp=first;
x=temp->data;
flag=temp->next;
y=flag->data;
temp1=flag->next;
z=temp1->data;
if(x>y)
a=x;
else if(x<y)
a=y;
if(y>z)
b=y;
else if(y<z)
b=z;
cout<<z<<" "<<b<<" "<<a;
}
int main()
{
first=NULL;
int input;
while(true)
{
cin>>input;
if(input==-1)
break;
add_at_first(input);
}
cout<<"Before maximizing:\n";
display();
cout<<"\nAfter maximizing:\n";
sort();
}
2. Problem statement:
Stack using array - Insertion
Write a C++ program to implement a stack using an array. if there is no
value, print "Stack is empty" and if the top is greater than the given n value,
print "Stack Overflow".
Answer
// You are using GCC
#include<iostream>
using namespace std;
int main()
{
int n,i;
cin>>n;
int a[n];
for(i=0;i<n;i++)
cin>>a[i];
cout<<"Stack elements are:\n";
for(i=n-1;i>=0;i--)
cout<<a[i]<<" ";
}
3. Problem statement:
Stack using array - Deletion
Write a C++ program to delete the element from the stack. If there is no
value, print "Stack is empty and if there are more than n values print "Stack
overflow" and while deleting if the top is -1 print "Stack underflow".
Answer
#include<iostream>
using namespace std;
class Node
{
public:
int data;
Node*next;
Node(int input)
{
data=input;
next=NULL;
}
}*first,*last,*temp,*temp1;
void add_at_top(int input)
{
Node *new_node=new Node(input);
new_node->next=first;
first=new_node;
}
void display(int j,int m)
{
temp=first;
if(temp!=NULL)
{
cout<<"Before pop starts:\n";
cout<<"Stack elements are:\n";
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<"\nEnter the number of values to be popped:\nAfter pop starts:";
for(int i=0;i<m;i++)
{
temp1=first;
if(temp1!=NULL)
{
cout<<"\npop number: "<<i+1<<"\nThe popped element is "<<temp1-
>data;
first=first->next;
temp=first;
if(temp==NULL)
{
cout<<"\nStack is empty"<<endl;
}
else
{
cout<<"\nStack elements are:"<<endl;
while(true)
{
if(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
else
break;
}
}
}
else
{
cout<<"pop number: "<<m<<"\nStack Underflow";
break;
}
}
}
else
cout<<"Stack is empty";
}
/*void delete_at_first(int i)
{
temp=top;
while(temp!=NULL)
{
cout<<"pop number: "<<i<<endl;
cout<<"The "
}
}*/
int main()
{
first=NULL;
int input,i,n,j=0;
cin>>n;
for(i=0;i<n;i++)
{
cin>>input;
add_at_top(input);
}
int m;
cin>>m;
display(j,m);
if(j==1)
return 0;
}
4. Problem statement:
Stack using linked list - Insertion
Write a C++ Program to implement a stack using a linked list.
Answer
// You are using GCC
#include<iostream>
using namespace std;
class Node
{
public:
int data;
Node*next;
Node(int input)
{
data=input;
next=NULL;
}
}*top,*temp;
void push(int input)
{
Node*new_node=new Node(input);
new_node->next=top;
top=new_node;
}
void display()
{
temp=top;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
}
int main()
{
top=NULL;
int input;
while(true)
{
cin>>input;
if(input==-1)
break;
else
push(input);
}
display();
}
5. Problem statement:
Stack using linked list - Deletion
Write a C++ program to delete the elements from the stack using linked list
implementation.
Answer
// You are using GCC
#include<iostream>
using namespace std;
class Node
{
public:
int data;
Node *next;
Node(int input)
{
data=input;
next=NULL;
}
}*top,*temp;
new_node->next=top;
top=new_node;
}
void display()
{
temp=top;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
}
void delete_at_first(int i)
{
temp=top;
while(temp!=NULL)
{
cout<<"Deleted element is "<<temp->data<<endl;
top=top->next;
delete(temp);
delete_at_first(i--);
}
}
int main()
{
top=NULL;
int input;
while(true)
{
cin>>input;
if(input==-1)
break;
create(input);
}
cout<<"Before deleting:"<<endl;
display();
for(int i=1;top!=NULL;i++)
{
delete_at_first(i);
}
cout<<"Stack is empty";
}
Attempt : 1
Total Mark : 40
Marks Obtained : 40
Section 1 : CODING
1. Problem statement:
Reverse a queue
Write a C++ program to display the queue in reverse order. If the queue
doesn't have a valued print "Queue is empty".
Answer
#include<iostream>
using namespace std;
class qnode
{
public:
int n;
qnode*next,*prev;
qnode(int ne)
{
n=ne;
next=NULL;
prev=NULL;
}
};
class queue
{
public:
qnode*front,*rear,*first;
queue()
{
front=rear=0;
}
void enqueue(int n)
{
qnode*temp= new qnode(n);
if(front==NULL)
{
front = rear=temp;
}
front->prev=NULL;
temp->prev=rear;
rear->next=temp;
rear=temp;
}
void before_rev()
{
qnode*temp=front;
cout<<"Before reversing:"<<endl;
while(temp!=NULL)
{
cout<<temp->n<<" ";
temp=temp->next;
}
cout<<endl;
}
void after_rev()
{
qnode*temp=rear;
cout<<"After reversing:"<<endl;
while(temp!=NULL)
{
cout<<temp->n<<" ";
//cout<<temp->prev<<" ";
temp=temp->prev;
}
}
bool check()
{
return front==NULL;
}
};
int main()
{
queue d;
int ele;
while(ele!=-1)
{
cin>>ele;
if(ele!=-1)[Link](ele);
}
if([Link]())
cout<<"Queue is empty";
else
{
d.before_rev();
d.after_rev();
}
return 0;
}
2. Problem statement:
Queue using array
Write a C++ program to implement Queue using an array.
Answer
#include<iostream>
using namespace std;
class qnode
{
public:
int front,rear;
unsigned capacity;
int *array;
qnode(int n)
{
front = rear = 0;
capacity=n;
array= new int [n];
}
void enqueue(int n)
{
if(capacity == rear)
{
cout<<"queue if full\n";
return;
}
array[rear]=n;
rear++;
}
void print()
{
cout<<"Queue elements are:"<<endl;
for(int i=0;i<rear;i++)
{
cout<<array[i]<<" ";
}
}
};
int main()
{
int n,ele;
cin>>n;
qnode d(n);
for(int i=0;i<n;i++)
{
cin>>ele;
[Link](ele);
}
[Link]();
return 0;
}
Answer
#include<iostream>
using namespace std;
class qnode
{
public:
int n;
qnode*next;
qnode(int ne)
{
n=ne;
next=NULL;
}
};
class queue
{
public:
qnode*first,*rear;
queue(){
first=rear=0;
}
void enqueue(int n)
{
qnode*temp=new qnode(n);
if(first==NULL)
{
first =rear=temp;
return;
}
first->next=temp;
rear=temp;
}
int dequeue()
{
qnode*temp=first;
int n=temp->n;
delete temp;
return n;
}
};
int main()
{
int n,ele;
queue d;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>ele;
[Link](ele);
}
cout<<[Link]();
return 0;
}
4. Problem statement:
Queue using a linked list :
Write a C++ program to implement a queue using a linked list.
Answer
#include<iostream>
using namespace std;
class qnode
{
public:
int n;
qnode*next;
qnode(int ne)
{
n=ne;
next=NULL;
}
};
class queue
{
public:
qnode*front,*rear;
queue()
{
front=rear=0;
}
void enqueue(int n)
{
qnode*temp=new qnode(n);
if(front==NULL)
{
front =rear=temp;
}
rear->next=temp;
rear=temp;
}
void print ()
{
while(front!=NULL)
{
cout<<front->n<<" ";
front=front->next;
}
}
};
int main()
{
int ele;
queue d;
while(ele!=-1)
{
cin>>ele;
if(ele!=-1)[Link](ele);
}
[Link]();
return 0;
}
Attempt : 1
Total Mark : 40
Marks Obtained : 40
Section 1 : CODING
1. Problem statement:
Implementation of binary search tree :
Write a C++ Program to implement a binary search tree.
Answer
#include<iostream>
using namespace std;
class node
{
public:
node*left;
int n;
node*right;
node(int ne)
{
left=NULL;
n=ne;
right=NULL;
}
};
void push(node**root,int n)
{
node*temp=new node(n);
if(*root==NULL)
{
*root=temp;
return;
}
node*last=*root;
while(true)
{
if(n<last->n)
{
if(last->left==NULL)
{
last->left=temp;
return ;
}
last=last->left;
}
else
{
if(last->right==NULL)
{
last->right=temp;
return;
}
last=last->right;
}
}
}
void inorder(node*a)
{
if(a==NULL) return;
inorder(a->left);
cout<<a->n<<" ";
inorder(a->right);
}
int main()
{
node*root=NULL;
int ele;
while(ele!=-1)
{
cin>>ele;
if(ele!=-1)push(&root,ele);
}
cout<<"Tree values are:"<<endl;
inorder(root);
return 0;
}
2. Problem statement:
Inorder traversal:
Write a C++ program to display the elements of trees in the inorder
traversal.
Answer
#include<iostream>
using namespace std;
class node
{
public:
node*left;
int n;
node*right;
node(int ne)
{
left=NULL;
n=ne;
right=NULL;
}
};
void push(node**root,int n)
{
node*temp=new node(n);
if(*root==NULL)
{
*root=temp;
return;
}
node*last=*root;
while(true)
{
if(n<last->n)
{
if(last->left==NULL)
{
last->left=temp;
return ;
}
last=last->left;
}
else
{
if(last->right==NULL)
{
last->right=temp;
return ;
}
last=last->right;
}
}
}
void inorder(node*a)
{
if(a==NULL)return;
inorder(a->left);
cout<<a->n<<" ";
inorder(a->right);
}
int main()
{
node*root=NULL;
int ele;
while(ele!=-1)
{
cin>>ele;
if(ele!=-1)push(&root,ele);
}
cout<<"Inorder Traversal:"<<endl;
inorder(root);
return 0;
}
3. Problem statement:
Preorder traversal
Write a C++ program to display the elements of tree in the preorder
traversal.
Answer
#include<iostream>
using namespace std;
class node
{
public:
node*left;
int n;
node*right;
node(int ne)
{
left=NULL;
n=ne;
right=NULL;
}
};
void push(node**root,int n)
{
node*temp= new node(n);
if(*root==NULL)
{
*root=temp;
return;
}
node*last=*root;
while(true)
{
if(n<last->n)
{
if(last->left==NULL)
{
last->left=temp;
return;
}
last=last->left;
}
else
{
if(last->right==NULL)
{
last->right=temp;
return;
}
last=last->right;
}
}
}
void preorder(node*a)
{
if(a==NULL) return;
cout<<a->n<<" ";
preorder(a->left);
preorder(a->right);
}
int main()
{
node*root=NULL;
int ele;
while(ele!=-1)
{
cin>>ele;
if(ele!=-1)push(&root,ele);
}
cout<<"Preorder Traversal:"<<endl;
preorder(root);
return 0;
}
Answer
#include<iostream>
using namespace std;
class node
{
public:
node*left;
int n;
node*right;
node(int ne)
{
left=NULL;
n=ne;
right=NULL;
}
};
void push(node**root,int n)
{
node*temp=new node(n);
if(*root==NULL)
{
*root=temp;
return;
}
node*last=*root;
while(true)
{
if(n < last->n)
{
if(last->left==NULL)
{
last->left=temp;
return;
}
last=last->left;
}
else
{
if(last->right==NULL)
{
last->right=temp;
return;
}
last=last->right;
}
}
}
void print(node*a)
{
if(a->right==NULL)
{
cout<<"Maximum element is "<<a->n;
return;
}
print(a->right);
}
int main()
{
node*root=NULL;
int ele;
while(ele!=-1)
{
cin>>ele;
if(ele!=-1)push(&root,ele);
}
print(root);
return 0;
}
Attempt : 1
Total Mark : 20
Marks Obtained : 20
Section 1 : CODING
1. Problem statement:
Graph - Adjacency Matrix Representation
Write a C++ program to implement a graph using an adjacency Matrix.
Answer
#include<iostream>
using namespace std;
int main()
{
int **graph,node,edge,f=0,sn,en,we;
char *dir;
dir=(char*)malloc(sizeof(char));
cin>>node;
cin>>edge;
graph=(int**)malloc(sizeof(int*)*node);
for(int i=0;i<node;i++)
*(graph+i)=(int*)malloc(sizeof(int)*edge);
cout<<"Before - adjacency Matrix Representation:\n";
for(int i=0;i<node;i++)
{
for(int j=0;j<edge;j++)
cout<<graph[i][j]<<" ";
cout<<endl;
}
for(int i=0;i<edge;i++)
{
cin>>sn>>en>>we;
graph[sn-1][en-1]=we;
if(f==0)
graph[en-1][sn-1]=we;}
cout<<"After -Adjacency Matrix Representation:\n";
for(int i=0;i<node;i++)
{
for(int j=0;j<edge;j++)
cout<<graph[i][j]<<" ";
cout<<endl;
}
return 0;
}
2. Problem statement:
Write a program to represent a graph using the adjacency list.
Example
Input
6
4
12
23
34
24
Output
0
1 -> 2
2 -> 1 -> 3 -> 4
3 -> 2 -> 4
4 -> 3 -> 2
5
Explanation
There are 6 vertices from 0 to 5. The connected edges are represented as
a list. While others are simply given without any connections.
Answer
#include<bits/stdc++.h>
using namespace std;
void addedge(vector<int> adj[],int u,int v)
{
adj[u].push_back(v);
adj[v].push_back(u);
}
void printGraph(vector<int> adj[],int V)
{
for(int v=0;v<V;++v)
{
cout<<v;
for(int i=0;i<adj[v].size();i++)
cout<<" -> "<<adj[v][i];
printf("\n");
}
}
int main()
{
int V,a,b;
cin>>V;
int E,c;
cin>>E;
vector<int> adj[V];
for(int i=0;i<E;i++)
{
cin>>a>>b;
addedge(adj,a,b);
}
printGraph(adj,V);
return 0;
}