In: Computer Science
1. Write a do while loop that continually loops until the user enters a single digit between 0 and 9. There should only be one prompt for the user to enter a number inside the loop.
2. Write a simple 4 function calculator using a switch statement. The program should switch on the operator: +, -, *, /. The user should enter two numbers (any type is fine) and then enter an operator and the program should perform the calculation indicated.
3. Write a program that has the user enter a numeric scores (0-100) and then, using the conditional operator, display whether or not the student made a passing score. A passing score is 70 or above.
1)
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int n=100;
do
{
cout<<" enter n:";
cin>>n;
if(n>=0 && n<=9)
break;
else
cout<<"
looping...";
}while(1);
}
2)
#include<iostream>
#include<math.h>
using namespace std;
int add(int a ,int b)
{
return a+b;
}
int sub(int a ,int b)
{
return a-b;
}
int mul(int a,int b)
{
return a*b;
}
float div(int a ,int b)
{
return a/b;
}
int main()
{
int a=0,b=0;
char opr;
cout<<"enter first number";
cin>>a;
cout<<"enter second number";
cin>>b;
cout<<"enter operation";
cin>>opr;
float result =0 ;
switch(opr)
{
case '+': result = add(a,b);break;
case '-': result = add(a,b);break;
case '*': result =
add(a,b);break;
case '/':result =
add(a,b);break;
default:"wrong operation
entered";
}
cout<<" result = "<<result;
}
3)
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int score;
cout<<"enter score";
cin>>score;
if(score>=70 && score <=100)
cout<<"you passed";
else if(score >=0 && score <70)
cout<<"you failed";
else
cout<<"invalid value for score
entered";
}