Saturday, November 29, 2014

C++ print& prime pro



Program to enter a string and find its length

#include 
#include 
#include 
void main()
{
clrscr();
int slength;
char x[81];
cout << "Enter the string : " << endl;
cin>>x;
slength=strlen(x);
cout << "The length of the string " << x << " is " << slength << "." << endl;
getch();
}
 
Output
         
       The length of the string goldfish is 8.

Program to enter an integer and print if it is prime or composite

#include 
#include 
#include 
void main()
{
clrscr();
int num1,x;
cout << "Enter an integer : " << endl;
cin>>num1;
for(x=2;x
{
if(num1%x==0)
{
cout << num1 << " is a composite number." << endl;
getch();
exit(0);
}
else
{
cout << num1 << " is a prime number." << endl;
getch();
exit(0);
     }
   }
}
 
Output
 
23 is a prime number.

Program to enter the principal, rate & time and print the simple interest

#include 
#include 
void main()
{
clrscr();
int x;
float sinterest,principal,rate,time;
for(x=4;x>=0;x--)
{
cout << "Enter the principal, rate & time : " << endl;
cin>>principal>>rate>>time;
sinterest=(principal*rate*time)/100;
cout << "Principal = $" << principal << endl;
cout << "Rate = " << rate << "%" << endl;
cout << "Time = " << time << " years" << endl;
cout << "Simple Interest = $" << sinterest << endl;
}
getch();
}
 
Output
 
Principal = $1000
Rate = 5%
Time = 3 years
Simple Interest = $150

Program to switch between different cases

#include 
#include 
int main()
{
clrscr();
int choice;
cout << "1. Talk" << endl;
cout << "2. Eat" << endl;
cout << "3. Play" << endl;
cout << "4. Sleep" << endl;
cout << "Enter your choice : " << endl;
cin>>choice;
switch(choice)
{
case 1 : cout << "You chose to talk...talking too much is a bad habit." << endl;
break;
case 2 : cout << "You chose to eat...eating healthy foodstuff is good." << endl;
break;
case 3 : cout << "You chose to play...playing too much everyday is bad." << endl;
break;
case 4 : cout << "You chose to sleep...sleeping enough is a good habit." << endl;
break;
default : cout << "You did not choose anything...so exit this program." << endl;
}
getch();
}
 
Output
 
You chose to play...playing too much everyday is bad.

No comments:

Post a Comment