2 - PDS - LabAssignment - 2 - Sub Format

Download as pdf or txt
Download as pdf or txt
You are on page 1of 42

NATIONAL INSTITUTE OF TECHNOLOGY WARANGAL

WARANGAL – 506 004


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
I B.Tech., I Semester; AIDS and Civil_B Sections
Programming and Data Structures Lab Assignment_2, September 2024

Roll No: 24CSB1A70


Name:Tushar Sampla
Write a program to find first and second biggest numbers in list of ‘n’ numbers
without using arrays and without sorting the list. Here, ‘n’ is user defined input.
Example: If the numbers given are 11, 3,
#include <iostream>
using namespace std;

int main() {
int num, firstbig, secondbig, n, i = 0;

firstbig = secondbig = INT_MIN;

cout << "Enter the total number of inputs: ";


cin >> n;

if (n < 2) {
cout << "At least two numbers are required." << endl;
return 1;
}

1. while (i < n) {
cout << "Enter number: ";
cin >> num;

if (num > firstbig) {


secondbig = firstbig;
firstbig = num;
} else if (num > secondbig && num != firstbig) {
secondbig = num;
}

i++;
}

cout << "The first biggest number is: " << firstbig << endl;
cout << "The second biggest number is: " << secondbig << endl;

return 0;
}

1
You are designing a program for an airport system. You must input each passenger's
arrival and departure times in the format hour: minute: second (24 hrs format). Your
program should calculate the waiting time for each person in the same format.
Additionally, you should calculate the cost incurred for this waiting time. For every 10-
minute interval, it is ₹100/-. Write a program to accomplish this task.
Example: Input

2 Arrival time = 10:15:20


Departure time = 12:10:12

Output
Waiting time = 1:54: 52
Cost incurred is 1200 Rupees

2
#include <iostream>
using namespace std;

int main() {
int ah, am, as;
int dh, dm, ds;
int ta, td, tt;
int wh, wm, ws;
int cost;

cout << "Enter arrival time (hour minute second): ";


cin >> ah >> am >> as;

cout << "Enter departure time (hour minute second): ";


cin >> dh >> dm >> ds;

ta = ah * 3600 + am * 60 + as;
td = dh * 3600 + dm * 60 + ds;

if (td >= ta) {


tt = td - ta;
} else {
tt = (24 * 3600 - ta) + td;
}

wh = tt / 3600;
tt %= 3600;
wm = tt / 60;
ws = tt % 60;

cout << "Waiting time = " << wh << ":" << wm << ":" << ws << endl;

cost = (wh * 60 + wm) / 10 * 100;


cout << "Cost incurred is " << cost << " Rupees" << endl;

return 0;
}

In a University, a department has N batches, where batch 1 is considered as the most


senior batch. The number of students in each batch are user input. Write a program to
assign the roll number to each student such that, the students in intra-batch follow
sequential numbering. Inter-batch roll numbers start with cumulative count of the number
3 of students and follow the same sequential numbering for the particular batch. For
example, if the number of batches are 6 and the number of students in those batches are:
3, 4, 5, 1, 2 and 3 respectively. Then the roll numbers of all the students are: 1.1, 1.2, 1.3,
4.1, 4.2, 4.3, 4.4, 8.1, 8.2, 8.3, 8.4, 8.5, 13.1, 14.1, 14.2, 16.1, 16.2, and 16.3.

3
#include <iostream>
using namespace std;
int main() {
int N;
cout << "Enter the number of batches: ";
cin >> N;
int cumulativecount = 0,temp=1;
int i = 1;
while (i <= N) {
int numStudents;
cout << "Enter the number of students in batch " << i << ": ";
cin >> numStudents;
int j = 1;
cumulativecount++;
cumulativecount=temp;
while (j <= numStudents) {
temp++;
cout << cumulativecount << "." << j << endl;
j++;
}
i++;
}
return 0;
}

Write a C++ program to display the leap years that contain the digit ‘x’ given by the user,
between two given year n1 and n2 both inclusive. For example, if n1 and n2 are 1990 and
4 2020 and if the digit ‘x’ is 6, the outputs need to be: The number of leap years between
1990 and 2020 that contains the digit 6 are: 1996, 2016.

4
#include<iostream>
using namespace std;

int main() {
int n1, n2, x;
cout << "Enter the initial year: ";
cin >> n1;
cout << "Enter the final year: ";
cin >> n2;
cout << "Enter the digit you want to be included in the years: ";
cin >> x;
cout << "The leap years between " << n1 << " and " << n2 << " that contain the digit " << x << " are:" <<
endl;

while (n1 <= n2) {


if ((n1 % 4 == 0 && n1 % 100 != 0) || (n1 % 400 == 0)) {
int temp = n1;
while (temp > 0) {
int digit = temp % 10;
if (digit == x) {
cout << n1 << endl;
break;
}
temp /= 10;
}
}
n1++;
}

return 0;
}

5
Consider that you are given a task of telling the berth position (lower/middle/upper/side
lower/side upper) based on the berth number in a railway coach. If the passenger is not
aware of his/her berth position, he/she shall approach you and you need to tell them the
same. Now imagine that you are in-charge for ‘n’ coaches. The following are your tasks:
i. Display the birth position according to the berth number told by the passenger.
ii. Display the total number of requests received from each coach’s passengers as
well as the total ‘n’ coaches.
Write a program to automate the above scenario.

A model coach with berth numbers is shown in Figure 1. Table 1 gives the respective
berth positions (continue the numbering till 72). Note that the capacity of a coach is for
72 passengers.

Figure 1: Berth numbers


1: Lower
2: Middle
3: Upper

4: Lower
5: Middle
5 6: Upper

7: Side Lower
8: Side Upper
Table-1: Berth positions for berth numbers 1 to8.

Example:
Enter the number of coaches: 2
Coach-1:
Passenger-1: Berth Number: 23-----> Informed his/her berth position as: Side Lower
Passenger-2: Berth Number: 45-----> Informed his/her berth position as: Middle Berth
Total number of requests received from coach-1 are: 2
Coach-2:
Passenger-1: Berth Number: 10-----> Informed his/her berth position as: Middle
Passenger-2: Berth Number: 4-----> Informed his/her berth position as: Lower
Passenger-3: Berth Number: 32-----> Informed his/her berth position as: Side Upper
Passenger-4: Berth Number: 6-----> Informed his/her berth position as: Upper
Total number of requests received from coach-2 are: 4
The total number of requests received for this train are: 6

6
#include <iostream>
using namespace std;

int get_berth_position(int berth_num) {


if (berth_num % 8 == 1 || berth_num % 8 == 4)
return 1; // Lower Berth
else if (berth_num % 8 == 2 || berth_num % 8 == 5)
return 2; // Middle Berth
else if (berth_num % 8 == 3 || berth_num % 8 == 6)
return 3; // Upper Berth
else if (berth_num % 8 == 7)
return 4; // Side Lower Berth
else
return 5; // Side Upper Berth
}

int main() {
int n, num_of_passengers, berth_num, i = 1, total_requests = 0;

cout << "Enter the number of coaches: ";


cin >> n;

while (i <= n) {
int requests = 0;
cout << "Coach-" << i << ":" << endl;

cout << "Enter the number of passengers: ";


cin >> num_of_passengers;

int j = 0;
while (j < num_of_passengers) {
cout << "Passenger-" << (j + 1) << ": Enter Berth Number: ";
cin >> berth_num;

int berth_position = get_berth_position(berth_num);

switch (berth_position) {
case 1:
cout << "Informed his/her berth position as: Lower Berth" << endl;
break;
case 2:
cout << "Informed his/her berth position as: Middle Berth" << endl;
break;
case 3:
cout << "Informed his/her berth position as: Upper Berth" << endl;
break;
case 4:
cout << "Informed his/her berth position as: Side Lower Berth" << endl;
break;
case 5:
cout << "Informed his/her berth position as: Side Upper Berth" << endl;
break;
}

requests++;
j++;
}

cout << "Total number of requests received from coach-" << i << " are: " << requests << endl;
total_requests += requests;
i++;
}

7
cout << "The total number of requests received for this train are: " << total_requests << endl;

return 0;
}

Write a program to read a 6-digit number and display it in words. Also shift the given
number one positions towards right by placing the last digit as the first digit in the result
and display it also in words.
Input: 123456
Output:
6 The given number in words: Lakhs: 1, Thousands: 23, Hundreds: 4, Tens: 5 and Ones: 6.
The resulted number is: 612345
The resulted number in words: Lakhs: 6, Thousands: 12, Hundreds: 3, Tens: 4 and Ones:
5.

8
#include <iostream>
using namespace std;

int main() {
int number;

cout << "Enter a 6-digit number: ";


cin >> number;

if (number < 100000 || number > 999999) {


cout << "Please enter a valid 6-digit number." << endl;
return 1;
}

int lakhs = number / 100000;


int thousands = (number / 1000) % 100;
int hundreds = (number / 100) % 10;
int tens = (number / 10) % 10;
int ones = number % 10;

cout << "The given number in words: ";


cout << "Lakhs: " << lakhs << ", Thousands: " << thousands << ", Hundreds: " << hundreds;
cout << ", Tens: " << tens << ", and Ones: " << ones << "." << endl;

int last_digit = number % 10;


int shifted_number = last_digit * 100000 + (number / 10);

cout << "The resulted number is: " << shifted_number << endl;

lakhs = shifted_number / 100000;


thousands = (shifted_number / 1000) % 100;
hundreds = (shifted_number / 100) % 10;
tens = (shifted_number / 10) % 10;
ones = shifted_number % 10;

cout << "The resulted number in words: ";


cout << "Lakhs: " << lakhs << ", Thousands: " << thousands << ", Hundreds: " << hundreds;
cout << ", Tens: " << tens << ", and Ones: " << ones << "." << endl;

return 0;
}

Write a program to display the 2nd biggest number in each of ‘n’ lists of numbers, where
each list contains ‘m’ numbers, m≥2.
Example:

List-1 List-2 List-3 List-4


10 21 -2 1
7 25 22 -6 2
573
27 4
5
Second Biggest is 25 21 -2 4

9
#include <iostream>
using namespace std;

int main() {
int n, m, num;
int firstbig, secondbig;

cout << "Enter the number of lists: ";


cin >> n;

for (int i = 1; i <= n; i++) {


cout << "Enter the number of elements in list " << i << ": ";
cin >> m;

firstbig = secondbig = INT_MIN;

int j = 0;
while (j < m) {
cout << "Enter number: ";
cin >> num;

if (num > firstbig) {


secondbig = firstbig;
firstbig = num;
} else if (num > secondbig && num != firstbig) {
secondbig = num;
}

j++;
}

if (secondbig == INT_MIN) {
cout << "There is no second biggest number in list " << i << "." << endl;
} else {
cout << "Second biggest number in list " << i << " is: " << secondbig << endl;
}
}

return 0;
}

Write a program to display the sum of first ‘n’ even numbers in Fibonacci series. For
8 example, if n=4, the result is 188.

10
#include<iostream>
using namespace std;

int main(){
int a=0,b=1,i,sum,count;
cout<<"Enter no. of element you want in series";
cin>>count;
while(i<=count){
sum=a+b;
a=b;
b=sum;
if(sum%2==0){
int en=sum;
cout<<en<<" ";
}
i++;
}
}

Write a program to print all the prime numbers between GCD and LCM of two user
defined integer numbers. If the given two integers are 20 and 14, the output needs to be
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,
9 101, 103, 107, 109, 113, 127, 131, 137, 139.

11
#include <iostream>
#include <cmath>

using namespace std;

int main() {
int n1, n2;
cout << "Enter two integers: ";
cin >> n1 >> n2;

int a = n1, b = n2;


while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
int gcd = a;

int lcm = (n1 * n2) / gcd;

cout << "Prime numbers between GCD (" << gcd << ") and LCM (" << lcm << "):" << endl;

int i = gcd;
while (i <= lcm) {
if (i >= 2) {
int prime = 1;
int j = 2;
while (j <= sqrt(i)) {
if (i % j == 0) {
prime = 0;
break;
}
j++;
}
if (prime == 1) {
cout << i << " ";
}
}
i++;
}

cout << endl;


return 0;
}

12
In India, when multiple couples show up at the marriage registration office at the same
time, they have trouble deciding which couple to marry first because they can only do one
wedding at a time. Dishant, who works in the marriage department along with Naman,
has a solution. They suggest a way to decide the order of marriages. They will compare
the age difference between the couples. The greater the age difference between a couple,
the higher their priority will be for getting married. Also print the date of marriage of each
couple
1. For First Couple Marriage (who is having maximum age difference) marriage date
is calculated by adding 15 days to current date (17-10-2023).
2. For the remaining couple marriage date is calculated by adding 30 days to current
date(17-10-2023).
Help Dishant and Naman in ordering the marriage of the two couples and displaying the
10 marriage day of each couple by writing a computer program for this. Note that all the 12
months contain 30 days each.
Example: Couple 1: Bride(Girl) date of birth: 15 5 2001
Groom (Boy) date of birth: 25 9 1997
Couple 2: Bride(Girl) date of birth: 25 6 2001
Groom (Boy) date of birth: 27 10 1997
Output
Age difference of Couple 1 = 03 Years 7 months 20 days
Age difference of Couple 2 = 03 Years 7 month 28 days
Couple 2 will get Married First
Marriage date for Couple 2 : 2 11 2023
Marriage date for Couple 1 :17 11 2023

13
#include <iostream>
using namespace std;

int main() {
int b1d, b1m, b1y;
int g1d, g1m, g1y;
int b2d, b2m, b2y;
int g2d, g2m, g2y;

cout << "Enter Bride 1 (day month year): ";


cin >> b1d >> b1m >> b1y;
cout << "Enter Groom 1 (day month year): ";
cin >> g1d >> g1m >> g1y;

cout << "Enter Bride 2 (day month year): ";


cin >> b2d >> b2m >> b2y;
cout << "Enter Groom 2 (day month year): ";
cin >> g2d >> g2m >> g2y;

int ad1y = g1y - b1y;


int ad1m = g1m - b1m;
int ad1d = g1d - b1d;

if (ad1d < 0) {
ad1m--;
ad1d += 30;
}

if (ad1m < 0) {
ad1y--;
ad1m += 12;
}

int ad2y = g2y - b2y;


int ad2m = g2m - b2m;
int ad2d = g2d - b2d;

if (ad2d < 0) {
ad2m--;
ad2d += 30;
}

if (ad2m < 0) {
ad2y--;
ad2m += 12;
}

cout << "Age difference of Couple 1 = " << ad1y << " Years "
<< ad1m << " months " << ad1d << " days" << endl;

cout << "Age difference of Couple 2 = " << ad2y << " Years "
<< ad2m << " months " << ad2d << " days" << endl;

if (ad1y > ad2y ||


(ad1y == ad2y && (ad1m > ad2m ||
(ad1m == ad2m && ad1d > ad2d)))) {
cout << "Couple 1 will get Married First" << endl;
cout << "Marriage date for Couple 1: " << b1d + 15 << " "
<< b1m << " " << b1y << endl;
cout << "Marriage date for Couple 2: " << b2d + 30 << " "
<< b2m << " " << b2y << endl;
} else {
cout << "Couple 2 will get Married First" << endl;

14
cout << "Marriage date for Couple 2: " << b2d + 15 << " "
<< b2m << " " << b2y << endl;
cout << "Marriage date for Couple 1: " << b1d + 30 << " "
<< b1m << " " << b1y << endl;
}

return 0;
}

Our NIT Warangal offer n undergraduate programmes (CSE, Civil,...). Moreover, let us
assume that for each program, intake of students is m based on JEE mains rank. Write a

program to read the JEE mains rank of students in each branch and display the starting
rank and ending rank for each branch.
Example:
Enter the number of undergraduate programs in NITW: 3
Enter the number of students in program 1: 4
Enter the student 1 in program 1: 524
Enter the student 2 in program 1: 124
Enter the student 3 in program 1: 555
Enter the student 4 in program 1: 444
Starting rank and ending rank for program 1 is 124 and 555
11 Enter the number of students in program 2: 3
Enter the student 1 in program 2: 5524
Enter the student 2 in program 2: 5124
Enter the student 3 in program 2: 5444
Starting rank and ending rank for program 2 is 5124 and 5524
Enter the number of students in program 3: 5
Enter the student 1 in program 3: 5
Enter the student 2 in program 3: 9
Enter the student 3 in program 3: 1
Enter the student 4 in program 3: 7
Enter the student 5 in program 3: 6
Starting rank and ending rank for program 3 is 1 and 9

15
#include <iostream>

using namespace std;

int main() {
int n;
cout << "Enter the number of undergraduate programs in NITW: ";
cin >> n;

int i = 1;
while (i <= n) {
int m;
cout << "Enter the number of students in program " << i << ": ";
cin >> m;

int rank;
cout << "Enter the student 1 in program " << i << ": ";
cin >> rank;

int minRank = rank;


int maxRank = rank;

int j = 2;
while (j <= m) {
cout << "Enter the student " << j << " in program " << i << ": ";
cin >> rank;

if (rank < minRank) {


minRank = rank;
}
if (rank > maxRank) {
maxRank = rank;
}
j++;
}

cout << "Starting rank and ending rank for program " << i << " is " << minRank << " and " <<
maxRank << endl;
i++;
}

return 0;
}

16
There are ‘n’ farmers in a village. They cultivate paddy in their agriculture lands. They
all send their paddy to a miller. The miller said that 76kgs of paddy is considered as one
bag. Each farmer has given his paddy to the miller. Write a program to read the paddy of
each farmer and display the total number of bags and kgs of the paddy of that village.
Note that each farmer enters two integers: first one represents the bags and the second one
represents the number of kgs.
Example: Enter the number of farmers: 4
Enter the amount of paddy of farmer 1: 16 20 (i.e 16 bags 20 kgs)
Enter the amount of paddy of farmer 2: 12 42 (i.e 16 bags 20 kgs)
Enter the amount of paddy of farmer 3: 14 25 (i.e 16 bags 20 kgs)
Enter the amount of paddy of farmer 4: 16 0 (i.e 16 bags 20 kgs)
Output: The total number of paddy from this village is: 59 bags 11 kgs.
#include<iostream>
using namespace std;

int main(){
int bags,weight,nof,i=1;
cout<<"Enter the no. of farmers in village ";
cin>>nof;

while(i<=nof){

cout<<"Enter no. of bags and weight of each bag of Farmer"<<i<<"has";


cin>>bags>>weight;
12
int bw=(bags*weight);
int sum=0;
sum=sum+bw;

if(i==nof){
cout<<"Total weight in the village:"<<(sum/16)<<"Bags &"<<(sum%16)<<"kgs";
}
i++;
}

The cost price, mark price (MRP) and discount (in %) is entered through keyboard.
Sometimes seller gets profit of x % or some time loss of y % depends on discount. Write
a program to determine whether the seller has made profit or incurred loss. Also determine
13 how much profit he made or loss incurred.

17
#include<iostream>
using namespace std;

int main(){
double cost,MRP;
cout<<"Enter the cost price of product";
cin>>cost;
cout<<"Enter the MRP of product";
cin>>MRP;

if((MRP-cost)>0){
double profit=MRP-cost;
cout<<"Profit:"<<(profit/cost)*100<<"%";
}
else{
double loss=cost-MRP;
cout<<"Loss:"<<(loss/cost)*100<<"%";
}
if(cost==MRP){
cout<<"You sold it at Zero profit";
}
}

It is possible to calculate easily the day of the week on which a given date falls, if one
knows the Julian day of that date. For example, January 1 is always Julian day 1, since it
is the first day of the year, whereas December 31 is day 365 in a non-leap year or day 366
in a leap year. The day of the week is calculated as follows:
Year = year in question (all four digits)
Julian_day = Julian day of date in question (1 to 366)
fours = integer portion of (year -1)/4
hundreds = integer portion of (year-1)/100
14 four_hundreds = integer portion of (year-1)/400
day_of_the_week = (year + Julian_day + fours – hundreds + four_hundreds ) % 7 where:

Write a program to calculate the day of the week as described above. Verify its correctness
by testing it on the current date.
Note: Your program need to display only the number, it need not display the
corresponding day name.

18
#include <iostream>
using namespace std;

int main() {
int y, jd, f, h, fh, dow;

cout << "Enter year (YYYY): ";


cin >> y;

cout << "Enter Julian day (1 to 366): ";


cin >> jd;

f = (y - 1) / 4;
h = (y - 1) / 100;
fh = (y - 1) / 400;

dow = (y + jd + f - h + fh) % 7;

cout << "The day of the week is: " << dow << endl;

return 0;
}

Write a program that plays the game of Mad Lib. Your program should prompt the user
to enter the following data.

The first or last name of the instructor


Your name
A food
A number between 100 and 120
An adjective
A color
An animal

After the data are input, they should be substituted into the story below and output to the
15 console.
----------------------------------------------------------------------------------------------------
Dear Instructor [Instructor Name],
I am sorry that I am unable to turn in my homework at this time. First I ate a rotten
[Food], which made me turn [Color] and extremely ill. I came down with a fever of
[Number 100-120]. Next, my [Adjective] pet [Animal] must have smelled the remains of

the [Food] on my homework, because he ate it. I am currently rewriting my homework


and hope you will accept it later.
Sincerely,
[Your Name]

19
#include<iostream>
using namespace std;

int main(){
char in,f,c,n,adj,an,yn;
cout<<"Enter Your name,A food,A number between 100 and 120,An adjective,A color,An animal"<<endl;
cin>>in>>f>>c>>n>>adj>>an>>yn;

cout<<"Dear Instructor"<<in<<endl;
cout<<"I am sorry that I am unable to turn in my homework at this time. First I ate a rotten
\n"<<'f'<<"which made me turn"<<'c'<<"and extremely ill. I came down with a fever of\n"<<'n'<<".Next,
my"<<'adj'<<"pet"<<'an'<<"must have smelled the remains of<<”\n”<<the"<<'f'<<"on my homework,
because he ate it. I am currently rewriting my homework\nand hope you will accept it
later.\nSincerely,\n"<<'yn'<<endl;
}

Write a program to display the sum of alternate numbers in each of ‘n’ lists but also this
sum need to be calculated by starting alternate positions in alternate list as given in the
example. Here each list can contain ‘m’ numbers, m≥1.

List-1 List-2 List-3 List-4 List-5


10 123 11 1 100
2 21 2 22 2222
32 300 13 3 300
16 4 41 4 444
54 52 52 500
65 61 6 666
7 75 73 700
89 81 8 888
91 999 900
10 101 100
Sum 194 305 149 22 2500

20
#include <iostream>
using namespace std;

int main() {
int n, m, num, sum;
cout << "Enter the number of lists: ";
cin >> n;

int i = 1;

while (i <= n) {
cout << "Enter the number of elements in list " << i << ": ";
cin >> m;

sum = 0;
int j = 0;

while (j < m) {
cout << "Enter number: ";
cin >> num;

if ((i % 2 == 1 && j % 2 == 0) || (i % 2 == 0 && j % 2 == 1)) {


sum += num;
}

j++;
}

cout << "Sum of alternate numbers in list " << i << " is: " << sum << endl;
i++;}
return 0;
}

21
In our NIT Warangal, we have some courses which have both theory and Lab components.
A faculty may conduct theory for ‘x’ marks and Lab part for ‘y’ marks, where each of ‘x’
and ‘y’ may have ‘m’ and ‘n’ subparts respectively. The total of x and y can be >100 or <
100 or equal to 100. The weightage given to theory part is 75% and lab part is 25%. The
final marks for the student to be considered is out of 100. Assume that the values of all
exam’s marks of both ‘m’ and ‘n’ are known to you. Write a program to display the
percentage of contribution of all the ‘m’ and ‘n’ parts. Your program should work for
any valid values of x, y, m and n.
Example-1:
Enter the Total marks of theory part: x : 40
Enter the number of exams in theory part: m: 2
Enter the marks for each of ‘m’ exams: 10 30
Enter the Total marks of lab part : y: 20

Enter the number of exams in theory part: n: 3


Enter the marks for each of ‘n’ exams: 5 7 8
Output:
The contribution of x1 exam is:18.75%
The contribution of x2 exam is:56.25%

17 The contribution of y1 exam is:6.25%


The contribution of y2 exam is:8.75%
The contribution of y3 exam is:10.0%
Example-2:
Enter the Total marks of theory part: x : 100
Enter the number of exams in theory part: m: 4
Enter the marks for each of ‘m’ exams: 10 20 30 40
Enter the Total marks of lab part : y: 100
Enter the number of exams in theory part: n: 3
Enter the marks for each of ‘n’ exams: 20 20 60
Output:
The contribution of x1 exam is:7.5%
The contribution of x2 exam is:15.0%
The contribution of x3 exam is:22.5%
The contribution of x4 exam is:30.0%

The contribution of y1 exam is:5.0%


The contribution of y2 exam is:5.0%
The contribution of y3 exam is:15.0%

22
#include <iostream>
using namespace std;

int main() {
int x, m, y, n;
cout << "Enter the total marks of theory part: ";
cin >> x;

cout << "Enter the number of exams in theory part: ";


cin >> m;

int theory[m];
cout << "Enter the marks for each of " << m << " theory exams: ";
int theory_sum = 0;

for (int i = 0; i < m; i++) {


cin >> theory[i];
theory_sum += theory[i];
}

cout << "Enter the total marks of lab part: ";


cin >> y;

cout << "Enter the number of exams in lab part: ";


cin >> n;

int lab[n];
cout << "Enter the marks for each of " << n << " lab exams: ";
int lab_sum = 0;

for (int j = 0; j < n; j++) {


cin >> lab[j];
lab_sum += lab[j];
}

cout << "Theory Part Contribution:\n";


for (int i = 0; i < m; i++) {
double contribution = (static_cast<double>(theory[i]) / x) * 75;
cout << "The contribution of theory exam " << (i + 1) << " is: " << contribution << "%" << endl;
}

cout << "Lab Part Contribution:\n";


for (int j = 0; j < n; j++) {
double contribution = (static_cast<double>(lab[j]) / y) * 25;
cout << "The contribution of lab exam " << (j + 1) << " is: " << contribution << "%" << endl;
}

return 0;
}

23
Given an integer N. Write a program to find the number in the range from 1 to N-1 which
is having the maximum number of terms in its Collatz Sequence and the number of terms
in the sequence. The Collatz Sequence is defined as: start with any positive integer n.
Then each term is obtained from the previous term as follows: if the previous term is even,
the next term is one half of the previous term. If the previous term is odd, the next term is
3 times the previous term plus 1. The conjecture is that no matter what value of n, the
18 sequence will always reach 1.
For example if N=13, Output: The Collatz Sequence is:
13 -> 40 -> 20 -> 10 -> 5 > 16 -> 8 -> 4 -> 2 -> 1
The number of terms are: 10

24
#include <iostream>
using namespace std;

int main() {
int n, max_terms = 0, number_with_max_terms = 0;

cout << "Enter an integer N: ";


cin >> n;

int i = 1;
while (i < n) {
int count = 0;
int current = i;

while (current != 1) {
if (current % 2 == 0) {
current /= 2;
} else {
current = 3 * current + 1;
}
count++;
}

if (count > max_terms) {


max_terms = count;
number_with_max_terms = i;
}

i++;
}

cout << "The number with the maximum terms in its Collatz sequence is: "
<< number_with_max_terms << endl;
cout << "The number of terms are: " << max_terms + 1 << endl;

return 0;
}

Write a program that converts letters of the alphabets into their corresponding digits on
the telephone keypad as shown in the following diagram. The program should let the user
enter letters repeatedly until a ‘$’ symbol is entered by user. An error message should be
19 printed for any non-alphabetic character that is entered. Example, if the input is ‘M’, then
the corresponding digit is 6.

25
#include <iostream>
using namespace std;

int main() {
char input;

cout << "Enter letters (enter '$' to stop): ";


cin >> input;

while (input != '$') {


if (input >= 'A' && input <= 'Z') {
if (input == 'A' || input == 'B' || input == 'C') {
cout << "Corresponding digit is: 2" << endl;
} else if (input == 'D' || input == 'E' || input == 'F') {
cout << "Corresponding digit is: 3" << endl;
} else if (input == 'G' || input == 'H' || input == 'I') {
cout << "Corresponding digit is: 4" << endl;
} else if (input == 'J' || input == 'K' || input == 'L') {
cout << "Corresponding digit is: 5" << endl;
} else if (input == 'M' || input == 'N' || input == 'O') {
cout << "Corresponding digit is: 6" << endl;
} else if (input == 'P' || input == 'Q' || input == 'R' || input == 'S') {
cout << "Corresponding digit is: 7" << endl;
} else if (input == 'T' || input == 'U' || input == 'V') {
cout << "Corresponding digit is: 8" << endl;
} else if (input == 'W' || input == 'X' || input == 'Y' || input == 'Z') {
cout << "Corresponding digit is: 9" << endl;
}
} else {
cout << "Error: Non-alphabetic character entered." << endl;
}
cin >> input;
}

return 0;
}

Write a program to find the second largest number among n numbers entered. Example:
20 Input: 840, 288, 261, 337, 335, 488 Output: 488.

26
#include <iostream>
using namespace std;

int main() {
int n, num;
int firstbig = INT_MIN, secondbig = INT_MIN;

cout << "Enter the total number of inputs: ";


cin >> n;

if (n < 2) {
cout << "At least two numbers are required." << endl;
return 1;
}

int count = 0;

while (count < n) {


cout << "Enter number: ";
cin >> num;

if (num > firstbig) {


secondbig = firstbig;
firstbig = num;
} else if (num > secondbig && num != firstbig) {
secondbig = num;
}

count++;
}

if (secondbig == INT_MIN) {
cout << "There is no second largest number." << endl;
} else {
cout << "The second largest number is: " << secondbig << endl;
}

return 0;
}

27
The “Russian Peasant Problem” is a method multiplying two numbers together, using only
division by 2, multiplication by 2, and addition. For example, if the numbers 17 and 19
are to be multiplied together, they are put at the head of two columns. The first number,
17 is divided by 2 (using integer division), and the result is placed below the original
number in the first column. The second number, 19 is multiplied by 2; This process is
continued until the number on the left reduces to 1. All these numbers in the right hand
column which lie to the right of even numbers in the left hand column are ignored. The
remaining numbers in the right hand column then are added together (19+304=323). The
result is the solution to the problem, 17 time 19. Write a program to implement the Russian
Peasant method for multiplying any tow positive integers.
17 19
8 38 (ignore)
4 76(ignore)
2 152(ignore)
1 304
The result is : 323.
#include <iostream>
using namespace std;

int main() {
int a, b, result = 0;

cout << "Enter two positive integers: ";


cin >> a >> b;

21 while (a > 0) {
if (a % 2 == 1) {
result += b;
}
a /= 2;
b *= 2;
}

cout << "The result is: " << result << endl;

return 0;
}

Write a program to count the occurrence of a particular digit in a given number.


For example, if n=12311 and in which we want to find occurrence of 1 - The
22 occurrence of 1 will be 3 in number 12311.

28
#include <iostream>
using namespace std;

int main() {
int n, digit, count = 0;

cout << "Enter a number: ";


cin >> n;

cout << "Enter the digit to find: ";


cin >> digit;

int temp = n;

while (temp > 0) {


if (temp % 10 == digit) {
count++;
}
temp /= 10;
}

cout << "The occurrence of " << digit << " in " << n << " is: " << count << endl;

return 0;
}

Write a program to convert a decimal number to a binary number and count length and
number of 1s and number of 0s in the binary number.
For Example, if n=12
Binary Number is 1100
23 Length is 4
No. of 1s is 2
No. of 0s is 2

29
#include <iostream>
using namespace std;

int main() {
int n, binary = 0, count = 0, ones = 0, zeros = 0;
cout << "Enter a decimal number: ";
cin >> n;

int temp = n;
int place = 1;

while (temp > 0) {


int bit = temp % 2;
binary += bit * place;
if (bit == 1) {
ones++;
} else {
zeros++;
}
temp /= 2;
place *= 10;
count++;
}

cout << "Binary Number is: " << binary << endl;
cout << "Length is: " << count << endl;
cout << "No. of 1s is: " << ones << endl;
cout << "No. of 0s is: " << zeros << endl;

return 0;
}

*
*A*
*A*A*
24 *A*A*A*
*A*A*A*A*

30
#include <iostream>
using namespace std;

int main() {
int n = 5;
int i = 1;
while (i <= n) {
cout << "* ";
int j = 1;
while (j < i) {
cout << "A * ";
j++;
}
cout << endl;
i++;
}
return 0;
}

1
32
456
25 10 9 8 7
11 12 13 14 15

31
#include <iostream>
using namespace std;

int main() {
int n = 5;
int num = 1;
int i = 1;
while (i <= n) {
if (i % 2 != 0) {
int j = num;
while (j < num + i) {
cout << j << " ";
j++;
}
} else {
int j = num + i - 1;
while (j >= num) {
cout << j << " ";
j--;
}
}
num += i;
cout << endl;
i++;
}
return 0;
}

1
2*2
3*3*3
26 3*3*3
2*2
1

32
#include <iostream>
using namespace std;

int main() {
int n = 5; // You can change n to adjust the size of the pattern
int i = 1;

while (i <= n) {
int j = 1;

while (j <= i) {
cout << i << " ";
j++;
}

cout << endl;


i++;
}

return 0;
}

**
**
*
27 **
**

33
#include <iostream>
using namespace std;

int main() {
int n = 5; // You can change n to adjust the size of the pattern
int i = 1;

while (i <= n) {
int j = 1;

while (j <= n - i) {
cout << " ";
j++;
}

j = 1;
while (j <= (2 * i - 1)) {
cout << "*";
j++;
}

cout << endl;


i++;
}

return 0;
}

12
12
1
28 21
21

34
#include <iostream>
using namespace std;

int main() {
int n = 5; // You can change n to adjust the size of the diamond
int i = 1;

while (i <= n) {
int j = 1;

while (j <= n - i) {
cout << " ";
j++;
}

j = 1;
while (j <= (2 * i - 1)) {
cout << "*";
j++;
}

cout << endl;


i++;
}

i = n - 1;

while (i >= 1) {
int j = 1;

while (j <= n - i) {
cout << " ";
j++;
}

j = 1;
while (j <= (2 * i - 1)) {
cout << "*";
j++;
}

cout << endl;


i--;
}

return 0;
}

35
*****
**
*
**
*****
#include <iostream>
using namespace std;

int main() {
int n = 5; // You can change n to adjust the size of the pattern
int i = 1;

while (i <= n) {
int j = 1;

while (j <= i) {
29 cout << j << " ";
j++;
}

int k = i - 1;
while (k > 0) {
cout << k << " ";
k--;
}

cout << endl;


i++;
}

return 0;
}

36
*****
*
*
*
*****
#include <iostream>
using namespace std;

int main() {
int n = 5; // You can change n to adjust the size of the pattern
int i = 1;

while (i <= n) {
int j = 1;

while (j <= n - i) {
cout << " ";
j++;
}

j = 1;
while (j <= (2 * i - 1)) {
30 cout << "*";
j++;
}

cout << endl;


i++;
}

return 0;
}

37
1
2
3
4
5
5
4
3
2
1

1
2
3
4
4
3
2
1

1
2
3
3
2
1

1
2
2
1
31
1

1
#include <iostream>
using namespace std;

int main() {
int n = 5; // You can change n to adjust the size of the pattern
int i = 1;

while (i <= n) {
int j = 1;

while (j <= i) {
cout << j << " ";
j++;
}

int k = i - 1;
while (k > 0) {
cout << k << " ";
k--;
}

cout << endl;


i++;
}

return 0;
}

38
A
B
C
D
E
F
G
H
I

J
K
L
M
N
O
P

Q
R
S
T
U
V
W
X
Y

39
#include <iostream>
using namespace std;

int main() {
int n = 6; // Number of rows
char c = 'A'; // Starting character
int i = 1;

while (i <= n) {
int j = 1;

while (j <= i) {
cout << c << " ";
j++;
}

c++; // Move to the next character


cout << endl;
i++;
}

return 0;
}

40
1

1
2
2
1

1
2
3
3
2
1

1
2
3
4
4
3
2
1

1
2
3
4
5
33 5
4
3
2
1

1
2
3
4
4
3
2
1

1
2
3
3
2
1

1
2
2
1

41
#include <iostream>
using namespace std;

int main() {
int n = 5; // Number of rows
int i = 1;

while (i <= n) {
int j = 1;

while (j <= i) {
cout << i << " ";
j++;
}

cout << endl;


i++;
}

return 0;
}

42

You might also like