01 Sample Programs C++
01 Sample Programs C++
01 Sample Programs C++
Programs related to
Basic Operators
Operator precedence
Loops
If
Switch
Case
Cin
Cout
WRITE
#include<iostream>
using namespace std;
int main()
{
int a, b, c;
cout<<"enter a : ";
cin>>a;
cout<<"enter b : ";
cin>>b;
c=a;
a=b;
b=c;
cout<<"\n new a=
"<<a;
cout<<"\nanew
b="<<b;
return 0;
}
FIND
note
cout
<<
// insertion operator
cin
>>
#include<iostream>
using namespace std;
if (num1>num2)
{
cout << "1st number is
int main()
greater.";
{
}
int num1, num2;
else
cout << "please enter the first number : {
";
cout << "2nd number is
cin >> num1;
greater.";
cout << "please enter the second
}
number :";
return 0;
cin >> num2;
}
/*note: inside if condition if you
more than one condition dont
forget to use opening and closing
{ } brackets
For only one condition it is not
required*/
Write
Operator precedence
Continued..
find x
X=5+8<14-2||6>3
X=6+7>=12&&(3+4)>2*4
Answer..
X=1
X=0
#include<iostream>
using namespace std;
int main ()
{
int no1,no2,sum;
cout<<"ENTER FIRST NUMBER : ";
cin>>no1;
cout<<"ENTER SECOND NUMBER :";
cin>>no2;
sum=no1+no2;
cout<<"THE SUM OF TWO NUMBER : "<<sum;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
float quotient, remainder;
int no1,no2;
cout<<"\nENTER THE DIVIDEND : ";
cin>> no1;
cout<<"\nENTER THE DIVISOR : ";
cin>>no2;
quotient=no1/no2;
remainder=no1%no2;
cout<<"\nTHE QUOTIENT IS "<<quotient;
cout<<"\n\nTHE REMAINDER IS "<<remainder;
return 0;
}
// CLASSIFY THE TOTAL NUMBER OF DAYS INPUT INTO YEAR, WEEK AND DAYS
#include<iostream.h>
using namespace std;
int main()
{
long num, temp, days, week, year;
char ch;
do
{
cout<<"ENTER THE TOTAL NUMBER OF DAYS : ";
cin>>num;
year=num/365;
temp=num%365;
week=temp/7;
days=temp%7;
cout<<"\n"<<year<<" years " << week << "
weeks " << days << " days.";
cout<<"\n\nRepeat? (y/n) : ";
cin>>ch;
}while(ch=='y'||ch=='Y');
return 0;
}
DISPLAY
MONTH CORRESPONDING TO
INPUT, USING IF-ELSECONSTRUCT
else if(n==5)
cout<<n<<" IMPLIES
else if(n==6)
cout<<n<<" IMPLIES
else if(n==7)
cout<<n<<" IMPLIES
else if(n==8)
cout<<n<<" IMPLIES
else if(n==9)
cout<<n<<" IMPLIES
else if(n==10)
cout<<n<<" IMPLIES
else if(n==11)
cout<<n<<" IMPLIES
else if(n==12)
cout<<n<<" IMPLIES
return 0;
}
MAY.";
JUNE.";
JULY.";
AUGUST.";
SEPTEMBER.";
OCTOBER.";
NOVEMBER.";
DECEMBER.";
MONTH
#include<iostream.h>
int main()
{
int m;
cout<<"ENTER A NUMBER : ";
cin>>m;
while(m>12||m<1)
{
cout<<"INVALID ENTRY. RE-ENTER YOUR CHOICE :
";
cin>>m;
}
switch(m)
{
case 1 : cout<<"JANUARY";break;
case 2 : cout<<"FEBRUARY"; break;
case 3 : cout<<"MARCH"; break;
case 4 : cout<<"APRIL"; break;
case 5 : cout<<"MAY"; break;
case 6 : cout<<"JUNE"; break;
case 7 : cout<<"JULY"; break;
case 8 : cout<<"AUGUST"; break;
case 9 : cout<<"SEPTEMBER"; break;
case 10 : cout<<"OCTOBER";break;
case 11 : cout<<"NOVEMBER"; break;
case 12 : cout<<"DECEMBER"; break;
}
return 0;
}
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char ch;
cout<<"ENTER GRADE : ";
cin>>ch;
switch(ch)
{
case 'a':
case 'A':
cout<<"\nEXCELLENT"<<endl;
break;
case 'b':
case 'B':
cout<<"\nGOOD"<<endl;
break;
case 'c':
case 'C':
cout<<"\nO.K."<<endl;
break;
case 'd':
case 'D':
cout<<"\nPOOR"<<endl;
break;
default:
cout<<"\nINVALID GRADE :"<<endl;
}
getch();
}
#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
float num1,num2,den1,den2,result; char sign,
choice;
do
{
clrscr();
cout<<"FRACTION 1 : "<<endl;
cout<<"\nENTER THE NUMERATOR : ";
cin>>num1;
cout<<"\nENTER THE DENOMINATOR :";
cin>>den1;
cout<<"\nENTER THE CALCULATION OPERATOR (+,
-, *, /) : ";
cin>>sign;
cout<<"\nFRACTION 2 : "<<endl;
cout<<"\nENTER THE NUMERATOR : ";
cin>>num2;
cout<<"\nENTER THE DENOMINATOR :";
cin>>den2;
if(num1==0 || num2==0 || den1==0|| den2==0)
switch(sign)
{ case '+': result=((num1/den1)+(num2/den2));
break;
case '-': result=((num1/den1)-(num2/den2)); break;
case '*': result=((num1/den1)*(num2/den2));
break;
case '/': result=((num1/den1)/(num2/den2)); break;
default :
cout<<"\nOOPS!!!INVALID CALCULATION
OPERATOR. BYE!!";
exit(0);
}
cout<<"\nRESULT IS : "<<result;
cout<<"\n\nDO YOU WISH TO RE-EXECUTE THE
PROGRAM? (Y/N) : ";
cin>>choice;
}while(choice=='y'||choice=='Y');
}
#include<iostream.h>
Using namespace std;
Int main ()
{
int num;
for (num=1; num<=100; num ++)
{
cout <<num<<"\t";
}
Return 0;
}
#include<iostream.h>
Using namespace std;
int main ()
{
long num, fact; int count;
cout << "ENTER A NUMBER : ";
cin >> num;
for ( fact=1, count=0; fact<=num/2; fact=fact+1)
{
if (num%fact==0)
count=count+1;
}
if (count==1)
cout << "\nYES! ITS IS A PRIME NUMBER." <<
endl;
else
cout << "\nNO! IT IS NOT A PRIME NUMBER." <<
endl;
return 0;
}
#include<iostream.h>
#include<conio.h>
void main()
{
int num, multi=0;
clrscr();
cout<<"\nPLEASE ENTER THE NUMBER : ";
cin>>num;
cout<<"\nTHE MULTIPLES OF " <<num <<" are: "
<<endl << endl;
do
{
multi++;
cout <<num << " X " << multi << " = "
<<num*multi << endl;
} while(multi<10);
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
int num, multi;
clrscr();
cout<<"\nPLEASE ENTER THE NUMBER : ";
cin>>num;
cout<<"\nTHE MULTIPLES OF " <<num <<" are: "
<<endl << endl;
for(multi=1; multi<=10; multi++)
{
cout<< num<<" X " <<multi << " = " <<
num*multi <<endl;
}
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
int sum=0, i,n, t;
clrscr();
cout<<"Enter the number of terms : ";
cin>>n;
cout<<"\n";
for(i=2, t=1; t<=n; t++,i=i+2)
{
cout<<sum<<"\t";
sum+=i;
}
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
/*FIBONACCI SERIES */
char choice;
do
{
clrscr();
long double num, a=1, b=0, count;
cout<<"ENTER THE NUMBER OF ELEMENTS
REQUIRED : ";
cin>>num;
cout<<"\n\n";
for(count=1; count<=num/2;count++)
{
a+=b;
cout<<b << "\t";
b+=a;
cout<<a << "\t";
}
cout<<"\n\nREPEAT THE SAME PROGRAM? (Y/N) :
";
cin>>choice;
}while(choice=='y'||choice=='Y');
#include<iostream.h>
#include<conio.h>
void main()
{
long double first, last, sum ; char choice;
do
{
clrscr();
cout<<"\n\nPLEASE ENTER THE FIRST TERM :";
cin>>first;
cout<<"\n\nPLEASE ENTER THE LAST TERM :";
cin>>last;
for(sum=0;first<=last;first++)
sum=sum+first;
cout<< "\n\n THE FINAL SUM IS : " << sum;
cout<<"\n\nPRESS Y TO REPEAT ELSE EXIT
PRESSING ANY KEY : ";
cin>>choice;
}while(choice=='y' || choice=='Y');
}
%400==0)
julian=366;
else
julian=365;
switch(month)
{
case 1: julian -= 0;
case 2: julian -= 31;
case 3: if ( (year%100!=0 && year%4==0) || year
%400==0)
julian -= 29;
else
julian -= 28;
case 4: julian -= 31;
case 5: julian -= 30;
case 6: julian -= 31;
case 7: julian -= 30;
case 8: julian -= 31;
case 9: julian -= 31;
case 10: julian -= 30;
case 11: julian -= 31;
case 12: julian -= 30;
}
julian+=date;
fours=(year-1)/4;
void main()
{
int array[10], count=0, sum1=0, sum2=0,
sum3=0;
clrscr();
//*********input**************
cout<<"\nENTER 10 NUMBERS FOR THE ARRAY :"
<<endl<<endl;
for(count=0;count<10;count++)
{
cin >> array[count];
}
//************process***********
for(count=0 ; count<10 ; count++)
{
if(count%2!=0)
sum1=sum1+array[count];
else
sum2=sum2+array[count];
}
for(count=2 ; count<10 ; count=count+3)
{
sum3=sum3+array[count];
}
//**************output***********
do
{
clrscr();
cout<<"\nENTER THE STRING:\n\n";
cin.getline(str, 70);
len=strlen(str);
cout<<"\nENTER A CHARACTER: ";
cin.get(ch);
flag='n';
for(count=0;str[count]!=len;count++)
{
if(ch==str[count])
{
flag='Y';
break;
}
}
if(flag=='Y')
{
cout<<"\n";
cout.put(ch);
cout<<" is contained in the string : \n\n";
cout.write(str,len);
}
else
TO INDEX
#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
void main()
{
clrscr();
char str[50];
int flag=1;
cout<<"\nENTER A STRING : \n\n";
cin.getline(str,50);
for(int i=0; str[i]!='\0';i++)
{
if(islower(str[i]))
{
flag=1;
str[i]=toupper(str[i]);
}
}
if((flag==1)||(str[i]=='\0'))
{
cout<<"\nUPPERCASE STRING IS :\n\n";
cout<<str;
}
{
char ch;
int i;
float amt[50], big;
float large(float array[],int n);
clrscr();
for(i=0;i<50;i++)
{
cout<<"ENTER ELEMENT NUMBER : "<<i+1<<" :
";
cin>>amt[i];
cout<<"WANT TO ENTER MORE? (Y/N): ";
cin>>ch;
if(ch!='y'&& ch!='Y')
break;
}
if(i<50)
i++;
big=large(amt, i);
cout<<"\nTHE LARGEST ELEMENT OF THE ARRAYIS
: "<<big<<endl;
getch();
}
float large(float array[], int n)
{
RETURN TO
OF DIGITS RETURN TO
INDEX
#include<iostream.h>
#include<conio.h>
void pyramid()
{
static int n=0;
int p, m ,q;
n++;
for(p=1;p<=n;p++)
{
for(q=0;q<=n-p;q++)
cout<<' ';
m=p;
for(q=1;q<=p;q++)
cout<<m++<<' ';
cout<<endl;
}
cout<<endl;
}
void main()
{
int i;
clrscr();
for(i=0;i<5;i++)
#include<conio.h>
int lcd(int a,int b)
{
int i,j=2,flag=1;
if(a>b)
i=a;
else
i=b;
while((j<=i)&&(flag))
{
if((a%j==0)&&(b%j==0))
flag=0;
else
j++;
}
if(flag)
j=1;
return j;
}
void main()
{
clrscr();
int x, y, z;
cout<<"\nENTER 2 NUMBERS WHOSE LCD IS TO
BE FOUND : ";
}
int getdata(void)
{
int marks;
void err_msg(void);
do
{
cout<<"ENTER MARKS : ";
cin>>marks;
if(marks<0||marks>100)
err_msg();
}while(marks<0||marks>100);
return marks;
}
void err_msg(void)
{
cout<<"MARKS ARE OUT OF 100"<<endl;
cout<<"\nPLEASE ENTER MARKS BETWEEN 0 TO
100";
cout<<"\nTRY AGAIN";
getch();
}
float calcperc(int s1,int s2,int s3)
{
float per;
clrscr();
cout<<"\nWELCOME!!!WELCOME!!!\n\nTO THE
MOST PRESTIGIOUS RESTAURANT.\
\n'THE AFFORDABLES''!!!";
for(r3=7; r3<=73; r3++)
{
gotoxy(r3,16);
cout<<"*";
gotoxy(r3,22);
cout<<"*";
}
for(r1=0, r2=17; r1<=4; r1++, r2++)
{
gotoxy(7,r2);
cout<<"*";
gotoxy(73,r2);
cout<<"*";
}
gotoxy(9,19);
cout << "PLEASE ENTER YOUR NAME : ";
cin.getline ( rachit, 24);
}
void hello()
{
switch(choice)
*********************************
//TERNARY OPERATOR
#include<iostream.h>
#include<conio.h>
void main()
{
int count=0;
int num1=7, num2=4;
clrscr();
count=(num1<num2)?num1:num2;
cout<<count;
getch();
}
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string user_name;
cout << "Please enter your name: ";
cin >> user_name;
cout << "Hi " << user_name << "\n";
}
//doesnt consider a space.characters upto
//space or enter key are considered for cout
//If you want to put two strings together, known as appending one string onto another, you can
use the +sign:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string user_first_name;
string user_last_name;
cout << "Please enter your first name: ";
cin >> user_first_name;
cout << "Please enter your last name: ";
cin >> user_last_name;
string user_full_name;
user_full_name = user_first_name + " " + user_last_name;
cout << "Your name is: " << user_full_name << "\n";
}
If you want to put two strings together, known as appending one string
onto another, you can use the +sign:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string user_first_name;
string user_last_name;
cout << "Please enter your first name: ";
cin >> user_first_name;
cout << "Please enter your last name: ";
cin >> user_last_name;
string user_full_name = user_first_name + " " + user_last_name;
cout << "Your name is: " << user_full_name << "\n";
}
you could write the distance between the sun and the earth as 9.295610^7
miles (approximately 93 million miles).
This allows the computer to store really big numbers by putting a very large number in the exponent.
But the non-exponent part cant store 300 digits, it can only store about 15 digits. So you can only get
15
digits of precision when you work with floating point numbers. If youre dealing with relatively small
values, then the difference between the number the computer stores, and the actual number, will be
very small. If youre working with huge values, well, youre going to have large absolute error, evenif
the relative error is small. For example, if I had only two digits of precision, I could write that the earth is
9.3 10^7 miles from the sun. Thats very close, in relative terms, to the right value (less than .1% off)
But in absolute terms, its 44 thousand miles! Thats nearly twice the circumference of the earth. But of
course, thats using only 2 digits of precision. With 15, you can get a lot closer for numbers as small as
a
million. In most cases, the inexactness of floating point numbers wont affect you unless youre doing
some
serious number crunching or scientific computing, but when it matters, it matters.
String comparisons
C++ string objects allow you to use all of the comparisons that you learned about earlier
in this chapter.
By comparing string objects, we can write our password checker!
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string password;
cout << "Enter your password: " << "\n";
getline( cin, password, '\n' );
if ( password == "xyzzy" )
{ cout << "Access allowed" << "\n";
} else
{ cout << "Bad password. Denied access!" << "\n"; // returning is a convenient way
to stop the program return 0;
} // continue onward!
}
//
if(!a)
{
cout<<"\nINVALID ENTRYFOR a.";
exit(0);
}
delta=(b*b)-(4*a*c);
if(delta<0)
cout<<"\nIMAGINARY ANDCOMPLEX ROOTS.";
else if (delta>0)
{
root1=(-b+sqrt(delta))/(2*a);
root2=(-b-sqrt(delta))/(2*a);
cout<<"\nROOTS ARE REAL AND
UNEQUAL."<<endl;
cout<<"\nROOTS ARE : "<< root1 << "\t"
<<root2;
}
else if(delta==0)
{
root1=(-b/(2*a));
root2=root1;
cout<<"\nROOTS ARE REAL AND EQUAL."<<endl;
cout<<"\nROOTS ARE : "<< root1 << "\t"
<<root2;
}