0% found this document useful (0 votes)
94 views53 pages

My 50 Programs On C++

The document contains 40 C++ programs covering various programming fundamentals concepts like arithmetic operators, data types, functions, conditional statements, loops etc. Each program is presented with its source code and brief description. The programs are grouped into different sections like programs on arithmetic operators, escape sequences, pattern printing, data type sizes etc.

Uploaded by

Fahad Mughal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views53 pages

My 50 Programs On C++

The document contains 40 C++ programs covering various programming fundamentals concepts like arithmetic operators, data types, functions, conditional statements, loops etc. Each program is presented with its source code and brief description. The programs are grouped into different sections like programs on arithmetic operators, escape sequences, pattern printing, data type sizes etc.

Uploaded by

Fahad Mughal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

Programming

Fundamentals

Name :Fahad Akram


Roll no :21021519-037
Department
:Computer science
Assignment : 50 C++
programs
Programs on Arithmetic operators
1. write a program that
adds together two values
#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 3;
cout << x + y;
return 0;
}

2. write a program that


Subtracts one value from
another
#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 3;
cout << x - y;
return 0;
}

3. write a program that


multiplies two values
#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 3;
cout << x * y;
return 0;
}
4. write a program that
divides one value by
another
#include <iostream>
using namespace std;

int main() {
int x = 12;
int y = 3;
cout << x / y;
return 0;
}

5. write a program that


returns the division
remainder
#include <iostream>
using namespace std;

int main() {
int x = 5;
int y = 2;
cout << x % y;
return 0;
}

6. write a program that


Increases the value of a
variable by 1
#include <iostream>
using namespace std;

int main() {
int x = 5;
++x;
cout << x;
return 0;
}

7. write a program that


decreases the value of a
variable by 1
#include <iostream>
using namespace std;

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>

using namespace std;

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>

using namespace std;


int main()
{
cout<<" *\n *\t*\n *\t*\t*\n *\t*\n
*";
}

27. write a program that


displays the 5×2 matrix using
a single output statement
(without using a loop)
#include <iostream>
using namespace std;
int main()
{
cout<<" 1\t 3\t 5\t 7\t 9\n 11\t 13\t
15\t 12\t 19 ";
}

28. write a program that


displays square, a cube of a
number in table form
#include <iostream>

using namespace std;


int main()
{
cout<<" Number\t Square\t Cube "<<endl;
cout<<" 1\t"<<1*1<<"\t"<<1*1*1<<endl;
cout<<" 2\t"<<2*2<<"\t"<<2*2*2<<endl;
cout<<" 3\t"<<3*3<<"\t"<<3*3*3<<endl;
cout<<" 4\t"<<4*4<<"\t"<<4*4*4<<endl;

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;
}

33. write a program to


convert string to lower case
letters using char data type
#include <iostream>
#include <string.h>
using namespace std;

int main()
{
char string[10];

cout<<"Input a string to convert to lower


case"<<endl;
cin.getline(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;

cout << "\nPlease Enter the String to


Convert into lowercase = ";
getline(cin, upTxt);

while(i < upTxt.length())


{
if(isupper(upTxt[i]))
{
upTxt[i] = tolower(upTxt[i]);
}
i--;
}

cout<< "\nThe Given String in lowercase =


" << upTxt;
return 0;
}

35. C++ program to find size


of different data types
#include <iostream>
using namespace std;

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";
}

37.Write any program using


bool data type.
#include<iostream>
using namespace std;
int main()
{
bool iscodingfun=true;
cout<<iscodingfun;
}

38. Write a program which


take inputs force & Distance
and give work done as ouput.
#include<iostream>
using namespace std;
int main()
{
float force,distance,work;
cout<<"enter force in Newtons = ";
cin>>force;
cout<<"enter distance in meters = ";
cin>>distance;
work=force*distance;
cout<<"work done = "<<work<<" joules";
return 0;

}
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;

40. program to calculate area


of a circle
#include<iostream>
#define pi 3.14
using namespace std;
int main()
{
float area,radius;
cout<<"enter radius in meters = ";
cin>>radius;
area=pi * radius * radius;
cout<<"area = "<<area;
}

41. write a program to


calculate area of square.
#include<iostream>
using namespace std;
int main()
{
float area,length_of_side;
cout<<"enter length of one side of square
in meters = ";
cin>>length_of_side;
area=length_of_side*length_of_side;
cout<<"area = "<<area<<" meter square";
}

42. program to find area of


rectangle
#include<iostream>
using namespace std;
int main()
{
float area,length,width;
cout<<"enter length of rectangle = ";
cin>>length;
cout<<"enter width of rectangle = ";
cin>>width;
area=length*width;
cout<<"area = "<<area<<" meter square";
}

43. program to find area of


parallelogram.
#include<iostream>
using namespace std;
int main()
{
float area,base,height;
cout<<"enter length of base in meters =
";
cin>>base;
cout<<"enter height in meters = ";
cin>>height;
area=base*height;
cout<<"area = "<<area<<" meter square";
}
44. program to find height by
given area and base of
parallelogram
#include<iostream>
using namespace std;
int main()
{
float area,base,height;
cout<<"enter area of parallelogram = ";
cin>>area;
cout<<"enter base = ";
cin>>base;
height=area/base;
cout<<"height = "<<height<<" meters";
}
45. program to find area of
cube
#include<iostream>
using namespace std;
int main()
{
float length_of_side,area;
cout<<"length of any side of cube = ";
cin>>length_of_side;
area=6*length_of_side*length_of_side;
cout<<"area of cube = "<<area<<" meter
square";
}

46. program to find velocity


of body by given data of
distance and time
#include<iostream>
using namespace std;
int main()
{
float velocity,distance,time;
cout<<"distance travelled by body = ";
cin>>distance;
cout<<"enter time taken = ";
cin>>time;
velocity=distance/time;
cout<<"velocity = "<<velocity<<" meter
per second";
}

47.Write a program that


converts
rupees into dollars
#include <iostream>
using namespace std;
int main()
{
float $,rs;
cout<<"Enter Rupees:";
cin>>rs;
$=rs/179;
cout<<$<<" dollars";
return 0;
}

48.Write a program to coverts


hours
input to seconds
#include<iostream>
using namespace std;
int main()
{
int h,s;
cout<<"Enter time in hours:";
cin>>h;
s=h*3600;
cout<<"Time in seconds:"<<s;
return 0;
}
49. Write a C++ program that
input
your weight, Height and
Gender and
print result on computer
screen.

#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;
}

51. Creat a unchangeable


variable with const keyword
#include <iostream>
using namespace std;

int main() {
const int myNum = 15;
myNum = 10; //compiler will not rewrite var.
cout << myNum;
return 0;
}

The end

You might also like