You are on page 1of 7

Switch statement

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
float score;
cout<<"enter your test score out of 100:";
cin>>score;
float y=(score/10);
int z=round(y);
cout<<"y="<<y<<"z="<<z<<endl;
//cout<<round(0.9)<<"\t"<<round(0.3)<<"\t"<<round(0.7);
switch(z)
{
case 10: cout<<"your grade is outstanding 'o'"<<endl; break;
case 9: cout<<"your grade is an A+"<<endl;break;
case 8: cout<<"your grade is an A"<<endl;break;
case 7: cout<<"your grade is B+"<<endl;break;
case 6:cout<<"your grade is B"<<endl;break;
case 5:cout<<"your grade is C+"<<endl;break;
case 4:cout<<"your grade is C"<<endl;break;
case 3: cout<<"your grade is D+"<<endl;break;
case 2:cout<<"your grade is D"<<endl;break;
case 1:cout<<"your grade is F"<<endl;break;
case 0:cout<<"your grade is F"<<endl;break;
default: cout<<"your grade is erroneous"<<endl; break;
}
cout<<"Goodbye"<<endl;
}
Conditional Expression Operator
#include<iostream>
using namespace std;
int main()
{
int a,b,c,d,min;
cout<<"enter four integer for finding out the minimum of
the four";
cin>>a>>b>>c>>d;
min=(a<b?a:b);
min=(min<c?min:c);
min=(min<d?min:d);
cout<<"min="<<min<<endl;
}
Looping statements
#include<iostream>
using namespace std;
int main()
{
int n, i=0; float sum=0;
cout<<"this program will generate summation upto a given
number"<<endl;
cout<<"please provide a number upto which the summation will be
calculated:";
cin>>n;
while (i<=n)
{
sum+=i++;
cout<<"The sum of the first "<<i<<" integers is "<<sum<<endl;
}
}
Looping statements
#include<iostream>
using namespace std;
int main()
{
int n, i=0; float sum=0;
cout<<"please provide the a number upto which summation will be
calculated: ";
cin>>n;
while(true) //somewhat peculiar while statement
{
if(i>n)
break;
sum+=i++;
}
cout<<"summation of first "<<n<<" integer is "<<sum;
}
Looping statements
#include<iostream>
using namespace std;
int main ()
{
int n,i=0;
cout<<"enter a positive integer: ";
cin>>n;
float sum=0;
do
{
sum+=i++;
}
while (i<=n);

cout<<"the sum of the first "<<n<<" integers is "<<sum;


}
Looping statement
#include<iostream>
using namespace std;
int main()
{
long n; double sum=1;
cout<<"enter a positive integer: "; cin>>n;
int i=1;
do
{
if(n==0)
{
sum=1; exit(0);
cout<<"the factorial of "<<n<<" is"<<sum<<endl; //please play with i to show the inherent flaws of
program
}
else
{
sum*=i++;
}
while(i<=n);

}
cout<<"the factorial of "<<n<<" is "<<sum<<endl;
Looping statement
#include<iostream>
using namespace std;
int main ()
{
int n;
double sum=0;
cout<<"enter a positive integer: "; cin>>n;
for(int i=1,j=0; i<=n;i++)
{
sum+=i;
}
cout<<"sum of first "<<n<<" integers is"<<sum<<endl;
}

You might also like