0% found this document useful (0 votes)
13 views

CPP File

This document contains 16 programming questions and their solutions in C++. The questions cover topics like inputting and calculating student marks and percentages, character manipulation, date conversion, area calculations, inheritance, structures, classes and operator overloading. Each question is presented with the problem statement and the C++ code to solve it. The code examples demonstrate basic programming concepts in C++ like input/output, conditional statements, functions, classes and inheritance.

Uploaded by

khuship200102
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

CPP File

This document contains 16 programming questions and their solutions in C++. The questions cover topics like inputting and calculating student marks and percentages, character manipulation, date conversion, area calculations, inheritance, structures, classes and operator overloading. Each question is presented with the problem statement and the C++ code to solve it. The code examples demonstrate basic programming concepts in C++ like input/output, conditional statements, functions, classes and inheritance.

Uploaded by

khuship200102
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Computer Programming December 30, 1899

S.No. ASSIGNMENT PAG DATE OF REMARK


E SUBMISSIO S
N

Q.1 Write a C++ program that inputs a student marks in three


subjects (out of 100) and prints the percentage marks.

Page | 1
23T1017
Computer Programming December 30, 1899

#include<iostream>

using namespace std;

int main(){

int m1,m2,m3;

cout<<"Enter marks of phy out of 100:";

cin>>m1;

cout<<"Enter marks of maths out of 100:";

cin>>m2;

cout<<"Enter marks of chem out of 100:";

cin>>m3;

float t=( m1+ m2+ m3)/3;

cout<< "percentage is:"<<t;

return 0;

Page | 2
23T1017
Computer Programming December 30, 1899

Q.2 Write a C++ program that accepts a character between a to j


and prints next 4 characters.

#include<iostream>

using namespace std;

int main(){

char ch;

cout<<"Enter any character between a to j:";

cin>>ch;

cout<<"The next four characters are:"<<endl;

int i=1;

while(i<=4){

char nextch=ch+i;

cout<<nextch<<endl;i++;

}}

Page | 3
23T1017
Computer Programming December 30, 1899

Q.3 Write a C++ program to convert a given number of days into


years, weeks and days.

#include<iostream>

using namespace std;

int main(){

int days,years,weeks,daysleft,remainingdays;

cout<<"Enter number of days:";

cin>>days;

years=days/365;

daysleft=days%365;

weeks=daysleft/7;

remainingdays=daysleft%7;

cout<<"The number of years are : "<<years<<endl;

cout<<"The number of weeks are:"<<weeks<< '\n' ;

cout<<"The number of days are:"<<remainingdays<<'\t';

Page | 4
23T1017
Computer Programming December 30, 1899

Q.4 Write a program to read two numbers and print their


quotient and remainder.

#include<iostream>

using namespace std;

int main(){

int a,b;

cout<<"Enter first no";

cin>>a;

cout<<"Enter second no";

cin>>b;

float Q=(float)a/b;

int R=a%b;

cout<<"The quoteint is:"<<Q<<endl;

cout<<"The remainder is:"<<R<<endl;

Page | 5
23T1017
Computer Programming December 30, 1899

Q.5 Write a program to find whether a given number is even or


odd.

#include<iostream>

using namespace std;

int main(){

long int n;

cout<<"Enter any no:";

cin>>n;

if(n%2==0)

cout<<"The given number is even";

}else

{ cout<<"The given number is odd";}

Page | 6
23T1017
Computer Programming December 30, 1899

Q.6 Write a program to find area of triangle. Take base and


height from user.

#include<iostream>

using namespace std;

int main(){

int b,h;

cout<<"Enter the base";

cin>>b;

cout<<"Enter the height";

cin>>h ;

float area = 1/2.00*b*h;

cout<<"The area of triangle is="<<area<<"units";}

Page | 7
23T1017
Computer Programming December 30, 1899

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)

#include<Iostream>

using namespace std;

int main(){

int h;

cout<<"Enter height in centimeters:";

cin>>h;

float i=h/2.54;

float f=i/12;

cout<<"The height in inches is:"<<i<<endl;

cout<<"The height in foots is:"<<f<<endl;}

Page | 8
23T1017
Computer Programming December 30, 1899

Q.8 Write a program to convert given inches into its equivalent


yards and feet.
(1 yard = 36 inches , 1 foot = 12 inches)

#include<iostream>

using namespace std;

int main(){

int l;

cout<<"Enter the length in inches:";

cin>>l;

float f=l/12;

float y=l/36;

cout<<"The height in foot is:"<<f<<endl;

cout<<"The height in yards is:"<<y<<endl;

Page | 9
23T1017
Computer Programming December 30, 1899

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
#include<iostream>

using namespace std;

int main(){

float C,F;

cout<<"Enter the temp in C:";

cin>>C;

F=1.8*C+32;

cout<<"The temp in F:"<<F<<endl;

Page | 10
23T1017
Computer Programming December 30, 1899

Q.1 Assuming that there are 7.418 gallons in a cubic foot, write
0 a program that asks the user to enter the number of
gallons, and then display the equivalent in cubic feet.

#include<iostream>

using namespace std;

int main(){

float G;

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

cin>>G;

float C=G/7.418;

cout<<"The number of cubic foot is:"<<C<<endl;

Q.11 Using recursions make a c++ program to print the factorial of a number.
code
#include<iostream.h>

Page | 11
23T1017
Computer Programming December 30, 1899

#include<conio.h>
int fact(int n){
if(n==1){
return 1;}
else{return n*fact(n-1);}}
void main(){
clrscr();
int num;
cout<<"enter any no.";
cin>>num;
int f=fact(num);
cout<<f;
getch();}
output

Q.12 Make a c++ program using class to find sum and average through inheritance.
Code
#include<iostream.h>
#include<conio.h>
class base{ protected:

int x;
public:
void get(){
cout<<"enter x ";
cin>>x;}};
class derived1:public base{
Page | 12
23T1017
Computer Programming December 30, 1899

protected:
int r, y;
public: void add(){
cout<<"enter y ";
cin>>y;
r=x+y;
cout<<"sum of x and y is:"<<" "<<r<<endl;}};
class derived2:public derived1{
protected :
int sum,z;
float avg;
public:
void a(){
cout<<"enter z ";
cin>>z;
cout<<"the average is:";
sum=x+y+z;
avg=(float)sum/3;
cout<<avg;}};
void main(){
clrscr();
derived1 d1;
derived2 d2;
d2.get();
d2.add();
d2.a();
getch();}
output

Page | 13
23T1017
Computer Programming December 30, 1899

Q.13 Make a c++ program using class to add two numbers by passing arguments to object.
Code
#include<iostream.h>
#include<conio.h>
class add{ int a,b,sum;
public:
add(int x,int y){
a=x;
b=y;
sum=x+y;}
void display(){
cout<<sum;}};
void main(){
clrscr();
add A(10,20);
A.display();
getch();}
output

Page | 14
23T1017
Computer Programming December 30, 1899

Q.14 Make a c++ program using structure to take roll numbers and names from user and
display it.
Code
#include<iostream.h>
#include<conio.h>
struct student{
int rollno;
char name[10];
float percent;};
void main(){
clrscr();
student s[3];
for(int i=0;i<3;i++){
cout<<"enter rollno:";
cin>>s[i].rollno;
cout<<"enter name:";
cin>>s[i].name;
cout<<"enter percent:";
cin>>s[i].percent;
}
for(int j=0;j<3;j++){
cout<<"rollno "<<s[j].rollno<<"\n";
cout<<"name "<<s[j].name<<"\n";
cout<<"percent "<<s[j].percent<<"\n"; }
getch();}
output

Page | 15
23T1017
Computer Programming December 30, 1899

Q.15 Make a c++ program using class to take roll number and name from user and display
it.
Code
#include<iostream.h>
#include<conio.h>
class student{ int rollno;
char name[10];
public:
void getdata();
void showdata();}s;
void student::getdata(){
cout<<"enter rollno. and name:";
cin>>rollno;
cin>>name;}
void student::showdata(){
cout<<"the roll no. and name are:"<<rollno<<" and "<<name;}
void main(){ clrscr();
s.getdata();

Page | 16
23T1017
Computer Programming December 30, 1899

s.showdata();
getch();}
output

Page | 17
23T1017
Computer Programming December 30, 1899

Q.16 Make a c++ program to make a class and show areas of triangle,square and circl using
the class.

Code

#include<iostream.h>

#include<conio.h>

class area{ public:

void triangle(float h,float b){

cout<<h*b*1/2<<"\n";}

void square(int s){

cout<<s*s<<"\n";}

void circle(float r){

cout<<22/7*r;}};

void main(){ clrscr();

area a;

a.triangle(2.3,4.3);

a.square(4);

a.circle(2.6);

getch();}

output

Page | 18
23T1017
Computer Programming December 30, 1899

Q.17 Make a c++ program to show complex numbers sum using operator overloading.

Code

#include<iostream.h>

#include<conio.h>

class complex{ public:

int real,img;

complex(int x,int y){

real=x;

img=y; }

complex operator+(complex &c){

complex sum(0,0);

sum.real=real+c.real;

sum.img=img+c.img;

return sum;}};

void main(){ clrscr();

complex c1(1,2);

complex c2(3,6);

complex c3=c1+c2;

cout<<"first complex no is: "<<c1.real<<"+"<<"i"<<c1.img<<"\n";

cout<<"second complex no is:"<<c2.real<<"+"<<"i"<<c2.img<<"\n";

cout<<"their sum is: "<<c3.real<<"+"<<"i"<<c3.img;

getch();}

output

Page | 19
23T1017
Computer Programming December 30, 1899

Q.18 Make a c++ program to swap values through pass by value.


Code
#include<iostream.h>
#include<conio.h>
void swap(int x,int y){
int v=x;
x=y;
y=v;
cout<<"values of and b after swapping:"<<"a="<<x<<" b="<<y;}
void main(){ clrscr();
int a=3;
int b=5;
cout<<"values of a and b before swapping:"<<"a="<<a<<" b="<<b<<"\n";
swap(a,b);
getch();}
output

Page | 20
23T1017
Computer Programming December 30, 1899

Q.19 Make a c++ program to swap values through pass by refarence.


Code
#include<iostream.h>
#include<conio.h>
void swap(int &x,int &y){
int v=x;
x=y;
y=v; }
void main(){clrscr();
int a=1;
int b=4;
cout<<"vlaues of a and b before swapping:"<<"a="<<a<<" b="<<b<<"\n";
swap(a,b);
cout<<"values of a and b after swapping:"<<"a="<<a<<" b="<<b<<"\n";
getch();}
output

Page | 21
23T1017
Computer Programming December 30, 1899

Q.20 Make a c++ program to print triangle pattern .


Code
#include<iostream.h>
#include<conio.h>
void main(){
clrscr();
int n;
cin>>n;
for(int i=n;i>=1;i--){
for(int j=1;j<=n-i;j++){
cout<<" ";}
for(int z=1;z<=2*i-1;z++){
cout<<"*";
}cout<<"\n"; }
getch();}
output

Page | 22
23T1017
Computer Programming December 30, 1899

Q.21 Make a c++ program to print triangle pattern .


Code
#include<iostream.h>
#include<conio.h>
void main(){
clrscr();
int n;
cin>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
cout<<"*"; }
cout<<"\n";}
getch();}
output

Page | 23
23T1017
Computer Programming December 30, 1899

Q.22 Make a c++ program to print triangle pattern .


Code
#include<iostream.h>
#include<conio.h>
void main(){
clrscr();
int n;
cin>>n;
for(int i=1;i<=n;i++){
for(int j=n;j>i;j--){
cout<<" ";}
for(int z=1;z<=i;z++){
cout<<"*";}
cout<<"\n";}
getch();}
output

Page | 24
23T1017
Computer Programming December 30, 1899

Q.23 Make a c++ program to print triangle pattern .

Code

#include<iostream.h>

#include<conio.h>

void main(){

clrscr();

int n;

cin>>n;

for(int i=n;i>=1;i--){

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

cout<<"*";}

cout<<"\n";}

getch();}

output

Page | 25
23T1017
Computer Programming December 30, 1899

Q.24 Make a c++ program to print sum of real and imaginery part of two complex numbers
separately.

Code

#include<iostream.h>

#include<conio.h>

class complex;

class calculator{ public:

int add(int a,int b){return(a+b);}

int sumreal(complex,complex);

int sumimg(complex,complex);};

class complex{int a,b;

friend class calculator;

public:

void setno(int n1,int n2){ a=n1;b=n2;}

void printno(){cout<<"your no. is:"<<a<<" + "<<b<<"i";}};

int calculator::sumreal(complex o1,complex o2){return(o1.a+o2.a);}

int calculator::sumimg(complex o1,complex o2){return(o1.b+o2.b);}

void main(){ clrscr();

complex o1,o2;

o1.setno(1,4);

o2.setno(5,7);

calculator calc;

int r=calc.sumreal(o1,o2);

cout<<"the sum of real o1 and o2 is:"<<r<<"\n";

int s=calc.sumimg(o1,o2);

cout<<"the sum of imaginery o1 and o2 is:"<<s; getch();}

Page | 26
23T1017
Computer Programming December 30, 1899

Q.25 Make a c++ program to print Fibonacci series.


Code
#include<iostream.h>
#include<conio.h>
int fib(int n){
if(n==0 || n==1)
return n;
return fib(n-1)+fib(n-2);}
void main(){
clrscr();
int n,i=0;
cout<<"enter the no. of terms of series:";
cin>>n;
while(i<n){
cout<<fib(i)<<" ";
i++;}
getch();}
output

Page | 27
23T1017
Computer Programming December 30, 1899

Q.26 Make a c++ program to print table .


Code
#include<iostream.h>
#include<conio.h>
float table(int n,int x){
if(n==1)
return x;
return (table((n-1),x)+x);}
void main(){
clrscr();
int n,x;
cout<<"enter the term till which table is to be shown:";
cin>>n;
cout<<"enter the no for which table is to be shown:";
cin>>x;
for(int i=1;i<=n;i++){
cout<<table(i,x)<<" ";}
getch();}
output

Q.27 Make a c++ program to show multiplication of matrices .


Code
#include<iostream.h>

Page | 28
23T1017
Computer Programming December 30, 1899

#include<conio.h>
void main(){
clrscr();
int arr1[3][3];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
cin>>arr1[i][j];}}
int arr2[3][3];
for(int a=0;a<3;a++){
for(int b=0;b<3;b++){
cin>>arr2[a][b];} }
int arr3[3][3];
for(int r=0;r<3;r++){
for(int c=0;c<3;c++){
arr3[r][c]=0;
for(int k=0;k<3;k++){
arr3[r][c]+=arr1[r][k]*arr1[k][c];}}}
for(int h=0;h<3;h++){
for(int j=0;j<3;j++){
cout<<arr3[h][j]<<" ";}cout<<"\n";}
getch();}
output

Page | 29
23T1017
Computer Programming December 30, 1899

Q.28 Make a c++ program to show addition of matrices .


Code
#include<iostream.h>
#include<conio.h>
void main(){
int arr1[3][3];
int arr2[3][3];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
cin>>arr1[i][j];}}

Page | 30
23T1017
Computer Programming December 30, 1899

for(int a=0;a<3;a++){
for(int b=0;b<3;b++){
cin>>arr2[a][b];}}
int arrsum[3][3];
for(int x=0;x<3;x++){
for(int y=0;y<3;y++){
cout<<arr1[x][y]+arr2[x][y]<<" ";}
cout<<endl;}
getch();}
output

Page | 31
23T1017
Computer Programming December 30, 1899

Q.29 Make a c++ progam to find the maximum element from an array.
Code
#include<iostream.h>
#include<conio.h>
void main(){clrscr();
int arr[6];
for(int i=0;i<6;i++){
cin>>arr[i];}
int max=arr[0];
for(int j=0;j<6;j++){
if(arr[j]>max){
max=arr[j];}}
cout<<"the maximum element of array is:"<<arr[j];
getch();}
output

Page | 32
23T1017
Computer Programming December 30, 1899

Q.30 Make a c++ program to sort an array .

Code

#include<iostream.h>

#include<conio.h>

void main(){clrscr();

int arr[8]={2,5,1,8,3,5,9,3};

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

for(int j=i+1;j<8;j++){

if(arr[i]<arr[j]){ }

else{int t=arr[i];

arr[i]=arr[j];

arr[j]=t; }}}

cout<<"sorted array ";

for(int z=0;z<8;z++){

cout<<arr[z]<<" ";}

getch();}

output

Page | 33
23T1017

You might also like