25
SERIAL NUMBER TITLE SIGNATURE 1. Swap using two variables Swap using three variables Convert Celsius to Farenheit

Some Questions on C++

Embed Size (px)

DESCRIPTION

solved programs for elementary C++

Citation preview

Page 1: Some Questions on C++

SERIAL NUMBER TITLE SIGNATURE1. Swap using two

variablesSwap using three variablesConvert Celsius to Farenheit

Page 2: Some Questions on C++

SWAP USING 2 VARIABLES

#include<iostream.h>#include<conio.h>

void main(){clrscr();int a,b; //variable declarationscout<<"This program will swap the values of two

variables."<<endl;

//input of the valuescout<<"Enter the value of variable a: ";cin>>a;cout<<"Enter the value of variable b: ";cin>>b;a=a+b; //swapping the valuesb=a-b;a=a-b;

//output of valuescout<<"The new value of variable a is "<<a<<endl;cout<<"The new value of variable b is "<<b;getch();

}

Output:This program will swap the values of two variables.Enter the value of variable a: 6Enter the value of variable b: 4The new value of variable a is 4The new value of variable b is 6

SWAP USING 3 VARIABLES#include<iostream.h>#include<conio.h>

void main(){//swap using 3 variablesclrscr();int a,b,c; //variable declarationscout<<"This program will swap the values of 2 variables by

using a third variable."<<endl;//taking input

cout<<"Enter the value of variable a: ";

Page 3: Some Questions on C++

cin>>a;cout<<"Enter the value of variable b: ";cin>>b;c=a; //swappinga=b;b=c;

//outputcout<<"The new value of variable a is "<<a<<endl;cout<<"The new value of variable b is "<<b;getch();

}

Output:This program will swap the values of two variables.Enter the value of variable a: 6Enter the value of variable b: 4The new value of variable a is 4The new value of variable b is 6

CONVERT CELSIUS TO FARENHEIT

#include<iostream.h>#include<conio.h>

void main(){//program to convert celsius to farenheitclrscr();float c,f; //variable declarationscout<<"This program will change the value of temperature

entered in Celsius to that in Farenheit."<<endl;cout<<"Enter the temperature in celsius : ";cin>>c; //input in celsiusf=(c*9/5)+32; //calculating in farenheitcout<<"The temperate in Farenheit is "<<f;getch();

}

Output:This program will change the value of temperature entered in Celsius to that in Farenheit.Enter the temperature in celsius : 10The temperature in Farenheit is 50

SORT 5 NUMBERS

Page 4: Some Questions on C++

#include<iostream.h>#include<conio.h>

int a[5];void sorter(int,int);int main(){

clrscr();cout<<"This program will sort 5 numbers."<<endl;

//input of valuesfor(int j=0;j<5;j++){

cout<<"Enter speed of car "<<j+1<<" : ";cin>>a[j];

}sorter(0,4);

//output of valuescout<<"The speed of cars in increasing order is "<<endl;for(int k=0;k<5;k++)cout<<a[k]<<" ";getch();return 0;

}

void sorter(int x,int y){//define the pivot and the wallint wall=0;//pivot is a[y] by defaultfor(int i=x;i<y;i++){

if (a[i]<a[y]){ //condition to check whether the element is lesser than the pivot

int b=a[x+wall]; //swapping the element and shifting the wall

a[x+wall]=a[i];a[i]=b;wall++;

}}int c=a[y];a[y]=a[x+wall];a[x+wall]=c;if(x!=(x+wall)) sorter(x,x+wall-1);if((x+wall)!=y) sorter(x+wall+1,y);

}

Output:This program will sort 5 numbers.Enter the speed of car 1 : 210Enter the speed of car 2 : 310Enter the speed of car 3 : 720Enter the speed of car 4 : 100

Page 5: Some Questions on C++

Enter the speed of car 5 : 540The speed of cars in increasing order is100 210 310 540 720

PRINT REPORT CARD

#include<iostream.h>#include<conio.h>

void main(){//program to print report cardclrscr();int m1,m2,m3,m4,m5,aggr; //variable declarationscout<<"This program will calculate aggregate percentage from

five percentages and print the report card."<<endl;cout<<"Enter the percentages of the 5 subjects:"<<endl;cout<<"Subject 1 : "; //inputscin>>m1;cout<<"Subject 2 : ";cin>>m2;cout<<"Subject 3 : ";cin>>m3;cout<<"Subject 4 : ";cin>>m4;cout<<"Subject 5 : ";cin>>m5;aggr= (m1+m2+m3+m4+m5)/5; //calculating aggregatecout<<"Your aggregate percentage is "<<aggr<<"%"<<endl;

//displaying appropriate gradeif(aggr>=80)cout<<"You got grade A!";else if (aggr>=60)cout<<"You got grade B!";else cout<<"Sorry you got no grade.";getch();

}

Output:This program will calculate aggregate percentage from five percentages and print the report card.Subject 1 : 85Subject 2 : 85Subject 3 : 85Subject 4 : 85Subject 5 : 85Your aggregate percentage is 85%You got grade A!

DAY OF THE WEEK

Page 6: Some Questions on C++

#include<iostream.h>#include<conio.h>

void main(){//program to print day of the weekclrscr();int a;cout<<"This program will print the day of the week,

corresponding to the number entered by the user."<<endl;cout<<"Enter a number from 1 to 7 : ";cin>>a; //input of valuesswitch(a){ //check the value given by user

case 1 : cout<<"Today is Sunday";break;case 2 : cout<<"Today is Monday";break;case 3 : cout<<"Today is Tuesday";break;case 4 : cout<<"Today is Wednesday";break;case 5 : cout<<"Today is Thursday";break;case 6 : cout<<"Today is Friday";break;case 7 : cout<<"Today is Saturday";break;default : cout<<"Error! Please enter a whole number from

1 to 7.";}getch();

}

Output:Enter a number from 1 to 7 : 3Today is Tuesday

CHECK FOR PRIME NUMBER AND SUM OF PRIME NUMBERS

#include<iostream.h>#include<conio.h>

void main(){//program to check for prime number and print sum of prime

numbersclrscr();int n,i,j,x=0,sum=0;

Page 7: Some Questions on C++

cout<<"This program will check whether a number is prime or not. Further, it will find the sum of all prime numbers between 1 and the given number."<<endl;

cout<<"Enter a number : "; //inputcin>>n;for(i=2;i<n;i++){ /*for loop to check whether number is

prime or not*/if(n%i==0)x=1;

}if(x==1)cout<<"The number is composite."<<endl;else cout<<"The number is prime."<<endl;

for(i=3;i<=n;i++){ /*for loop to check all numbers for prime and to add the ones which are prime*/

x=0;for(j=2;j<i;j++){

if(i%j==0)x=1;}if(x==0)sum+=i;

}sum+=2;cout<<"Sum of prime numbers from 1 to this number is "<<sum;getch();

}

Output:This program will check whether a number is prime or not. Further, it will find the sum of all prime numbers between 1 and the given number.Enter a number : 13The number is prime.Sum of prime numbers from 1 to this number is 41

FACTORIAL OF A NUMBER

#include<iostream.h>#include<conio.h>

void main(){//program to find factorialclrscr();int n,i; //variable declarationslong long long unsigned int fac=1;cout<<"This program will find the factorial of a given

number."<<endl;

Page 8: Some Questions on C++

cout<<"Enter a number : "; //inputcin>>n;for(i=1;i<=n;i++)fac*=i; //for loop to calculate factorialcout<<"Factorial of the number is "<<fac;getch();

}

Output:This program will find the factorial of a given number.Enter a number : 5Factorial of the number is 120

FIBONACCI SERIES

#include<iostream.h>#include<conio.h>

void main(){//fibonacciclrscr();int fn=0,sn=1,tn,n;cout<<"This program will print the fibonacci series."<<endl;cout<<"Enter the number of terms of the fibonacci series you

want to print : ";cin>>n;cout<<"0 1 ";for(int i=3;i<=n;i++){

tn=fn+sn;cout<<tn<<" ";fn=sn;sn=tn;

}getch();

}

Output:This program will print the fibonacci series.Enter the number of terms of the fibonacci series you want to print : 7

0 1 1 2 3 5 8

MENU DRIVEN PROGRAM TO PERFORM THE FOLLOWING FUNCTIONS: calculate sum of digits, number of digits, reverse of number, check for palindrome and Armstrong number

#include<iostream.h>

Page 9: Some Questions on C++

#include<conio.h>#include<math.h>

void main(){//menu drivenclrscr();int a,n,dig=0,sum=0,rev=0,nn,arm=0;cout<<"Please select one of the given options: "<<endl;cout<<"1. Find sum of digits of a number."<<endl;cout<<"2. Find the number of digits in a number."<<endl;cout<<"3. Find the reverse of a number."<<endl;cout<<"4. Check whether a number is a palindrome or

not."<<endl;cout<<"5. Check whether a number is an armstrong number or

not."<<endl;cin>>a;switch(a){ //check what the user wants

case 1: cout<<"Enter a number : ";cin>>n;for(;n>0;){ //for loop to find sum of digits

sum+=(n%10);n/=10;

}cout<<"The sum of its digits is "<<sum;getch();break;

case 2: cout<<"Enter a number : ";cin>>n;for(;n>0;){ //for loop to find no. of digits

dig++;n/=10;

}cout<<"The number of digits is "<<dig;getch();break;

case 3: cout<<"Enter a number : ";cin>>n;for(;n>0;){ //for loop to reverse number

rev+=(n%10);rev*=10;n/=10;

}rev/=10;cout<<"The reverse of the number is "<<rev;getch();break;

case 4: cout<<"Enter a number : ";cin>>n;

Page 10: Some Questions on C++

nn=n;for(;n>0;){ //for loop to reverse number

rev+=(n%10);rev*=10;n/=10;

}rev/=10;

//check whether it is a palindromeif(rev==nn)cout<<"The number is a palindrome.";else cout<<"The number is not a palindrome.";getch();break;

case 5: cout<<"Enter a number : ";cin>>n;nn=n;for(;n>0;){

dig++;n/=10;

}n=nn;for(;n>0;){

arm+=pow(n%10,dig);n/=10;

}//check whether it is an Armstrong number or notif(arm==nn)cout<<"The number is an armstrong

number.";else cout<<"The number is not an armstrong number.";getch();break;

}}

Output:Please select one of the given options: 1. Find sum of digits of a number.2. Find the number of digits in a number.3. Find the reverse of a number.4. Check whether a number is a palindrome or not.5. Check whether a number is an armstrong number or not.1Enter a number : 123The sum of its digits is 6

Page 11: Some Questions on C++

MENU DRIVEN PROGRAM TO PRINT FOLLOWING PYRAMIDS

#include<iostream.h>#include<conio.h>#include<iomanip.h>

void main(){//pyramidclrscr();int a,n,i,j;char b=65;cout<<"1. Print pyramid 1"<<endl;cout<<"2. Print pyramid 2"<<endl;cout<<"3. Print pyramid 3"<<endl;cout<<"4. Print pyramid 4"<<endl;cout<<"5. Print pyramid 5"<<endl;cout<<"6. Print pyramid 6"<<endl;cout<<"7. Print pyramid 7"<<endl;cout<<"8. Print pyramid 8"<<endl;cout<<"What do you choose?"<<endl;cin>>a;switch(a){ //check what the user wants

case 1: cout<<"Enter the number of rows of the pyramid you want to print : ";

cin>>n;for(i=1;i<=n;i++){ //for loop tocycle through row

for(j=1;j<=i;j++)cout<<j; //print elementscout<<endl;

}break;

case 2: cout<<"Enter the number of rows of the pyramid you want to print : ";

cin>>n;for(i=n;i>=1;i--){ //for loop tocycle through row

for(j=i;j>=1;j--)cout<<j; //print elementscout<<endl;

Page 12: Some Questions on C++

}break;

case 3: cout<<"Enter the number of rows of the pyramid you want to print : ";

cin>>n;for(i=1;i<=n;i++){ //control the row

for(j=1;j<=i;j++)cout<<j; //print elementscout<<endl;

}for(i=n;i>=1;i--){ //to print the reverse pattern

for(j=i;j>=1;j--)cout<<j;cout<<endl;

}break;

case 4: cout<<"Enter the number of rows of the pyramid you want to print : ";

cin>>n;for(i=1;i<=n;i++){ //cycle through rows

b=65; //since 65 is ASCII code for Afor(j=1;j<=i;j++){ //print elements

cout<<b;b++;

}cout<<endl;

}break;

case 5: cout<<"Enter the number of rows of the pyramid you want to print : ";

cin>>n;for(i=1;i<=n;i++){ //cycle through rows

for(j=1;j<=i;j++){ //print elementscout<<b;b++;

}cout<<endl;

}break;

case 6: cout<<"Enter the number of rows of the pyramid you want to print : ";

cin>>n;for(i=1;i<=n;i++){ //cycle through rows

cout<<setw(i); //for spacingfor(j=n;j>=i;j--)cout<<j; //print elementscout<<endl;

}break;

case 7: cout<<"Enter the number of rows of the pyramid you want to print : ";

Page 13: Some Questions on C++

cin>>n;for(i=1;i<=n;i++){ //cycle through rows

cout<<setw(30-i); //for spacingfor(j=1;j<=i;j++)cout<<"* ";cout<<endl;

}break;

case 8: cout<<"Enter the number of rows of the pyramid you want to print : ";

cin>>n;for(i=1;i<=n;i++){ //cycle through rows

cout<<setw(n-i+1); //for spacingfor(j=1;j<=i;j++)cout<<i;cout<<endl;

}break;

}getch();}

Output:1. Print pyramid 12. Print pyramid 23. Print pyramid 34. Print pyramid 45. Print pyramid 56. Print pyramid 67. Print pyramid 78. Print pyramid 8What do you choose?1Enter the number of rows of the pyramid you want to print : 41121231234

MENU DRIVEN PROGRAM TO PRINT THE FOLLOWING TRIANGLES

Page 14: Some Questions on C++

#include<iostream.h>#include<conio.h>#include<iomanip.h>

void main(){//print patternsclrscr();int a,n,i,j;cout<<"This program will print the pattern based on what the

user wants."<<endl;cout<<"1. Print Floyd's Triangle"<<endl;cout<<"2. Print Pascal's Triangle"<<endl;cout<<"3. Print pyramid"<<endl;cout<<"Choose an option : ";cin>>a;switch(a){

case 1: cout<<"Enter the number of rows of the triangle you want to print : ";

cin>>n;int b=1;for(i=1;i<=n;i++){ //cycle through rows

for(j=1;j<=i;j++)cout<<b++;//print elementscout<<endl;

}break;case 2: cout<<"Enter the number of rows of the

triangle you want to print : ";cin>>n;int num,dec,x; //initializing variablesfor(i=1;i<=n;i++){ //for loop starts

cout<<setw(30-i);x=1;dec=1; //denominatornum=i-1; //numeratorfor(j=1;j<=i;j++){

if(x!=0)cout<<x<<" ";x=(x*num--)/dec++; //calculation

}cout<<endl;

}break;

Page 15: Some Questions on C++

case 3: cout<<"Enter the number of rows of the triangle you want to print : ";

cin>>n;for(i=1;i<=n;i++){ //print forward pattern

cout<<setw(30-i); //for spacingfor(j=1;j<=i;j++)cout<<j;j-=2;for(;j>=1;j--)cout<<j;cout<<endl;

}i-=2;for(;i>=1;i--){ //print backward pattern

cout<<setw(30-i);for(j=1;j<=i;j++)cout<<j;j-=2;for(;j>=1;j--)cout<<j;cout<<endl;

}break;

}getch();

}

Output:

1. Print Floyd's Triangle2. Print Pascal's Triangle3. Print pyramidChoose an option : 1Enter the number of rows of the triangle you want to print : 3123456

MENU DRIVEN PROGRAM TO DO THE FOLLOWING WITHOUT USING MATH.H: Calculate x to the power y, square root of a number, sum of all even digits of a number, all possible combinations of a number

#include<iostream.h>#include<conio.h>

void swap(int,int);int abc[3];void main(){

clrscr();

Page 16: Some Questions on C++

int a,x,y,i,sum=0; //variable initializationsfloat sqrt;long long long unsigned int pow;cout<<"1. Calculate power of a number."<<endl;cout<<"2. Calculate square root of a number."<<endl;cout<<"3. Find sum of all even digits of a number."<<endl;cout<<"4. Find all possible numbers that can be derived from a

3 digit number."<<endl;cout<<"Choose an option : ";cin>>a;switch(a){

case 1: cout<<"To find A to the power B, enter A = ";cin>>x;cout<<"Enter B = ";cin>>y;pow=1;for(i=1;i<=y;i++)pow*=x; //calculate powercout<<endl<<"The result is "<<pow;

break;case 2: cout<<"Enter the number you want to find the

square root of : ";cin>>x;sqrt=0;

//calculate whole number part of rootfor(sqrt=0;(sqrt*sqrt)<=x;sqrt++){}sqrt--;for(;(sqrt*sqrt)<=x;sqrt+=0.1){}

//calculate 1st decimal digitsqrt-=0.1;for(;(sqrt*sqrt)<=x;sqrt+=0.01){}

//calculate second decimal digitsqrt-=0.01;cout<<"The square root of the number is "<<sqrt;

break;case 3: cout<<"Enter a number : ";

cin>>x;for(;x>0;x/=10)if((x%10)%2==0)sum+=(x%10);cout<<"The sum of even digits of the number is

"<<sum;break;

case 4: cout<<"Enter a 3 digit number : ";cin>>x;for(i=0;i<3;i++){

abc[i]=x%10;x/=10;

}for(i=1;i<7;i++){

if(i%2==0)swap(1,2);

Page 17: Some Questions on C++

else swap(0,2);for(int j=0;j<3;j++)cout<<abc[j];cout<<" ";

}}getch();

}

void swap(int p,int q){abc[p]=abc[p]+abc[q];abc[q]=abc[p]-abc[q];abc[p]=abc[p]-abc[q];

}Output:

1. Calculate power of a number.2. Calculate square root of a number.3. Find sum of all even digits of a number.4. Find all possible numbers that can be derived from a 3 digit number.Choose an option : 2Enter the number you want to find the square root of : 3The square root of the number is 1.73

TO CHECK WHETHER CHARACTER ENTERED BY USER IS UPPERCASE/LOWERCASE ALPHABET OR DIGIT

#include<iostream.h>#include<conio.h>#include<ctype.h>

void main(){clrscr();char a;cout<<"Enter a digit or an alphabet : ";cin>>a;if (isalnum(a)){ //to check if its alphabet or digit

if (isalpha(a)){//to check if its alphabet//to check for upper/lowercase

if (isupper(a))cout<<"It is an uppercase alphabet.";else cout<<"It is a lowercase alphabet.";

}else cout<<"It is a digit.";

}else cout<<"Error!";getch();

Page 18: Some Questions on C++

}

Output:Enter a digit or an alphabet : AIt is an uppercase alphabet.

MENU DRIVEN PROGRAM USING USER DEFINED FUNCTIONS TO FIND: reverse of number, sum of digits, factorial of a number

#include<iostream.h>#include<conio.h>

//function prototypesvoid factorial(int b);void reverse(int b);void sumofdigits(int b);void main(){

clrscr();int a,n;cout<<"1. Find the reverse of a given number."<<endl;cout<<"2. Find the sum of digits of a number."<<endl;cout<<"3. Find the factorial of a number."<<endl;cout<<"Choose an option : ";cin>>a;cout<<"Enter a number : ";cin>>n;switch(a){

case 1: reverse(n); //function callsbreak;case 2: sumofdigits(n);break;case 3: factorial(n);

}getch();

}

void reverse(int b){ //function to find reversecout<<"Reverse of the number is ";for(;b>0;b/=10)cout<<b%10;

}void factorial(int b){ //function to find factorial

cout<<"Factorial of the number is ";int fac=1;for(int i=1;i<=b;i++)fac*=i;cout<<fac;

}void sumofdigits(int b){ //function to find sum of digits

cout<<"Sum of digits of the number is ";

Page 19: Some Questions on C++

int sum=0;for(;b>0;b/=10)sum+=(b%10);cout<<sum;

}

Output:1. Find the reverse of a given number.2. Find the sum of digits of a number.3. Find the factorial of a number.Choose an option : 1Enter a number : 123Reverse of the number is 321

MENU DRIVEN PROGRAM TO CALCULATE THE FOLLOWING SERIES’

#include<iostream.h>#include<conio.h>#include<math.h>

void series1(); //function prototypesvoid series2();void series3();void series4();int fac(int b);int term(int c,int d);void main(){

clrscr();int a;cout<<"1. Calculate the result of series 1."<<endl;cout<<"2. Calculate the result of series 2."<<endl;cout<<"3. Calculate the result of series 3."<<endl;cout<<"4. Calculate the result of series 4."<<endl;cout<<"Choose an option : ";cin>>a;switch(a) //to check what user wants{

case 1: series1(); //function callsbreak;case 2: series2();break;case 3: series3();break;case 4: series4();

Page 20: Some Questions on C++

break;}getch();

}

void series1(){int n;float result=0,temp;cout<<"Enter the number of terms : ";cin>>n;for(int i=1;i<=n;i++){ //calculation of series

temp=sqrt(i);result+=temp;

}cout<<"The result is "<<result;

}

void series2(){int x,n;float result;cout<<"Enter the value of x : ";cin>>x;cout<<"Enter the number of terms : ";cin>>n;for(int i=1;i<=n;i++)result+=(pow(x,i)/fac(i));cout<<"The result is "<<result;

}

int fac(int b){ //function to find factorialint fact=1;for(int i=1;i<=b;i++)fact*=i;return fact;

}

void series3(){int x,n,sign=1,result=0;cout<<"Enter the value of x : ";cin>>x;cout<<"Enter the number of terms : ";cin>>n;for(int i=1;i<=n;i++){ //calculate series

result+=((sign*pow(x,i))/fac(i+1));sign*=-1;

}cout<<"The result is "<<result;

}

void series4(){

Page 21: Some Questions on C++

int x,n,result=0;cout<<"Enter the value of x : ";cin>>x;cout<<"Enter the value of n : ";cin>>n;for(int i=1;i<=n;i++)result+=term(i,x); //adding all termscout<<"The result is "<<result;

}

int term(int c,int d){ //function to calculate 1 term of series4int finalterm=0;for(int j=1;j<=c;j++){

finalterm+=((pow(d,(j*2)))/fac(j*2));}return finalterm;

}

Output:1. Calculate the result of series 1.2. Calculate the result of series 2.3. Calculate the result of series 3.4. Calculate the result of series 4.Choose an option : 1Enter the number of terms : 2The result is 2.414