0% found this document useful (0 votes)
47 views13 pages

C++ Codes

The document contains C++ code examples demonstrating different types of selection and repetition control structures: 1) One-way selection with an if statement to check if a number is greater than 100. 2) Two-way selection with an if-else statement to check if a number is greater or less than 100. 3) Nested if-else statements to assign a letter grade based on test marks. 4) A switch statement with break to output GPAs based on letter grades. 5) Examples of while, do-while, and for loops to iterate through loops and calculate sums and outputs. 6) C++ code snippets from a lab assignment containing problems using selection and repetition

Uploaded by

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

C++ Codes

The document contains C++ code examples demonstrating different types of selection and repetition control structures: 1) One-way selection with an if statement to check if a number is greater than 100. 2) Two-way selection with an if-else statement to check if a number is greater or less than 100. 3) Nested if-else statements to assign a letter grade based on test marks. 4) A switch statement with break to output GPAs based on letter grades. 5) Examples of while, do-while, and for loops to iterate through loops and calculate sums and outputs. 6) C++ code snippets from a lab assignment containing problems using selection and repetition

Uploaded by

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

“ONE WAY SELECTION”

#include<iostream>

using namespace std;

int main()

int x;

cout<<"Enter the number: "<<endl;

cin>>x;

if(x>=100)

cout<<"The entered number is greater than 100"<<endl;

system("pause");

return 0;

“TWO WAY SELECTION”

#include<iostream>
using namespace std;
int main()
{
int x;
cout<<"Enter the number: "<<endl;
cin>>x;
if(x>=100)
{
cout<<"The entered number is greater than 100"<<endl;
}
else
cout<<"The entered number is smaller than 100"<<endl;
system("pause");
return 0;
}

“NESTED IF-ELSE”

#include<iostream>
using namespace std;
int main()
{
int marks;
cout<<"Enter the marks obtained: "<<endl;
cin>>marks;
if(marks>=90)
{
cout<<"Your grade is A"<<endl;
}
else if(marks>=80)
{
cout<<"Your grade is B"<<endl;
}
else if(marks>=70)
{
cout<<"Your grade is C"<<endl;
}
else if(marks>=60)
{
cout<<":Your grade is D"<<endl;
}
else
cout<<"Your grade is F"<<endl;
system("pause");
return 0;
}

“SWITCH BREAK OPERATIONS”

#include<iostream>
using namespace std;
int main()
{
char grade;
cout<<"Enter the grade: "<<endl;
cin>>grade;
switch(grade)
{
case 'A':
cout<<"Your GPA is 4.00"<<endl;
break;

case 'B':

cout<<"Your GPA is 3.00"<<endl;


break;

case 'C':

cout<<"Your GPA is 2.00"<<endl;


break;

case 'D':
cout<<"Your GPA is 1.00"<<endl;
break;

case 'F':

cout<<"Sorry! You are failed"<<endl;


break;

default :
cout<<"Invalid Entry"<<endl;
}

system("pause");
return 0;
}

“WHILE LOOP”

#include<iostream>
using namespace std;
int main()
{
int i=0;
while(i<20)
{
cout<<"The value of i is: "<<i<<endl;
i=i+5;
}
system("pause");
return 0;
}
“WHILE LOOP”

#include<iostream>
using namespace std;
int main()
{
int num,sum,avg,count;
sum=0; count=0;
while(count<5)
{
cout<<"Enter the number: "<<endl;
cin>>num;
sum=sum+num;
count++;
}
cout<<"The sum of entered numbers is: "<<sum<<endl;
avg=sum/5;
cout<<"The average is: "<<avg<<endl;

system("pause");
return 0;
}
“DO-WHILE LOOP”

#include<iostream>
using namespace std;
int main()
{
int num,sum=0;
char ch;
do
{
cout<<"Enter the numbers: "<<endl;
cin>>num;
sum=sum+num;
cout<<"Do you want to enter another number (y/n): "<<endl;
cin>>ch;
}
while(ch!='n');
cout<<"The sum of entered number is: "<<sum<<endl;

system("pause");
return 0;
}
“FOR LOOP”
#include<iostream>
using namespace std;
int main()
{
int i;
for(i=0;i<10;i++)
{
cout<<"Hello"<<endl;
cout<<"******"<<endl;
}
system("pause");
return 0;
}

“FOR LOOP”
#include<iostream>
using namespace std;
int main()
{
int i;
for(i=0;i<10;i++)
{
cout<<i*i<<endl;
}
system("pause");
return 0;
}

LAB TASK # 1
Q1
# include <iostream>
#include<conio.h>
using namespace std;

void main()
{
char op;
float num1, num2,b;

cout << "Enter operator either + or - or * or /: ";


cin >> op;

cout << "Enter two operands: ";


cin >> num1 >> num2;

switch(op)
{

case '+':
cout << num1+num2;
break;

case '-':
cout << num1-num2;
break;

case '*':
cout << num1*num2;
break;

case '/':
if (num2==0)
{
cout<<" it cannot happened "<<endl;
}
cout << num1/num2;
break;

getch();
}
Q2
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
int roll;
cout<<"enter the roll numbver : "<<endl;
cin>>roll;
if (roll<=100)
{
switch(roll)
{
case 13:
cout<<"you got 1st position "<<endl;
break;

case 23:
cout<<"you got 2nd position "<<endl;
break;

case 55:
cout<<"you got 3rd position "<<endl;
break;
case 87:
cout<<"you got 4th position "<<endl;
break;
case 93:
cout<<"you got 5th position "<<endl;
break;
default:
cout<<"sorry no position obtained "<<endl;
break;
}
}
else
cout<<"the program is invalid "<<endl;
getch();
}
Q3
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
int scale;
double temperature;
cout<<"\n Type 1 to convert fahrenheit to celsius"<<"\n Type 2 to convert celsius
to fahrenheit"<<endl;
cin>>scale;
if (scale==1)
{
cout<<"enter temperature in fahrenheit : "<<endl;
cin>>temperature ;
cout<<"in celsius :" <<5.0/9.0*(temperature - 32.0 );
}
else

cout<<"enter temperature in celsius : "<<endl;


cin>>temperature ;
cout<<"in fahrenheit :" <<5.0/9.0*(temperature + 32.0 );
getch();
}
Q4
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{

int amount,year,a;
float interest_rate,amount1;
cout<<"Enter the initial amount:";
cin>>amount;
cout<<"Enter the number of year:";
cin>>year;
cout<<"Enter the interest rate:";
cin>>interest_rate;
for(a=1;a<=year;a=a+1)
{
amount1=amount+(amount*(interest_rate/100));
cout<<"At the end of "<< a<< " year:"<<amount1<<" dollar "<<"\n";
amount=amount1;
}
cout<<"At the end of "<<year<<" year"<<",you will have " <<amount1<<"
dollars";
getch();
}

Q5
#include <iostream>
#include<conio.h>
using namespace std;

void main()
{
int n;
int factorial = 1;

cout << "Enter a positive integer: ";


cin >> n;

for(int i = 1; i <=n; ++i)


{
factorial *= i;
}

cout << "Factorial of " << n << " = " << factorial;

getch();
}

You might also like