Important Coding Questions CS101
Important Coding Questions CS101
CLASS: CS101
COURSE: 2023428
Write the Program that prints the number in reverse order. For example, your program
asks the user for input i.e., 7654, and then the program should output 4567. Use Proper
logic instead of “cout”.
CODE:
#include <iostream>
using namespace std;
int main()
{
int num;
int reversenum=0;
cout<<"WELCOME TO THE OPPOSITE DIMENSION :)"<<endl;
cout<<"ENTER THE NUMBER WANTED TO BE REVERSED"<<endl;
cin>>num;
while(num>0)
{
int digit = num%10;
reversenum=reversenum*10+digit;
num/=10;
}
cout<<reversenum<<endl;
}
EXPLANATION:
In this code we solved the problem by initially initializing an integer that was named as ‘num’
and would store the value taken from the user that is to be reversed. Moreover, we have
initialized the reversed num with ‘0’. After that we take the value from the user and save it in
the int num,
Now. At the major part of the code, we use the while loop to execute our problem giving the
initial condition expression to the code as num>0, obviously a positive number can be reversed.
Then, we initialize another term named ‘digit’ which is equal to the modulus of the user entered
number with 10, this would give us the last term of the number entered by the user as the
remainder and now equal to the digit.
The code will take this first digit and multiply initially with 0 the first value of reverse number
*10 and add it to the digit, resulting in the last digit to be the first of the user entered number.
And, so on.
Briefly the overview of the while loop iterations is given below with an example:
1. First Iteration:
- digit = 7654 % 10 gives digit = 4.
- reversedNum = 0 * 10 + 4 gives reversedNum = 4.
- num /= 10 gives num = 765.
2. Second Iteration:
- digit = 765 % 10 gives digit = 5.
- reversedNum = 4 * 10 + 5 gives reversedNum = 45.
- num /= 10 gives num = 76.
3. Third Iteration:
- digit = 76 % 10 gives digit = 6.
- reversedNum = 45 * 10 + 6 gives reversedNum = 456.
- num /= 10 gives num = 7.
4. Fourth Iteration:
- digit = 7 % 10 gives digit = 7.
- reversedNum = 456 * 10 + 7 gives reversedNum = 4567.
- num /= 10 gives num = 0.
Now, the loop stops because `num` is no longer greater than 0. The original number (7654) has
been reversed, and the reversed number (4567) is the result.
Write a program that prints the following patterns separately one below the other. Use
nested for loops to generate the patterns.
A. CODE:
#include <iostream>
using namespace std;
int main()
{
for(int i=1; i<=10; i++)
{
for(int j=1; j<=i; j++)
{
cout<<"*";
}
cout<<endl;
}
}
EXPLANATION:
In this code we have used the nested for loops to make the required pattern of stars. It is to be
noted that ‘i’ represented by rows and ‘j’ represented by the columns thus as the question
suggests the j will be less than i at each of the interval to print the * pattern correctly.
The program requires 10 rows with the no. of stars increasing at each row.
B. CODE:
int main()
{
for(int i=1; i<=10; i++)
{
for(int j=10; i<=j; j--)
{
cout<<"*";
}
cout<<endl;
}
}
EXPLAINATION
In this code we have used the nested for loops to make the required pattern of stars. It is to be
noted that ‘i’ represented by rows and ‘j’ represented by the columns thus as the question
suggests the j will be less than i at each of the interval to print the * pattern correctly.
The program requires 10 rows with the no. of stars decreasing at each row.
Write a program that prints a table of the binary, octal and hexadecimal equivalents of
the decimal numbers in the range 1 through 256.
CODE:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout<<"Table:\n";
cout<<left<<setw(20)<<"Decimal:"<<setw(20)<<"Binary:"<<setw(20)<
<"Octal:"<< setw(20) << "HexaDecimal:" << endl;
//Convert to Octal
while (OctaNum != 0)
{
int Octa = OctaNum % 8;
sumOcta = sumOcta + Octa * localOcta;
//REVERTING
localOcta = localOcta * 10;
OctaNum = OctaNum / 8;
}
// CONVERTING to hexadecimal
while (HexaNum != 0)
{
int result = HexaNum % 16;
if (result < 10)
{
hexResult = char(result + '0') + hexResult; //
Adding the hexaDecial to string HexResult. Beginning of HexDecimal
}
else
{
hexResult = char(result - 10 + 'A') + hexResult; //
Adding the hexaDecial to string HexResult. Beginning of HexDecimal
}
HexaNum = HexaNum / 16;
}
cout << left << setw(20) << i << setw(20) << sumB <<
setw(20) << sumOcta << setw(20) << hexResult << endl;
}
}
EXPLANATION:
In this program we are required to convert decimal numbers into binary numbers first we
do that by first taking mod with 2 and then we divide by 2 to find the next binary digit,
however to print it in reverse/required order, we use ‘sumB’ to reverse it.
Similar is the case for octal form in which we just change the 2 with 8 at every point.
However, for hexa we replace 8 with 16 and add a condition to check for letters the
condition number mod greator than 9, in this case we use string and ASCII code
representation to reverse it.
Q.No.04 (CLO # 2,PLO #1)
A mail-order house sells five different products whose retail prices are
product 1 — $2.98,
product 2—$4.50,
product 3—$9.98,
product 4—$4.49
product 5—$6.87
#include <iostream>
using namespace std;
int main()
{
double priceprod1 = 2.98, priceprod2 = 4.50, priceprod3 = 9.98, priceprod4 =
4.49, priceprod5 = 6.87;
int productselected;
int quantitysold;
double dailyRetailPrice = 0.0;
double weeklyRetailPrice = 0.0;
do
{
cout << "Enter product number (or enter 0 to end sales for the day):
";
cin >> productselected;
switch (productselected)
{
case 1:
dailyRetailPrice += quantitysold * priceprod1;
break;
case 2:
dailyRetailPrice += quantitysold * priceprod2;
break;
case 3:
dailyRetailPrice += quantitysold * priceprod3;
break;
case 4:
dailyRetailPrice += quantitysold * priceprod4;
break;
case 5:
dailyRetailPrice += quantitysold * priceprod5;
break;
}
}
else if (productselected != 0)
{
cout << "Invalid product number. Please enter a number between 1-
5 or 0 to end sales for the day." << endl;
}
weeklyRetailPrice += dailyRetailPrice;
cout << "TOTAL RETAIL PRICE FOR DAY " << day << ": $" << dailyRetailPrice
<< endl;
dailyRetailPrice = 0.0; // Reset for the next day
}
cout << "TOTAL RETAIL PRICE FOR THE WEEK: $" << weeklyRetailPrice << endl;
return 0;
}
EXPLANATION:
The basic idea of the program is to ask the user for which product he sold in a day and to show
the sales of that day, but the twist is to repeat this act for 7 days and eventually showing the
output of the different products sold in the course of 7 days, summing the revenue of each
single/individual day.
We have to take care of the fact that the user can sell 2,3 or more products in a single day.
To overcome this problem, we ask the user in the do part of the do-while loop that which
product he sold.
Moving on, if the user selects 1-5 as the product number, the program will again ask to tell how
much of that certain product is sold by the user. Correspondingly it is to be noted that the
switch statement is working to calculate the amount of revenue generated by the user on that
specific product. Okay, now finally we bring the buddy of do, i.e., while in our code and we used
to end the sales of that specific day only if zero is pressed by the user and the program will
come out of the do while loop. Now the program being sequential will give the user revenue
generated at that day. It is to be noted that the daily revenue of each day is to be displayed after
the user entry of each day that is why it is to be resettled to zero every once the specific day is
completed. However, we also need to find the weekly revenue generated by the user so, in
order to keep all the daily revenues intact we use deposit each day’s value to the weekly
revenue variable and save it.
The loop runs for 7 days and eventually the weekly revenue is generated.
Enter the input for the table which you want to print: 5
Multiplication table of 5
CODE:
#include <iostream>
using namespace std;
int main()
{
int number;
cout<<"ENTER THE NUMBER TO FORM ITS TABLE"<<endl;
cin>>number;
EXPLANATION:
We made a multiplication table by simply using for loop running up to 12 multiples. We provide
proper format to make it look like a table.