Programs on Library Functions in C++. |
- Write a program which input principal, rate and time from user and calculate compound interest. You can use library function.
#include<iostream.h>
#include<conio.h>
#include<math.h>
int main()
{
float P,R,T,CI;
cout<<"Enter Principal, Rate and Time : ";
cin>>P>>R>>T;
CI=P*pow((1+R/100),T) - P;
cout<<"Compound Interest is : "<<CI;
getch();
return 0;
}
Output:
Enter Principal, Rate and Time : 4500 6.5 3
Compound Interest is : 935.774
- Write a program to compute area of triangle. Sides are input by user.
Area = sqrt(s*(s-a)*(s-b)*(s-c)) ,where s=(a+b+c)/2.
#include<iostream.h>
#include<conio.h>
#include<math.h>
int main()
{
float a,b,c,s,Area;
cout<<"Enter three sides of triangle : ";
cin>>a>>b>>c;
s=(a+b+c)/2;
Area=sqrt(s*(s-a)*(s-b)*(s-c));
cout<<"Area of triangle is : "<<Area;
getch();
return 0;
}
Output:
Enter three sides of triangle : 5.6 8.1 10.3
Area of triangle is : 22.5651
#include<iostream.h>
#include<conio.h>
#include<math.h>
int main()
{
float a,b,c,s,Area;
cout<<"Enter three sides of triangle : ";
cin>>a>>b>>c;
s=(a+b+c)/2;
Area=sqrt(s*(s-a)*(s-b)*(s-c));
cout<<"Area of triangle is : "<<Area;
getch();
return 0;
}
Output:
Enter three sides of triangle : 5.6 8.1 10.3
Area of triangle is : 22.5651
- Write a program to check character entered is alphabet, digit or special character using library functions.
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#include<stdio.h>
int main()
{
char ch;
cout<<"Enter any character :";
ch=getchar();
if(isalpha(ch))
cout<<"Alphabet";
else if(isdigit(ch))
cout<<"Number";
else
cout<<"Special Character";
getch();
return 0;
}
Output:
SAMPLE RUN # 1
Enter any character : Z Alphabet
SAMPLE RUN # 2
Enter any character : 9 Number
SAMPLE RUN # 3
Enter any character : $ Special Character
- Write a program which display a number between 10 to 100 randomly.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int n;
randomize();
n=random(91)+10;
cout<<"The randomly selected number is :"<<n;
getch();
return 0;
}
Output:
SAMPLE RUN # 1
The randomly selected number is : 34
SAMPLE RUN # 2
The randomly selected number is : 45
SAMPLE RUN # 3
The randomly selected number is : 76
- Write a program which accept a letter and display it in uppercase letter.
#include<iostream.h>
int main()
{
char ch;
cout<<"\nEnter any character : ";
cin>>ch;
cout<<"ASCII equivalent is : "<<static_cast<int>(ch);
return 0;
}
- Write a C++ program to implement the Guessing Number game. In this game the machine selects a random number between 1 and 100, and in as few attempts as possible the player tries to guess the number.
- The computer tells him each time the player enters a guess whether the guess is too high, too low, or ok. Once the amount is determined by the player, the game is over.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int num, guess, tries = 0;
srand(time(0)); //seed random number generator
num = rand() % 100 + 1; // random number between 1 and 100
cout << "Guess My Number Game\n\n";
do
{
cout << "Enter a guess between 1 and 100 : ";
cin >> guess;
tries++;
if (guess > num)
cout << "Too high!\n\n";
else if (guess < num)
cout << "Too low!\n\n";
else
cout << "\nCorrect! You got it in " << tries << " guesses!\n";
} while (guess != num);
cin.ignore();
cin.get();
return 0;
}
Output:
Guess My Number Game
Enter a guess between 1 and 100 : 67 Too high!
Enter a guess between 1 and 100 : 34 Too high!
Enter a guess between 1 and 100 : 15 Too low!
Enter a guess between 1 and 100 : 27 Too high!
Enter a guess between 1 and 100 : 23 Too low!
Enter a guess between 1 and 100 : 26 Too high!
Enter a guess between 1 and 100 : 25
Correct! You got it in 8 guesses!