Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C++ Lab Assignment

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 13

Subject: Computer Programming

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.5 Write a program to find whether a given number is even or odd.

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.

Hint . Fahrenheit = (1.8*Celsius)+32and celsius = (Fahrenheit -32)/1.8

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.

(i) If <char> is a lower case letter, the message

“The upper case character corresponding to <char> is …”

(ii) If <char> is an upper case letter , the message

“The lower case character corresponding to <char> is …”

(iii) If <char> is not a letter, the message <char> is not a letter”.

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

Sales Commission rate

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:

Characters ASCII Range

‘0’ – ‘9’ 48-57

‘A’-‘Z’ 65-90

‘a’ –‘z’ 97-122

Other characters 0-255 excluding the above mentioned codes.

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.5 Program to print first n natural numbers and their sum.

Q.6 Program to calculate the factorial of an integer.

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

*
**

***

****

Q.10 Program to check whether a number is prime or not.

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 (a < 100) if (a < 100)

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”;

Q.3 What will be the output produced by following code fragment :

for(I = 10 ; i<=50; i+=10)

j = i/2;

cout<<j<<” “;

Q.4 What will be the output of following two code fragments? Justify your answer.

// version 1 // version 2

int f = 1, i =2; int f = 1, i =2;

while(++i<5) do {

f *=I; f *=I;

cout<<f; }while(+
+i<5);

cout<<f;

Q.5 Write a C++ program that prints 1 2 4 8 16 32 64 128.

Q.6 Write a C++ program to check whether the given number is palindrome or not.

Q.7 Write a C++ program to generate divisors of integers.

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.9 Write a C++ program to print table of a given number.

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.3 Write a C++ program to print Fibonacci series i.e.,0 1 1 2 3 5 8 . . .

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).

Q.5 Give the output of the following : -

a)# include<iostream.h> b)for(r=0;r<4;r++)

void main( ) {

{long number = 5572331, result = for (c=0;c<10;c++)


0;
do cout<<”#”;

{ result*=10; cout<<”\n”;

int digit = number % 10; }

result + =digit; c)for(i=0;i<10;i++);

number/=10; cout<<i;

}while(number); d) for(int I =1 ; i< 10 ; i++)

cout<<”Output=”<<result<<endl; cout<<i;

Q.6 Write a program to convert a lowercase character to uppercase.

(Hint : Subtracting 32 from a lowercase character gives you equivalent uppercase


character e.g., ‘b’ – 32 will give you ‘B’ and ‘W’-32 will give you ‘W’.

Q.7 Write a complete C++ program to do the following:

(i) read an integer X.

(ii)determine the number of digits n in X.

(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 : -

(i)26 -35 (ii) 36-45 (iii)46-55

Q.9 Write programs using nested loops to produce the following designs:-

a) b)
A

A B & & & & & & &

A B C & & & & &

A B C D E & & &

A B C D E F &

c) d)Write a program using nested

& loops to produce a rectangle of *

& & with 6 rows and 20 * per row.

& &

& &

& &

& & & & & & & & & & &

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 .

(ii) S = x+x2 /2 + x3/3 + ………..xn/n.

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

number 12.Each number in Pascals Triangle is one of the combinations C(n,k).If we


count

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.

Q.5 Write the function : int digit( int n , int k)

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.8 Write a program to find least common divisor of two integers.

Q.9 Write a program to sum n natural numbers starting from a given number.

Q.10 Write a program giving demonstration of function overloading.


Assignment 7

Note: For hints refer Sumita Arora.

Q.1 Write a program to print largest element of an array.(using a function).

Q.2 Write a program to find the largest and smallest elements in a vector.

Q.3 Write a program to reverse a vector without using a temporary variable.

Q.4 Write a program to add two matrices.

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.7 Write a program to multiply two matrices.

Q.8 Write a program to find transpose of a matrix.

Q.9 Write a program to concatenate two strings.

Q.10 Write a program to find the position of a pattern string in main string.

Q.11 Write a program to delete duplicate elements from a vector.

Q.12 Write a program to calculate the length of a string without using a library function.

Q.13 Write a program to count number of words present in a line.

Q.14 Write a program to delete a substring from a string.

Q.15Write a program to determine statistics of some text in terms of line-count, word-


count, character count . Use a function Text_Stat to do he same.This function receives
four parameters : text,lc,wc,cc,where last three parameters lc,wc and cc are of integer
type and are passed by reference.
Assignment 8

Write the output of the following programs:-

Q.1 #include<iostream.h> Q.2#include<iostream.h> Q.3#include<iostream.h>


#include<conio.h> #include<conio.h> #include<conio.h>
void junk( int * , int *); void junk( int * , int ); void main( )
void main( ) void main( ) { float a =13.5;
{ int i=5 , j=2; { int i=5 , j=2; float *b,*c;
junk(&i,&j); junk(&i , j); b = &a;
cout<<i<<j; cout<<i<<j; c = b;
} } cout<<a<<*(&a)<<endl;
void junk(int *i , int *j) void junk(int *i , int j) cout<<*&a<<*b<<endl;
{ { cout<<*c<<endl;
*i = *i**i; *i = *i**i; }
*j = *j**j; j = j*j;
} }
Q.4 #include<iostream.h> Q.5 #include<iostream.h> Q.7 #include<iostream.h>
#include<conio.h> void main( ) void main( )
void main( ) { int a , b = 5; { float a = 7.99999;
{ a= b+NULL; float *b , *c;
static int i = 0; cout<<a; b=&a;
i++; } c= b;
if(i<=5) Q.6#include<stdio.h> cout<<a<<*(&a);
{ cout<<i; } void main( ) cout<<*b<<*c;
main( ); { cout<<sizeof(NULL); }
} cout<<sizeof( “ “);
else }
exit( );
}
Q.8 Q.9 Q.10
#include<iostream.h> #include<iostream.h> #include<iostream.h>
#include<conio.h> #include<conio.h> #include<conio.h>
void main( ) void main( ) void display(int * , int);
{ { void main( )
int a ,*b,**c,***d,****e; int num[]={24,34,12,44,56,17}; {
a = 10; int i = 0; int num [ ]= {24,34,12,44,56 };
b= &a; int *j; display(&num[0] , 5);
c = &b; j= &num[0]; }
d = &c; while(i<=5) void display( int *j , int n)
e = &d; { cout<<&num[i]; {int i =1;
cout<<<a<<b<<c<<d<<e; cout<<*j; while(i<=n)
cout<<a<<a+*b; i++; {
cout<<**c+***d; j++; cout<<*j;
cout<<****e; } i++;
} j++;
}
}
Q.11 Q.12
#include<iostream.h> #include<iostream.h>
#include<conio.h> #include<conio.h>
void main( ) void main( )
{ {
int num[]={24,34,12,44,56}; int stud[5][2] = {
int i = 0; {1234 , 56 },
while(i<=5) {1212, 33},
{ cout <<”Address = ”<<” “<< &num[i]; {1434 , 80},
cout<<”Element = “<<num[i]<< “ “; {1312 , 78},
cout<<*(num+i)<< “ “; {1203 , 75}
cout<<*(i+num)<<” “; };
cout<<i[num]; int i , j;
i++; for( i = 0 ; i < = 4 ; i++)
} {
} cout<<”\n”;
for(j = 0 ; j< = 1 ; j++)
{ cout<<*(*(stud+i)+j)); }
}
Q.13 Q.14
#include<iostream.h> #include<iostream.h>
#include<conio.h> #include<conio.h>
void main( ) int i =20;
{ void main( )
int a[ ] [4] = { {
5,7,5,9, int i = 5;
4,6,3,1, cout<<i<<endl<<::i ;
2,9,0,6 {
}; int i = 10 ;
int *p; cout<<endl<<i<<endl<<::i ;
int (*q)[4]; }
p= (int *)a; }
q=a; Q.15
// In the above statement q holds the base address #include<iostream.h>
of 0th 1 D array. #include<conio.h>
cout<<p<<q; void main( )
p++; {
q++; int i = 5;
cout<<p<<q; int &j = i;
} int p = 10;
j=p;
cout<<endl<<i<<endl<<j;
p=20;
cout<<endl<<i<<endl<<j;
}

Assignment 9

Q.1 Write a complete program demonstrating the use of following things:-

(a)Friend Function (b) Overloading the + binary operator

(c)Copy Constructor (d) Overloading the unary – operator.

Q.2 What are the characteristics of constructor function.

Q.3 What are the characteristics of destructor function.

Q.4 What are the rules for operator overloading.

Q.5 What are the types of inheritance. Write a program for single inheritance.

Q.6 Write a program for function overloading (compile time polymorphism).

You might also like