C++ Lab Assignment
C++ Lab Assignment
C++ Lab Assignment
Branch:Mech
Assignment 1
Q.1 Write a C++ program that inputs a student marks in three subjects (out of 100) and
prints the percentage marks.
Q.2 Write a C++ program that accepts a character between a to j and prints next 4
characters.
Q.3 Write a C++ program to convert a given number of days into years, weeks and days.
Q.4 Write a program to read two numbers and print their quotient and remainder.
Q.6 Write a program to find area of triangle. Take base and height from user.
Q.7 Write a program that asks for your height in centimeters and then converts your
height to feet and inches.(1 foot = 12 inches , 1 inch = 2.54 cm)
Q.8 Write a program to convert given inches into its equivalent yards and feet.
(1 yard = 36 inches , 1 foot = 12 inches)
Q.9 Write a C++ program that reads temperature in Celsius and displays it in
fahrenheit.
Q.10 Assuming that there are 7.418 gallons in a cubic foot, write a program that asks the
user to enter the number of gallons, and then display the equivalent in cubic feet.
Assignment 2
Q.1 Write a C++ program that inputs experience and age of a person. The salary of the person is 6000 if
person is experienced and his age is more than 35, otherwise if the person is experienced and his
is more than 28 but less than 35 than the salary should be 4800 otherwise for experienced person the salar
should be 3000 and for inexperienced person the salary should be 2000.The program should be made using
ternary operator.
Q.2 A computer programming contest requires teams of five members each. Write a program that asks for the
number of players, and then gives the number of teams and number of players left over.
Q.3 The value of e is known to be 2.71828. Using this value, write a program to determine the value of the
expression: 2 – ye2y + 4y. Obtain the value of y from the user.
Q.4 Write a program to compute simple interest and compound interest. Take values from user.
Q.5 Write a program that reads in a character <char> from the keyboard and then displays one of the followin
messages: Hint. You will need to refer to a table of ASCII characters.
Q.6 Write a program to input three integers and print the largest of three.
Q.7 Write a C++ program to input a number. If the number n is odd and positive, print its square root otherwi
print n3.
Q.8 Write a C++ program to create the equivalent of a four –function calculator. The program requires the use
enter two numbers and an operator. It then carries out the specified arithmetical operation: addition,
subtraction, multiplication or division of the two numbers. Finally, it displays the result.
Q.9 Program to input a character and to print whether a given character is an alphabet, digit or any other
character.
Q.10 Program to calculate commission for the salesmen. The commission is calculated according to following rates
30001 onwards 15 %
22001-30000 10 %
12001-22000 7%
5001-12000 3%
0-5000 0%
Assignment 3
Q.1 Program to print whether a given character is an uppercase or a lowercase character or a digit or
any other character. Use ASCII codes for it. The ASCII codes are as given below:
‘A’-‘Z’ 65-90
Q.2 Program to calculate and print roots of a quadratic equation ax2+bx+c=0(where a is not equal to 0).
Q.3 Program to input number of week’s day (1-7) and translate to its equivalent name of the day of the
week. (e.g., 1 to Sunday, 2 to Monday, . . . . , 7 to Saturday).
Q.4 Program to calculate area of a circle, a rectangle or a triangle depending upon user’s choice.
Q.7 Program to calculate and print the sums of even and odd integers of the first n natural numbers
using while loop.
Q.8 Program to display a menu regarding rectangle operations and perform according to user’s response
using do-while loop and switch case.
Hint..
Rectangle menu
Display the area if the user enters number 1, 2 for perimeter, 3 for diagonal, 4 for exit, and default
case for wrong choice and to again enter a valid choice.
Q.9 Write a program using for loop to display the following output
*
**
***
****
Hint. A number is not prime if it is completely (leaves reminder 0 on dividing) divisible by 2 or any
numbers greater than 2 till half of the number itself. Any number cant be completely divisible by a
number which is greater than half of the number itself
Assignment 4
Q.1 Evaluate the following C++ expressions with the help of a program and analyze their
output where a, b, c are integers and d, f are floating point numbers. The value of a =
5, b=3 and d=1.5
I. f = a+b/a
II. c = d*a+b
III. c= (a++)*d+a
IV. f=(++b)*b-a
V. c=a-(b++)*(--a)
Q.2 What will be the output of following two code fragments? Justify your answer.
// version 1 //version 2
i = j = 10; i = j = 10;
if(b>50) { if(b>50)
++i; ++i;
else }
++j ; else
cout<<”i=”<<i<<”\n”; ++j ;
cout<<”j=”<<j<<”\n”; cout<<”i=”<<i<<”\n”;
cout<<”j=”<<j<<”\n”;
j = i/2;
cout<<j<<” “;
Q.4 What will be the output of following two code fragments? Justify your answer.
// version 1 // version 2
while(++i<5) do {
f *=I; f *=I;
cout<<f; }while(+
+i<5);
cout<<f;
Q.6 Write a C++ program to check whether the given number is palindrome or not.
Q.8 Write a C++ program to find whether a given number is odd or even prime. The
program should continue as long as the user wants.
Q.10 Write a C++ program to print sum of negative numbers, sum of positive even
numbers, and sum of positive odd numbers from a list of numbers entered by the
user. The list terminates when the user enters 0.
Assignment 5
Q.1 Write a C++ program to find out whether a year (entered in 4-digit number
representing it) is a leap year.
Q.2 Given three numbers A, B and C, write a program to write their values in
descending order. For example, if A=7,B = 4, and C=10, your program should print
out : 10, 7, 4.
Q.4 Write a C++ program to print largest even and largest odd number from a list of
numbers entered through keyboard. The list terminates as soon as one enters
0(zero).
void main( ) {
{ result*=10; cout<<”\n”;
number/=10; cout<<i;
cout<<”Output=”<<result<<endl; cout<<i;
(iii)form an integer Y that has the number of digits n at ten’s place and the most
significant digit of X at one’s place.
(iv)Output Y
(For example, if X is equal to 2134, then Y should be 42 as there are 4 digits and the
most significant number is 2)
Q.8 Write a program to accept the age of n employees and count the number of persons
in the following age group : -
Q.9 Write programs using nested loops to produce the following designs:-
a) b)
A
A B C D E F &
& &
& &
& &
& & & & & & & & & & &
Q.10 Write programs to find sum of the following series take the values of x and n from
user.
(i) S= 1 + x + x2 + x3 + ………..xn .
Assignment 6
Note : Make programs 1 to 10 using functions. For hints refer Sumita Arora.
Q.1 Write a C++ program that invokes a function calc ( ) which intakes two integers and
an arithmetic operator and prints the corresponding result.
Q.2 Write a C++ function that normally takes one argument, the address of a string, and
prints the string once. However, if a second, type int argument is provided and is non
zero and other than 1, the function prints the string a number of times equal to the
number of time that function has been called to that point.
Q.3 A positive integer is entered through the keyboard. Write a function printbinary to
find the binary equivalent of this number.
Q.4 Pascals Triangle is a triangular array of numbers that begins like this:-
1 1
1 2 1
1 3 3 1
Write a program that uses the comb( ) function to print Pascals Triangle down to row
the rows and diagonal columns starting with 0, then the number C(n,k) is in row n and
column k. For example, the number C(3, 1) = 2 is in row number 3 and column number
1.
This function returns the kth digit of the positive integer n.For example , if n is the
integer 29,415 then the call digit(n,0) would return the digit 5, and the call digit(n,2)
would return the digit 4. Note that the digits are numbered from right to left
beginning with the “zeroth digit”.
Q.6 Write a program that implements the Euclidean Algorithm to return the greatest
common divisor of the two given positive integers.
Q.7 Write a program which has a void isTriangular(int n) function. The function checks
whether n is triangular or not. .
Q.9 Write a program to sum n natural numbers starting from a given number.
Q.2 Write a program to find the largest and smallest elements in a vector.
Q.5 Write a program to find row sum and column sum of a matrix.
Q.6 Write a program to sum the elements above and below the main diagonal of a matrix.
Q.10 Write a program to find the position of a pattern string in main string.
Q.12 Write a program to calculate the length of a string without using a library function.
Assignment 9
Q.5 What are the types of inheritance. Write a program for single inheritance.