C++ Practicals With Index
C++ Practicals With Index
1. Calculate the sum of series 1/1 + 1/2 + 1/3 +……. +1/n for any positive integer n.
2. Write a user defined function to find the absolute value of an integer and use it to
evaluate the function (-1)/|n|, for n=-2,-1, 0, 1, 2.
3. Calculate the factorial of any number.
4. Read the floating numbers and compute two averages: average of negative
numbers and average of positive numbers.
5. Write a program that prompts the user to input a positive integer. It should then
output a message whether the number is prime or not.
6. Write a program that prompts the user to input the values of a, b & c involved in
the equation ax^2+bx+c=0 and outputs the type of roots of equation and all the
roots of the equation.
7. Write a program that generates random integer between 0 and 99. Given that the
first two Fibonacci numbers are 0 and 1 generate all Fibonacci numbers less
than or equal to generated number.
8. Write a program that does the following:
● prompts the user to input five decimal numbers
● prints the five numbers
● converts each decimal number to the nearest integer
● adds the five integers
● prints the sums and average of them.
9. Write a program that uses while loops to perform and following steps:
● prompts the user inputs two integers :firstNum secondNum
● output all odd and even numbers between firstNum and secondNum
● output the sum of all the even numbers between firstNum and secondNum
● output the sum of the square of the odd numbers firstNum and secondNum
● output all uppercase letters corresponding to the numbers between first number
and second number, if any.
10. Write a program that prompts the user to input five decimal numbers. The
program should then add the five numbers, converts the sum to the nearest
integer and prints the result.
11. Write a program that prompts the user to enter the lengths of the three sides of a
triangle and then outputs a message indicating whether the triangle is a right
triangle and scalene triangle.
12. Write the value returning function smaller to determine the smallest number from
a set of numbers. Use this function to determine the smallest number from a set
of 10 numbers.
13. Write a function that takes as a parameter an integer (as a long value) and
returns the number of odd, even, and zero digits. Also write a program to test
your function.
14. Enter 100 digits into an array and sort them in ascending/descending order and
print the largest/smallest integer.
15. Enter 10 integers into an array and then search for a particular integer in the
array.
16. Multiplication /addition of two matrices using two dimensional arrays.
17. Using arrays, read the vectors of the following type: a= (1 2 3 4 5 6 7 8) b= (0 2 3
4 0 1 5 6) and compute the product and addition of these vectors.
18. Read from a text file and write to a text file.
19. Write a program to create the following grid using for loops:
12345
23456
34567
45678
56789
20. Write a function, reversedigit that takes an integer as a parameter and returns
the number with its digits reversed.
Ques1. CALCULATE THE SUM OF SERIES 1/1+1/2+1/3....+1/N FOR ANY POSITIVE INTEGER N
#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int n;
double sum=0.0;
cout<<"Enter a positive integer n: ";
cin>>n;
for(int i=1;i<=n;i++)
{
sum+=(float)1/i;
}
cout<<"sum of series 1+1/2+1/3+...+1/n for n="<<n<<" is "<<sum;
getch();
}
Ques2. WRITE A USER DEFINED FUNCTION TO FIND THE ABSOLUTE VALUE OF AN INTEGER AND USE IT
TO EVALUATE THE FUNCTION ((-1)^N)/|N|,FOR N=-2,-1,0,1,2.
#include<iostream>
#include<conio.h>
#include<cmath>
using namespace std;
int absolute(int n)
{
if(n<0)
return -n;
else
return n;
}
main()
{
int num,abs;
double func=0;
for(int i=1;i<=5;i++)
{
cout<<"Enter an integer: ";
cin>>num;
abs=absolute(num);
func= pow(-1,num)/abs;
cout<<"Value of function (-1)^n/|n| for n="<<num<<" is "<<func;
cout<<endl;
}
getch();
}
#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int n,fact=1;
cout<<"Enter a positive integer n: ";
cin>>n;
for(int i=n;i>=1;i--)
{
fact*=i;
}
cout<<"Factorial of "<<n<<" is "<<fact;
getch();
}
Ques4. READ FLOATING NUMBERS AND COMPUTE AVERAGES:
a) THE AVERAGE OF NEGATIVE NUMBERS
b) AVERAGE OF POSITIVE NUMBERS
#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int n,neg=0,pos=0;
double negsum=0.0,possum=0.0,num;
cout<<"Enter how many numbers you wish to enter: ";
cin>>n;
for(int i=1;i<=n;i++)
{
cout<<"Enter a floating point number: ";
cin>>num;
if(num<0)
{
negsum+=num;
neg++;
}
else
{
possum+=num;
pos++;
}
}
cout<<"Average of negative numbers= "<<negsum/neg<<endl;
cout<<"Average of positive numbers= "<<possum/pos<<endl;
getch();
}
Ques5. WRITE A PROGRAM THAT PROMPTS THE USER TO INPUT A POSITIVE INTEGER. IT SHOULD
THEN OUTPUT A MESSAGE INDICATING WHETHER THE NUMBER IS PRIME OR NOT.
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
main()
{
int num,i,count=0;
if(num==1)
{
cout<<"It is neither prime nor composite";
}
else
{
if(num%2==0)
{
if(num==2)
count=0;
else
count=1;
}
else{
for(i=3;i<=sqrt(num);i=i+2)
{
if(num%i==0)
{
count=1;
break;
}
}
if(count==0)
cout<<"prime number"<<endl;
else
cout<<"Not a Prime number"<<endl;
}
getch();
}
Ques6. WRITE A PROGRAM THAT PROMPTS THE USER TO INPUT THE VALUE OF a,b AND c INVOLVED
IN THE EQUATION ax^2+bx+c=0 AND OUTPUTS THE TYPE OF THE ROOTS OF THE EQUATION. ALSO THE
PROGRAM SHOULD OUTPUT ALL THE ROOTS OF THE EQUATION.
#include<iostream>
#include<conio.h>
#include<cmath>
using namespace std;
main()
{
double a,b,c,x,y,d;
cout<<"Enter coefficients of x^2, x and constant term of quadratic equation : ";
cin>>a>>b>>c;
cout<<endl;
d=(pow(b,2)-4*a*c);
if(d==0)
{
cout<<"Equation has single repeated root as follows:"<<endl;
x = (-b)/(2*a);
cout<<"x = "<<x;
}
else if(d>0)
{
cout<<"Equation has two real roots as follows:"<<endl;
x=(-b+sqrt(d))/(2*a);
y=(-b-sqrt(d))/(2*a);
cout<<"x = "<< x <<" , "<<y<<endl;
}
else
{
cout<<"Equation has imaginary roots."<<endl;
x=((-b)+sqrt(-d))/(2*a);
y=(-b-sqrt(-d))/(2*a);
cout<<"x = "<< ((-b)/(2*a))<<"+ i"<<(sqrt(-d)/(2*a))<<" , "<<((-b)/(2*a))<<"- i"<<(sqrt(-d)/(2*a))<<endl;
}
getch();
}
Ques7. WRITE A PROGRAM THAT GENERATES RANDOM INTEGER BETWEEN 0 AND 99. GIVEN THAT
FIRST TWO FIBONACCI NUMBERS ARE 0 AND 1,GENERATE ALL THE FIBONACCI NUMBERS LESS THAN
OR EQUAL TO GENERATED NUMBER.
#include<iostream>
#include<conio.h>
#include<cstdlib>
#include<ctime>
using namespace std;
main()
{
int first,second,third,num;
num=(rand() + time(0)) % 100;
cout<<"Random number generated= "<<num<<endl;
first=0;
second=1;
if(num==0)
cout<<"Fibonacci series till "<<num<<" is "<<first<<endl;
else if(num==1)
cout<<"Fibonacci series till "<<num<<" is "<<first<<" "<<second<<endl;
else
{
cout<<"Fibonacci series till "<<num<<" is "<<endl;
while((first+second)<num)
{
third=first+second;
first=second;
second=third;
cout<<third<<" ";
}
}
getch();
#include<iostream>
#include<conio.h>
using namespace std;
main()
{ int intsum=0,intnum;
double num,sum=0.0;
cout<<"Enter five decimal numbers: "<<endl;
for(int i=1;i<=5;i++)
{
cin>>num;
intnum=static_cast<int>(num+0.5);
intsum+=intnum;
Ques9. WRITE A PROGRAM THAT USES whileLOOPS TO PERFORM THE FOLLOWING STEPS:
a) PROMPT THE USER TO INPUT TWO INTEGERS: FIRSTNUM AND SECONDNUM (FIRSTNUM SHOULD
BE LESS THAN SECONDNUM)
b) OUTPUT ALL ODD AND EVEN NUMBERS BETWEEN FIRSTNUM AND SECONDNUM.
c) OUTPUT THE SUM OF ALL EVEN NUMBERS BETWEEN FIRSTNUM AND SECONDNUM.
d) OUTPUT THE SUM OF THE SQUARES OF THE ODD NUMBERS BETWEEN FIRSTNUM AND
SECONDNUM.
e) OUTPUT ALL UPPERCASE LETTERS CORRESPONDING TO THE NUMBERS BETWEEN FIRSTNUM AND
SECONDNUM.
#include<iostream>
#include<conio.h>
#include<cmath>
using namespace std;
main()
{
int firstNum,secondNum,evensum=0,oddsum=0; //Input two integers(first<second)
cout<<"Input two integers: ";
cin>>firstNum>>secondNum;
cout<<endl;
while(firstNum>secondNum)
{
cout<<"Enter another integer: ";
cin>>secondNum;
}
int i=firstNum+1; //Print even and odd numbers between first and second
while(i<secondNum)
{
if(i%2==0)
cout<<i<<" ";
i++;
evensum+=i; //sum of even numbers
}
i=firstNum+1;
while(i<secondNum)
{
if(i%2!=0)
cout<<i<<" ";
i++;
oddsum+=pow(i,2); //sum of square of odd numbers
}
cout<<endl;
cout<<"Sum of even numbers between "<<firstNum<<" and "<<secondNum<<" = "<<evensum<<endl;
cout<<"Sum of square of odd numbers between "<<firstNum<<" and "<<secondNum<<" =
"<<oddsum<<endl;
getch();
}
Ques10. WRITE A PROGRAM THAT PROMPTS THE USER TO INPUT 5 DECIMAL NUMBER. THE
PROGRAM SHOULD THEN ADD THE 5 DECIMAL NUMBERS, CONVERT THE SUM TO THE NEAREST
INTEGER, AND PRINT THE RESULT.
#include<iostream>
#include<conio.h>
using namespace std;
main()
{
double num,sum=0.0;
cout<<"Enter five decimal numbers: "<<endl;
for(int i=1;i<=5;i++)
{
cin>>num;
sum+=num;
}
sum=static_cast<int>(sum+0.5);
cout<<"Nearest integer to sum = "<<sum<<endl;
getch();
}
Ques11. WRITE A PROGRAM THAT PROMPTS THE USER TO ENTER THE LENGTHS OF THREE SIDES OF A
TRIANGLE AND THEN OUTPUTS A MESSAGE INDICATING WHETHER A TRIANGLE IS A RIGHT TRIANGLE
OR A SCALENE TRIANGLE.
#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int a,b,c;
cout<<"Enter length of three sides of a triangle : ";
cin>>a>>b>>c;
cout<<endl;
if((a*a==b*b+c*c)||(b*b==a*a+c*c)||(c*c==b*b+a*a))
cout<<"It is a right angled triangle."<<endl;
else
cout<<"Not a right angled triangle."<<endl;
cout<<endl;
if((a!=b)&&(a!=c))
cout<<"It is a scalene triangle."<<endl;
else
cout<<"Not a right angled triangle."<<endl;
getch();
}
Ques12. WRITE A VALUE RETURNING FUNCTION SMALLER TO DETERMINE THE SMALLEST NUMBER
FROM A SET OF NUMBERS. USE THIS FUNCTION TO DETERMINE THE SMALLEST NUMBER FROM THE
SET OF 10 NUMBERS.
#include<iostream>
#include<conio.h>
using namespace std;
main()
{int num,maxnum,least;
least=smaller(num,maxnum);
#include<iostream>
#include<conio.h>
digitType(long num, int &zero, int &even, int &odd) //function to check the type of digit
{
int digit;
while(num!=0)
{
digit=num%10;
if(digit==0)
zero++;
switch(digit%2)
{
case 0: even++;
break;
case -1:
case 1: odd++;
break;
}
num=num/10;
}
main()
{
long n;
int evencount=0,oddcount=0,zerocount=0;
cout<<"Enter an integer: ";
cin>>n;
cout<<endl;
digitType(n,zerocount,evencount,oddcount);
getch();
}
Ques14. ENTER 100 INTEGERS INTO AN ARRAY AND SHORT THEM IN AN ASCENDING AND
DESCENDING ORDER AND PRINT THE LARGEST AND SMALLEST INTEGERS.
#include<iostream>
#include<conio.h>
using namespace std;
main()
{int n,a;
int A[n];
cout<<"Enter integers: ";
for(int i=0;i<n;i++) //Input array
{
cin>>A[i];
}
}
}
cout<<endl;
}
}
cout<<endl;
cout<<"Smallest integer= "<<A[0]<<endl;
cout<<"Largest integer= "<<A[n-1]<<endl;
getch();
}
Ques15. ENTER 10 INTEGERS INTO AN ARRAY AND THEN SEARCH FOR A PARTICULAR INTEGER IN THE
ARRAY.
#include<iostream>
#include<conio.h>
using namespace std;
main()
{ int num,position,ans=0;
int A[10],i;
double average=0;
cout<<endl;
if(ans==1)
cout<<"The integer entered exists at position "<<i+1<<" in array A "<<endl;
else
cout<<"The integer entered does not exist in array A "<<endl;
getch();
Ques16. MULTIPLICATION AND ADDITION OF TWO MATRICES USING TWO DIMENSIONAL ARRAY.
#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int A[10][10],B[10][10],C[10][10],r1,c1,r2,c2,i,j,k;
if((r1==r2)&&(c1==c2))
{
cout<<"Sum of matrices A and B: "<<endl;
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
cout<<A[i][j]+B[i][j]<<" ";
}
cout<<endl;
}
}
else
cout<<"Matrices cannot be added."<<endl;
if(c1==r2)
{
}
else
cout<<"Matrices can't be multiplied."<<endl;
getch;
}
Ques17. USING ARRAYS, READ THE VECTORS OF THE FOLLOWING TYPE:
A=(1 2 3 4 5 6 7 8), B=(0 2 3 4 0 1 5 6)
AND COMPUTE THE PRODUCT AND ADDITION OF THESE VECTORS.
#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int A[8] = {1,2,3,4,5,6,7,8};
int B[8] = {0,2,3,4,0,1,5,6};
cout<<"Array A : ";
for(int i=0;i<8;i++)
{
cout<<A[i]<<" ";
}
cout<<endl;
cout<<"Array B : ";
for(int i=0;i<8;i++)
{
cout<<B[i]<<" ";
for(int i=0;i<8;i++)
{
cout<<A[i]+B[i]<<" ";
}
cout<<endl;
for(int i=0;i<8;i++)
{
cout<<A[i]*B[i]<<" ";
}
cout<<endl;
getch();
#include<iostream>
#include<fstream>
#include<string>
#include<conio.h>
using namespace std;
main()
{
ifstream inFile; //Declare Variables
ofstream outFile;
string name,college,subject;
int rollno;
char ch;
inFile.open("input.txt");
outFile.open("output.txt");
cout<<"Processing data"<<endl;
inFile>>name;
outFile<<"Name: "<<name<<endl;
inFile>>rollno;
outFile<<"Roll No: "<<rollno<<endl;
inFile.get(ch); //to read newline character
getline(inFile,subject);
outFile<<"Subject: "<<subject<<endl;
getline(inFile,college);
outFile<<"Institution: "<<college<<endl;
inFile.close();
outFile.close();
getch();
}
Ques19. WRITE A PROGRAM TO CREATE THE FOLLOWING GRID USING FOR LOOPS.
12345
23456
34567
45678
56789
#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=i;j<5+i;j++)
{
cout<<j<<" ";
}
cout<<endl;
}
getch();
}
#include<iostream>
#include<conio.h>
using namespace std;
reverseDigit(int n)
{int digit;
while(n>0)
{
digit=n%10;
cout<<digit;
n=n/10;
}
cout<<endl;
}
main()
{
int num,i,count;
cout<<"How many numbers you want to enter? ";
cin>>count;
cout<<endl;
for(i=1;i<=count;i++)
{
cout<<"Enter an integer: ";
cin>>num;
if(num<0)
{
num=-num;
cout<<"-";
}
reverseDigit(num);
}
getch();