Haider
Haider
Fundamentals
Class : BS-SE_(B)
Naz.
C++ Programs
Get Started :
Statement_1 : My First Program.
#include<iostream>
using namespace std;
int main()
{
cout << "Hello World!"
<< endl;
return 0;
}
Output:
int main()
{
int num;
cout << "Enter a number :";
cin >> num;
cout << "The number is :"<<num;
return 0;
}
Output:
Conditional Statements:
Use of if-statement
Statement_3 : WAP to find the number is less then 10 or not?
#include<iostream>
using namespace std;
int main()
{
int num;
cout <<"Enter a
number :";
cin>>num;
if(num<10)
cout<<"The
number is less then
10";
return 0;
}Output :
#include<iostream>
using namespace std;
int main()
{
int num;
cout <<"Enter a number :";
cin>>num;
if(num%2==0)
cout<<"The number is even";
else
cout<<"The number is odd"; 3
return 0;
}
Output:
Repetition Statements:
Use of for loop
Statement_5 : Print numbers using for loop
#include<iostream>
using namespace std;
int main()
{
int num, i;
cout <<"Enter a number :";
cin>>num;
for(i=0; i<=num; i++)
cout<<"result is :"<<i<<endl;
return 0;
}
Output:
int main()
{
int num, i;
cout <<"Enter a number :";
cin>>num;
i=1;
while(i<=num)
{
cout<<"result is :"<<i<<endl;
i++;
}
return 0;
}
Output:
Dry run:
Dry run:
int main()
{
int num, i, j;
cout <<"Enter a number :";
cin>>num;
for(i=1; i<=num; i++)
{
for(j=1; j<=num; j++)
cout<<"Hello..!!"<<endl;
}
return 0;
}
Output:
Dry run:
Break Statement
Statement_9 : WAP to use break statement
#include<iostream>
using namespace std;
int main()
{
int num, i; 7
cout <<"Enter a number :";
cin>>num;
for(i=1; i<=num; i++)
{
if(i==5)
break;
cout<<i<<endl;
}
return 0;
}
Output:
Dry Run:
Continues Statement
Statement_10 : WAP to use continue statement
#include<iostream>
using namespace std;
int main()
{
int num, i;
cout <<"Enter a number :";
cin>>num;
for(i=1; i<=num; i++)
{
if(i==5)
continue;
cout<<i<<endl;
}
return 0;
} 8
Output:
Switch Statement :
Statement_11 : WAP to calculate grade in switch statement
#include<iostream>
using namespace std;
int main()
{
int page;
cout<<"enter your percentage :";
cin>>page;
if(page==80 && page>=71)
switch(page/=10)
{
case 1:
cout<<"your grade is 'A'"<<endl;
break;
case 2:
cout<<"your grade is 'B'"<<endl;
break;
case 3:
cout<<"your grade is 'C'"<<endl;
break;
case 4:
cout<<"your grade is 'D'"<<endl;
break;
}
return 0;
}
Output:
C++ functions :
Statement_12 : WAP to convert kg to grams
#include<iostream>
using namespace std;
float gram(float kg);
int main()
{
float kg;
cout<<"enter amount in KG :";
cin>>kg;
cout<<"the amount in G is :"<< gram(kg);
return 0;
}
float gram(float kg)
{
float grams;
grams=kg*1000;
return grams;
}
Output:
Statement_13 :
WAP to find factorial of a
number by using function
#include<iostream>
using namespace std;
int fact(int a);
int main()
{
int n;
cout<<"Enter a number"<<endl;
cin>>n;
cout<<"The factorial of n is: "<< n <<" is :"<< fact(n);
return 0;
}
int fact(int a)
{
int factorial=1;
for(int i=1 ; i<=a ; i++)
{
factorial=factorial*i;
}
return factorial;
} 10
Output:
Dry run:
Statement_15 : WAP to find the number of zeros, odd digits & even
digits in a given integer by func()
#include<iostream>
using namespace std;
void analyze(int num);
int main()
{
int num;
cout<<"Enter an integer :";
cin>>num;
analyze(num);
return 0;
}
void analyze(int num)
{
int zeros=0, odd=0 ,even=0;
num=abs(num);
if(num==0)
zeros=1;
while(num>0)
{
int digit=num % 10;
if(digit==0)
{
zeros++;
}
else if(digit%2==0)
{
even++;
}
else
{
odd++;
}
num=num/10;
}
cout<<"The number of Zeros are :"<<zeros<<endl;
cout<<"The number of odd digits are :"<<odd<<endl;
cout<<"The number of even digits are :"<<even<<endl;
}
12
Output:
Output:
13
{
int i=1, j=1;
for( ; ;
)
{
if(i>5)
break;
else
j+=i;
cout<<"\
n"<<j;
i+=j;
i=i+j;
}
return 0;
}
}
Output:
14
Dry run:
15
Dry Run:
16
Statement_20 :
WAP to find the square of
7 integer by func()
#include <iostream>
using namespace std;
void sqrnum();
int main()
{
cout << "Square of 1st seven numbers are: " << endl;
sqrnum();
return 0;
}
void sqrnum()
{
for (int i = 1; i <= 7; i++)
{
int square = i * i;
cout << square<<", " ;
}
}
Output:
Dry run:
Arrays
Statement_21 : WAP that gets marks of student & price of items as
input and displays them as output
#include<iostream>
17
using namespace std;
int main()
{
int marks[5],i;
float price[5];
cout<<"enter marks"<<endl;
for(i=1;i<=5;i++)
cin>>marks[i];
cout<<"enter price"<<endl;
for(i=1;i<=5;i++)
cin>>price[i];
cout<<"Here is your array data"<<endl;
for(i=1;i<=5;i++)
{
cout<<"marks of "<<i<<"th student are"<<marks[i]<<endl;
cout<<"price of "<<i<<"th item are"<<price[i]<<endl;
}
return 0;
}
Output:
Statement_22 :
WAP to find largest &
smallest 3 numbers from
given array
#include<iostream>
using namespace std;
int main()
{
int num[15]={8,3,5,7,9,32,54,12,56,89,64,32,24,43,22};
int max1=num[0], max2=num[0], max3=num[0];
int min1=num[0], min2=num[0], min3=num[0];
18
for(int i=1; i<15; i++)
{
if(num[i]>max1)
{
max3=max2;
max2=max1;
max1=num[i];
}
else if(num[i]>max2)
{
max3=max2;
max2=num[i];
}
else if(num[i]>max3)
{
max3=num[i];
}
if(num[i]<min1)
{
min3=min2;
min2=min1;
min1=num[i];
}
else if(num[i]<min2)
{
min3=min2;
min2=num[i];
}
else if(num[i]<min3)
{
min3=num[i];
}
}
cout<<"Largest 3 elements are :"<<max1<<", "<<max2<<",
"<<max3<<endl;
cout<<"Smallest 3 elements are :"<<min1<<", "<<min2<<",
"<<min3<<endl;
return 0;
}
Output:
19
Dry Run:
Statement_23 : WAP to find largest element from given array
#include<iostream>
using namespace std;
int main()
{
int num[15]={68, 4, 3, 6, 7, 23, 25, 35, 4, 67, 22, 54, 11, 9,
34};
int max=num[0];
for(int i=0; i<15; i++)
{
if(num[i]>max)
{
max=num[i];
}
}
cout<<"The largest Element is :"<<max<<endl;
return 0;
}
Output:
Dry Run:
20
Statement_25 :
WAP to find the repeated
value from given array
#include<iostream>
using namespace std;
int main()
{
int num[15]={3,4,7,6,34,4,6,43,22,10,26,34,55,43,55};
for(int i=0; i<15; i++)
{
for(int j=i+1; j<15; j++) 21
{
if(num[i]==num[j])
{
cout<<num[i]<<" Is a repeated value"<<endl;
}
}
}
return 0;
}
Output:
Dry run:
Arrays in structures
Statement_26 : WAP to add numbers by using structure array
#include<iostream>
using namespace std;
struct sum
{
int num;
float decimal;
};
int main() 22
{
sum s[5];
cout<<"Enter your numbers :\n";
for(int i=0; i<5; i++) {
cout<<"Enter integer numbers :";
cin>>s[i].num;
cout<<"Enter number in decimal :";
cin>>s[i].decimal;
}
float totalSum = 0;
cout << "Sum is :\n";
for (int i = 0; i < 5; i++) {
totalSum += s[i].num + s[i].decimal;
}
cout << "Total Sum is :" << totalSum;
return 0;
}
Output:
int main()
{
Bookstore books[10];
cout << "Enter details for 10 books (Price and Year):\n";
for (int i = 0; i < 10; i++)
{
cout << "Book " << i + 1 << ":\n";
cout << "Enter Price: ";
cin >> books[i].price;
cout << "Enter Year: ";
cin >> books[i].year; 23
}
cout << "\nDetails of the 10 books:\n";
for (int i = 0; i < 10; i++)
{
cout << "Book " << i + 1 << " - Price: " << books[i].price <<
", Year: " << books[i].year << endl;
}
return 0;
}
Output:
Structures
Statement_28 : WAP to display marks, grade & cgpa of student by
using structures
#include<iostream>
using namespace std;
struct std1
{
int marks;
char grade;
float cgpa;
} s1;
int main()
{
std1 s2;
cout<<"enter input data"<<endl;
cin>> s1.marks;
cin>>s1.grade;
cin>>s1.cgpa; 24
cout<<"here is your data"<<endl;
cout<< s1.marks;
cout<<s1.grade;
cout<<s1.cgpa;
//for another variable
cout<<"enter input data"<<endl;
cin>> s2.marks;
cin>> s2.grade;
cin>> s2.cgpa;
cout<<"here is your data"<<endl;
cout<< s2.marks;
cout<< s2.grade;
cout<< s2.cgpa;
return 0;
}
Output:
2 dimensional Arrays
Statement_29 : WAP to display data matrix
#include<iostream>
using namespace std;
int main()
{
int i, j;
int arr1[2][2];
cout << "enter data matrix :"<<endl;
for(i=0; i<2; i++)
for(j=0; j<2; j++)
cin>>arr1[i][j];
for(i=0; i<2; i++)
{
cout<<endl;
for(j=0; j<2; j++)
cout<<arr1[i][j];
}
cout<<endl;
return 0;
}
25
Output:
26
Dry run:
27
Output:
String operator
Statement_32 : WAP by using string operator
#include<iostream>
using namespace std;
int main()
{
char name[]="Klinsman";
int i=0;
while(name[i]!='\0')
{
cout<<" "<<name[i];
i++;
}
return 0;
}
Output:
Pointer operator
Statement_33 : WAP by using Pointer operator
#include<iostream>
using namespace std;
int main()
{
int i=3;
int *p;
p=&i; 28
cout<<"\nAdress of i="<<&i<<endl;
cout<<"contents of p="<<p<<endl;
cout<<"Adress of p is="<<&p;
cout<<"Value of i="<<i<<endl;
cout<<"Value of i="<<*(&i);
return 0;
}
Output:
Additional Tasks
Statement_34 : WAP to find whether the number is prime or
composite?
#include<iostream>
using namespace std;
int main()
{
int n,j,count=0;
cout<<"enter a number"<<endl;
cin>>n;
j==1;
while(j<=n)
{
if(n%j==0)
count++;
j++;
}
if(count==0)
cout<<n<<"PRIME"<<endl;
else
cout<<n<<"COMPOSITE"<<endl;
return 0;
}
Output:
29
Output:
int main() {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
cout << j;
}
cout << endl;
}
return 0;
}
Output:
31
Dry Run
Dry Run:
33
Output:
Dry Run:
Dry Run:
35
Statement_45 : WAP
#include<iostream>
using namespace std;
struct
Student {
string name;
int marks[5];
float cgpa;
char grade;
};
char calculateGrade(float cgpa) {
if (cgpa >= 3.5) return 'A';
else if (cgpa >= 2.5) return 'B';
else return 'C';
}
int main() {
Student student;
cout << "Enter student's name: ";
cin >> student.name; int total = 0;
for (int i = 0; i < 5; i++) {
cout << "Enter marks for subject " << i + 1 << ": ";
cin >> student.marks[i];
total += student.marks[i];
}
student.cgpa = total / 50.0;
student.grade =
calculateGrade(student.cgpa);
cout << "Name: " << student.name << endl;
cout << "CGPA: " << student.cgpa << endl;
cout << "Grade: " << student.grade << endl;
return 0;
}
Output:
Statement_46 : WAP that’s get employee details and print the
same as output.
#include
using namespace std;
struct Employee {
int age;
char gender;
int salary;
};
int main() {
Employee emp;
36
cout << "Enter age: ";
cin >> emp.age;
cout << "Enter gender (M/F): ";
cin >> emp.gender;
cout << "Enter salary: ";
cin >> emp.salary;
cout << "Employee Details:" << endl;
cout << "Age: " << emp.age << endl;
cout << "Gender: " << emp.gender << endl;
cout << "Salary: " << emp.salary << endl;
return 0;
}
Output:
Statement_47 : WAP that ask for a choice for circle & rectangle and
print Area & perimeter of selection.
#include
#include
using namespace std;
float circleArea(int radius) {
return M_PI * radius * radius;
}
float circlePerimeter(int radius) {
return 2 * M_PI * radius;
}
float rectangleArea(float length, float width) {
return length * width;
}
float rectanglePerimeter(float length, float width) {
return 2 * (length + width);
}
int main() {
int choice;
cout << "Choose Shape: 1) Circle 2) Rectangle: ";
cin >> choice;
if (choice == 1) {
int radius;
cout << "Enter radius: ";
cin >> radius;
cout << "Area: " <<
Statement_49 :
WAP that indicate
temperature.
#include <iostream>
using namespace std;
int main()
{
int temperature; 39
cout<<"Enter temperature in dgree celsius : ";
cin>>temperature;
if(temperature<30){
cout<<"freezing"<<endl;
}else {
cout<<"temperature
is exactly 30 dgree
celsius."<<endl;
}
return 0;
}
Output:
Statement_50 : WAP that tell you about your eligibility of bonus.
#include <iostream>
using namespace std;
int main()
{
int current_year,year_of_joining,year_served;
const int bonus =10000;
cout<<"Enter current year : ";
cin>>currentyear;
cout<<"Enter year of joining : ";
cin>>yearofjoining;
year_served=current_year-year_of_joining;
if(year_served>3){
cout<<"Employee is elligible for bonus : "<<bonus<<endl;
}else {
// do nothing
}
Output:
39