[go: up one dir, main page]

0% found this document useful (0 votes)
84 views16 pages

"One Way Selection": #Include Using Namespace Int Int

The document contains 9 code snippets demonstrating different C++ programming concepts like selection statements (if, if-else), loops (while, do-while, for), switch statements, functions, arrays and more. It also contains 5 questions with answers implementing concepts like selection using switch case, converting temperatures between Fahrenheit and Celsius scales, compound interest calculation, factorial calculation.

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)
84 views16 pages

"One Way Selection": #Include Using Namespace Int Int

The document contains 9 code snippets demonstrating different C++ programming concepts like selection statements (if, if-else), loops (while, do-while, for), switch statements, functions, arrays and more. It also contains 5 questions with answers implementing concepts like selection using switch case, converting temperatures between Fahrenheit and Celsius scales, compound interest calculation, factorial calculation.

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/ 16

“ONE WAY SELECTION”

1: #include<iostream>
using namespace std;

int main()
{
int num;

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

cin>>num;

if (num>=100)
{
cout<<"the entered number is greater than
100"<<endl;
}

system ("pause");
return 0;
}
“TWO WAY SELECTION”
2: #include<iostream>
using namespace std;

int main()
{
int num;

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

cin>>num;

if (num>=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”
3: #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 will be F"<<endl;
}

system ("pause");
return 0;
}
“SWITCH BREAK OPERATIONS”
4: #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<<"You are failed"<<endl;
break;

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

system ("pause");
return 0;
}

WHILE LOOP
5: #include<iostream>
using namespace std;

int main()
{
int i;
i=0;

while(i<20)
{
cout<<"The value of i is :"<<i<<endl;
i=i+5;
}

system ("pause");
return 0;
}

WHILE LOOP
6:
#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”


7: #include<iostream>
using namespace std;

int main()
{
int num,sum;
sum=0;
char ch;
do
{
cout<<"Enter the number:"<<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”
8: #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”
9:
#include<iostream>

using namespace std;

int main()
{
int i;
for(i=0;i<10;i++)
{
cout<<i*i<<endl;
}
system ("pause");
return 0;
}
Lab report
#include<iostream>
#include<conio.h>
#include<cmath>
using namespace std;
int main()
{
int a, b;
char x;
cout << "enter the first number:" << endl;
cin >> a;
cout << "Enter the operation (+,-,*,/) " << endl;
cin >> x;
cout << "enter the second number:" << endl;
cin >> b;
switch (x)
{
case '+':
cout << "ADDITION : " << a + b << endl;
break;
case '-':
cout << "SUBTRACTION : " << a - b << endl;
break;
case '*':
cout << "MULTIPLACTION : " << a * b << endl;
break;
case '/':
{
if (b == 0)
cout << "MATH ERROR " << endl;
else

cout << "DIVISION: " << a / b <<


endl;
break;
}
default:
cout << "INVALID ENTRY........." << endl;
break;
}
_getch();

QUESTION NO 2:
ANSWER:
#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();
}

QUESTION NO 3:
ANSWER:
#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();
}

QUESTION NO 4:
ANSWER:
#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();
}

QUESTION NO 5:
ANSWER:
#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