My 50 Programs On C++
My 50 Programs On C++
Fundamentals
int main() {
int x = 12;
int y = 3;
cout << x / y;
return 0;
}
int main() {
int x = 5;
int y = 2;
cout << x % y;
return 0;
}
int main() {
int x = 5;
++x;
cout << x;
return 0;
}
int main() {
int x = 5;
--x;
cout << x;
return 0;
}
C++ programs on precendence of
Arithmetic operators
(8)
#include<iostream>
using namespace std;
int main()
{
int num1=10+20-30 ; /*addtion and subtraction
has same precedence level so expression is evaluated from
left to right*/
cout<<num1;
}
(9)
#include<iostream>
using namespace std;
int main()
{
int num1=10-20+30 ; //expression is
evaluated from left to right
cout<<num1;
}
(10)
#include<iostream>
using namespace std;
int main()
{
int num1=10-20+30*40/50 ; /*All
multiplications and divisions are performed first from left to
right, all additions and subtractios are then performed from
left to right*/
cout<<num1;
}
(11)
#include<iostream>
using namespace std;
int main()
{
int num1=(10-20+30)*40/50 ; /*if we want
compilier to perform addtion subtraction first we use
parenthesis*/
cout<<num1;
}
(12)
#include<iostream>
using namespace std;
int main()
{
int num1=10-(20+30)*40/50 ; /*the
expression within parenthesis is first computed from left to
right then in this case multiplication and division from left to
right*/
cout<<num1;
}
(13)
#include<iostream>
using namespace std;
int main()
{
int num1=(10-(20+30))*40/50 ; /*the
expression in inner most parenthesis is evaluated first*/
cout<<num1;
}
(14)
#include<iostream>
using namespace std;
int main()
{
float a=5.0*30.0+10.0/30.0-20.0;
//multiplication and division is performed first from left to
right
cout<<a;
}
(15)
#include<iostream>
using namespace std;
int main()
{
float a=10.0/20.0+30.0*40.0/12.0;
//multiplication and division is performed first from left to
right
cout<<a;
}
Programs using Escape sequence
(16)
\a :
#include<iostream>
using namespace std;
int main()
{
cout<<"Code\aExapmle"; //This escape
sequence is used to play beep during program’s execution
}
(17)
\b :
#include<iostream>
using namespace std;
int main()
{
cout<<"Code\bExample"; //This escape
sequence is used to delete previous character.
(18)
\t :
#include<iostream>
using namespace std;
int main()
{
cout<<"Code\tExample"; //it causes the
cursur to move one tab forward.
(19)
\n :
#include<iostream>
using namespace std;
int main()
{
cout<<"Code\nExample"; //This escape
sequence is used to insert newline in text
(20)
\r :
#include<iostream>
using namespace std;
int main()
{
cout<<"Code\rExample"; //This escape
sequence is used to insert carriage return in text
(21)
\\ :
#include<iostream>
using namespace std;
int main()
{
cout<<"Code\\Example"; //To insert
BackSlash is string
(22)
\” :
#include<iostream>
using namespace std;
int main()
{
cout<<"\"CodeExample\""; //This escape
sequence is used to print double quote marks ” in text.
(23)
\’ :
#include<iostream>
using namespace std;
int main()
{
cout<<"\'CodeExample\'"; //This escape
sequence is used to print single quote marks ” in text.
Some Examples
24. write a program that
displays following output:
‘D’ ‘I’ ‘g’
‘e’ ‘s’ ‘t’
“Programming”
#include <iostream>
using namespace std;
int main()
{ cout<<"\'D\'\t\'i\'\t\'g\'\t\'e\'\t\'s\'\t\'t\'\n";
cout<<"\"Programming\"\n";
}
25. write a program that
displays the triangle pattern
using a single output
statement(without using a
loop)
#include <iostream>
int main()
{
cout<<"*\n* *\n* * *\n* * * *\n";
}
26. Write a program that
displays the arrowhead
pattern using a single output
statement (without using a
loop) :
#include <iostream>
29.
#include<iostream>
using namespace std;
int main ()
{
cout<<"here is the demo\n ";
cout<<"bell sound\a";
return 0;
}
30.
#include<iostream>
using namespace std;
int main ()
{
cout<<"What is your name\?";
return 0;
}
31. Write a program in C++ to
print the sum of two numbers
using variables
#include<iostream>
using namespace std;
int main()
{
int first_digit,second_digit,sum;
cout<<"\n\tEnter first digit =";
cin>>first_digit;
cout<<"\tEnter second digit =";
cin>>second_digit;
sum=first_digit+second_digit;
cout<<"\tsum of "<<first_digit <<" and
"<<second_digit<<" is "<<sum;
}
32. Write a c++ program
which finds the length of
input string
#include <iostream>
using namespace std;
int main()
{
string str;
cout<<"Enter your string = ";
getline(cin,str);
cout<< "string length ="<<str.size();
return 0;
}
int main()
{
char string[10];
cout<<"Character in lowercase:"<<
strlwr(string);
// strlwr is use to convert upper case
character to lower case
return 0;
}
34. Write a program to
convert string to lower case
using string data type
#include<iostream>
#include<string>
using namespace std;
int main()
{
string upTxt;
int i = 0;
int main()
{
cout<<"size of int = "<<sizeof(int)<<"
bytes"<<endl;
cout<<"size of shotr int = "<<sizeof(short
int)<<" bytes"<<endl;
cout<<"size of long int = "<<sizeof(long
int)<<" bytes"<<endl;
cout<<"size of float = "<<sizeof(float)<<"
bytes"<<endl;
cout<<"size of double =
"<<sizeof(double)<<" bytes"<<endl;
cout<<"size of long double =
"<<sizeof(long double)<<"bytes"<<endl;
cout<<"size of char = "<<sizeof(char)<<"
bytes"<<endl;
cout<<"size of bool = "<<sizeof(bool)<<"
bytes"<<endl;
return 0;
}
36. Write a program to check
if Given number is even or
Odd
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
if (n%2==0)
cout<<n<<" is even"<<endl;
else
cout<<n<<" is odd";
}
}
39. write a program to
calculate area of tiangle.
#include<iostream>
using namespace std;
int main()
{
float a,b,c;
cout<<"enter length os side a = ";
cin>>a;
cout<<"enter length of side b = ";
cin>>b;
cout<<"enter length of side c = ";
cin>>c;
float s;
s=(a+b+c)/2;
float area;
area=s*(s-a)*(s-b)*(s-c);
cout<<"area of tiangle is = "<<area;
#include <iostream>
#include <string>
using namespace std;
int main()
{
float w,h;
string g;
cout<<"Enter your weight in pounds:";
cin>>w;
cout<<"Enter your hight in inches:";
cin>>h;
cout<<"Enter your gender:";
cin>>g;
cout<<endl;
cout<<"Wieght:"<<w;
cout<<"\nHighet:"<<h;
cout<<"\nGender:"<<g;
return 0;
}
50. Write a C++ program that
can
calculate the average of two
numbers and print the result
on
screen.
#include <iostream>
using namespace std;
int main()
{
float n_1,n_2,avg;
cout<<"Enter 1st Number";
cin>>n_1;
cout<<"Enter 2nd Number";
cin>>n_2;
avg=(n_1+n_2)/2.0;
cout<<"Average of Numbers is"<<avg;
return 0;
}
int main() {
const int myNum = 15;
myNum = 10; //compiler will not rewrite var.
cout << myNum;
return 0;
}
The end