Question

How to convert C++ code to C #include<iostream> #include <stdlib.h> using namespace std; void multiplication() {...

How to convert C++ code to C

#include<iostream>
#include <stdlib.h>
using namespace std;
void multiplication()
{
int num1;
int c, num2, ans;
cout<<"Enter difficulty level(1/2)\n";
cin>>c;
if(c==1)
{
num1= rand() % 10;
num2= rand() % 10;
cout<<"what is"<<num1 <<"*"<<num2<<"?\n" ;
cin>>ans
while(ans!=(num1*num2))
{
if(ans!=(num1*num2))
{
cout<< "No. Please try again.\n";
cout<<"what is"<<num1 <<"*"<<num2<<"?\n" ;
cin>>ans;
}
}
}
else if(c==2)
{
num1= rand() % 10+90;
num2= rand() % 10+90;
cout<<"what is"<<num1 <<"*"<<num2<<"?\n" ;
cin>>ans;
while(ans!=(num1*num2))
{
if(ans!=(num1*num2))
{
cout<< "No. Please try again.\n";
cout<<"what is"<<num1 <<"*"<<num2<<"?\n" ;
cin>>ans;
}
}
}
}
void addition()
{
int num1;
int c, num2, ans;
cout<<"Enter difficulty level(1/2)\n";
cin>>c;
if(c==1)
{
num1= rand() % 10;
num2= rand() % 10;
cout<<"what is"<<num1 <<"+"<<num2<<"?\n" ;
cin>>ans;
while(ans!=(num1+num2))
{
if(ans!=(num1+num2))
{
cout<< "No. Please try again.\n";
cout<<"what is"<<num1 <<"+"<<num2<<"?\n" ;
cin>>ans;
}
}
}
else if(c==2)
{
num1= rand() % 10+90;
num2= rand() % 10+90;
cout<<"what is"<<num1 <<"+"<<num2<<"?\n" ;
cin>>ans;
while(ans!=(num1+num2))
{
if(ans!=(num1+num2))
{
cout<< "No. Please try again.\n";
cout<<"what is"<<num1 <<"+"<<num2<<"?\n" ;
cin>>ans;
}
}
}
}
void subtraction()
{
int num1;
int c, num2, ans;
cout<<"Enter difficulty level(1/2)\n";
cin>>c;
if(c==1)
{
num1= rand() % 10;
num2= rand() % 10;
cout<<"what is"<<num1 <<"-"<<num2<<"?\n" ;
cin>>ans;
while(ans!=(num1-num2))
{
if(ans!=(num1-num2))
{
cout<< "No. Please try again.\n";
cout<<"what is"<<num1 <<"-"<<num2<<"?\n" ;
cin>>ans;
}
}
}
else if(c==2)
{
num1= rand() % 10+90;
num2= rand() % 10+90;
cout<<"what is"<<num1 <<"-"<<num2<<"?\n" ;
cin>>ans;
while(ans!=(num1-num2))
{
if(ans!=(num1-num2))
{
cout<< "No. Please try again.\n";
cout<<"what is"<<num1 <<"-"<<num2<<"?\n" ;
cin>>ans;
}
}
}
}
void division()
{
float num1,num2;
int c;
float ans;
cout<<"Enter difficulty level(1/2)\n";
cin>>c;
if(c==1)
{
num1= rand() % 10;
num2= rand() % 10;
cout<<"what is"<<num1 <<"/"<<num2<<"?\n" ;
cin>>ans;
while(ans!=(num1/num2))
{
if(ans!=(num1/num2))
{
cout<< "No. Please try again.\n";
cout<<"what is"<<num1 <<"/"<<num2<<"?\n" ;
cin>>ans;
}
}
}
else if(c==2)
{
num1= rand() % 10+90;
num2= rand() % 10+90;
cout<<"what is"<<num1 <<"/"<<num2<<"?\n" ;
cin>>ans;
while(ans!=(num1/num2))
{
if(ans!=(num1/num2))
{
cout<< "No. Please try again.\n";
cout<<"what is"<<num1 <<"/"<<num2<<"?\n" ;
cin>>ans;
}
}
}
}
int question()
{   
int c, o,i;
cout<<" 1.Addition\n 2.Subtraction \n 3.Multiplication \n
4.Division\n 5.Random \n Enter your choice\n";
cin>>o;
switch(o)
{
case 1:
addition();
break;
case 2:
subtraction();
break;
case 3:
multiplication();
break;
case 4:
division();
break;
case 5:
i=rand() %4;
if(i==0)
{
addition();
break;
}
else if(i==1)
{
subtraction();
break;
}
else if(i==2)
{
multiplication();
break;
}
else if(i==3)
{
division();
break;
}
break;
}
return 1;
}
//Main function
int main()
{
int n;
char ch = 'y';
while(ch=='y')
{
n= question();
if(n==1)
cout<<"Very good!\n";
cout<<"Do you wish to continue(y/n)?\n";
cin>>ch;
}
return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The converted code in C language:

#include <stdio.h>
#include<stdlib.h>
#include<time.h>

void multiplication(){
int num1;
int c, num2, ans;
printf("Enter difficulty level(1/2)\n");
scanf("%d",&c);
if(c==1)
{
num1= rand() % 10;
num2= rand() % 10;
printf("what is %d*%d?\n",num1,num2);
scanf("%d",&ans);
while(ans!=(num1*num2))
{
if(ans!=(num1*num2))
{
printf("No. Please try again.\n");
printf("what is %d*%d?\n",num1,num2);
scanf("%d",&ans);
}
}
}
else if(c==2)
{
num1= rand() % 10 + 90;
num2= rand() % 10 + 90;
printf("what is %d*%d?\n",num1,num2);
scanf("%d",&ans);
while(ans!=(num1*num2))
{
if(ans!=(num1*num2))
{
printf("No. Please try again.\n");
printf("what is %d*%d?\n",num1,num2);
scanf("%d",&ans);
}
}
}
}
void addition()
{
int num1;
int c, num2, ans;
printf("Enter difficulty level(1/2)\n");
scanf("%d",&c);
if(c==1)
{
num1= rand() % 10;
num2= rand() % 10;
printf("what is %d+%d?\n",num1,num2);
scanf("%d",&ans);
while(ans!=(num1+num2))
{
if(ans!=(num1+num2))
{
printf("No. Please try again.\n");
printf("what is %d+%d?\n",num1,num2);
scanf("%d",&ans);
}
}
}
else if(c==2)
{
num1= rand() % 10 + 90;
num2= rand() % 10 + 90;
printf("what is %d+%d?\n",num1,num2);
scanf("%d",&ans);
while(ans!=(num1+num2))
{
if(ans!=(num1+num2))
{
printf("No. Please try again.\n");
printf("what is %d+%d?\n",num1,num2);
scanf("%d",&ans);
}
}
}
}
void subtraction()
{
int num1;
int c, num2, ans;
printf("Enter difficulty level(1/2)\n");
scanf("%d",&c);
if(c==1)
{
num1= rand() % 10;
num2= rand() % 10;
printf("what is %d-%d?\n",num1,num2);
scanf("%d",&ans);
while(ans!=(num1-num2))
{
if(ans!=(num1-num2))
{
printf("No. Please try again.\n");
printf("what is %d-%d?\n",num1,num2);
scanf("%d",&ans);
}
}
}
else if(c==2)
{
num1= rand() % 10 + 90;
num2= rand() % 10 + 90;
printf("what is %d-%d?\n",num1,num2);
scanf("%d",&ans);
while(ans!=(num1-num2))
{
if(ans!=(num1-num2))
{
printf("No. Please try again.\n");
printf("what is %d-%d?\n",num1,num2);
scanf("%d",&ans);
}
}
}
}
void division()
{
float num1,num2;
int c;
float ans;
printf("Enter difficulty level(1/2)\n");
scanf("%d",&c);
if(c==1)
{
num1= rand() % 10;
num2= rand() % 10;
printf("what is %d/%d?\n",num1,num2);
scanf("%d",&ans);
while(ans!=(num1/num2))
{
if(ans!=(num1/num2))
{
printf("No. Please try again.\n");
printf("what is %d/%d?\n",num1,num2);
scanf("%d",&ans);
}
}
}
else if(c==2)
{
num1= rand() % 10 + 90;
num2= rand() % 10 + 90;
printf("what is %d/%d?\n",num1,num2);
scanf("%d",&ans);
while(ans!=(num1/num2))
{
if(ans!=(num1/num2))
{
printf("No. Please try again.\n");
printf("what is %d/%d?\n",num1,num2);
scanf("%d",&ans);
}
}
}
}
int question()
{   
int c, o,i;
printf(" 1.Addition\n 2.Subtraction \n 3.Multiplication \n 4.Division\n 5.Random \n Enter your choice\n");
scanf("%d",&o);
switch(o)
{
case 1:
addition();
break;
case 2:
subtraction();
break;
case 3:
multiplication();
break;
case 4:
division();
break;
case 5:
i=rand() %4;
if(i==0)
{
addition();
break;
}
else if(i==1)
{
subtraction();
break;
}
else if(i==2)
{
multiplication();
break;
}
else if(i==3)
{
division();
break;
}
break;
}
return 1;
}
//Main function
int main()
{
srand(time(0));
int n;
char ch = 'y';
while(ch=='y')
{
n= question();
if(n==1)
printf("Very good!\n");
printf("Do you wish to continue(y/n)?\n");
scanf("%c",&ch);
scanf("%c",&ch);
}
return 0;
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
How to convert C++ code to C #include<iostream> #include <stdlib.h> using namespace std; void multiplication() {...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • How convert this c++ into C #include<iostream> #include <stdlib.h> using namespace std; void multiplication() { int...

    How convert this c++ into C #include<iostream> #include <stdlib.h> using namespace std; void multiplication() { int num1; int c, num2, ans; cout<<"Enter difficulty level(1/2)\n"; cin>>c; if(c==1) { num1= rand() % 10; num2= rand() % 10; cout<<"what is"<<num1 <<"*"<<num2<<"?\n" ; cin>>ans while(ans!=(num1*num2)) { if(ans!=(num1*num2)) { cout<< "No. Please try again.\n"; cout<<"what is"<<num1 <<"*"<<num2<<"?\n" ; cin>>ans; } } } else if(c==2) { num1= rand() % 10+90; num2= rand() % 10+90; cout<<"what is"<<num1 <<"*"<<num2<<"?\n" ; cin>>ans; while(ans!=(num1*num2)) { if(ans!=(num1*num2)) { cout<< "No. Please...

  • #include <iostream> #include <conio.h> #include<limits> using namespace std; int main(){ char oparand, ch = 'Y'; int...

    #include <iostream> #include <conio.h> #include<limits> using namespace std; int main(){ char oparand, ch = 'Y'; int num1, num2, result; while(ch == 'Y'){ cout << "Enter first number: "; cin >> num1; while(1){//for handling invalid inputs if(cin.fail()){ cin.clear();//reseting the buffer cin.ignore(numeric_limits<streamsize>::max(),'\n');//empty the buffer cout<<"You have entered wrong input"<<endl; cout << "Enter first number: "; cin >> num1; } if(!cin.fail()) break; } cout << "Enter second number: "; cin >> num2; while(1){ if(cin.fail()){ cin.clear(); cin.ignore(numeric_limits<streamsize>::max(),'\n'); cout<<"You have entered wrong input"<<endl; cout <<...

  • Fix this code so only the function prototype comes before main. #include <iostream> using namespace std;...

    Fix this code so only the function prototype comes before main. #include <iostream> using namespace std; bool isMultiple(int num1, int num2) { return num1 % num2 == 0; } int main() { char ch = 'Y'; int num1, num2; while(ch =='Y') // While ch is equal to Y { cout << "Enter two numbers(largest first): "; cin >> num1; // Getting 1st number cin >> num2; // Getting 2nd number if(isMultiple(num1, num2)) cout << num2 << " " << "IS...

  • Write the missing statements for the following program. #include <iostream> using namespace std; int main(void) {...

    Write the missing statements for the following program. #include <iostream> using namespace std; int main(void) { int Num1; cout << "Enter 2 numbers: ";    cin >> Num2; if (Num1 < Num2) cout << "Smallest number is " << Num1; else cout << "Smallest number is " << Num2;    return 0; }

  • This is C++ code for parking fee management program #include <iostream> #include <iomanip> using namespace std;...

    This is C++ code for parking fee management program #include <iostream> #include <iomanip> using namespace std; void input(char& car, int& ihour,int& imin, int& ohour, int& omin); void time(char car, int ihour, int imin, int ohour, int omin, int& phour, int& pmin, int& round, double& total); void parkingCharge (char car, int round, double& total); void print(char car, int ihour, int imin, int ohour, int omin, int phour, int pmin, int round, double total); int main() { char car; int ihour; int...

  • Could someone re-write this code so that it first prompts the user to choose an option...

    Could someone re-write this code so that it first prompts the user to choose an option from the calculator (and catches if they enter a string), then prompts user to enter the values, and then shows the answer. Also, could the method for the division be rewritten to catch if the denominator is zero. I have the bulk of the code. I am just having trouble rearranging things. ------ import java.util.*; abstract class CalculatorNumVals { int num1,num2; CalculatorNumVals(int value1,int value2)...

  • Game of Craps C++ #include <iostream> using namespace std; void roll (int *); // using pointer...

    Game of Craps C++ #include <iostream> using namespace std; void roll (int *); // using pointer to catch the random variable value of two dicerolls at one time . //these global variable with static variable win use to operate the working of code . //static win, account because two times input given by user to add the diceroll with win number and //account (means balance of user) . static int account = 100; static int win = 0; int bet...

  • I have C++ code. How I can change to Java code #include <iostream> using namespace std;...

    I have C++ code. How I can change to Java code #include <iostream> using namespace std; int initialNumber[4]; int main() {    void ShowResult();       //initialization    int NuberOfpage[20]={1,0,7,1,0,2,1,2,3,0,3,2,4,0,3,0,2,1,0,7},i,j,f12[3];    int num1=0,num2=0,countr=0,framsize=3;    int inital,current,current1;    for(i=0;i<3;i++) { initialNumber[i]=-1; } for(j=0;j<20;j++) { num1=0,num2=0;    for(i=0;i<3;i++) {    if(initialNumber[i]==NuberOfpage[j]) {    num1=1;    num2=1;    break; } }    if(num1==0)    { for(i=0;i<3;i++)    { if(initialNumber[i]==-1)    {    initialNumber[i]=NuberOfpage[j];    num2=1;    break; } } }    if(num2==0)   ...

  • Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void...

    Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void messageAndKey(){ string msg; cout << "Enter message: "; getline(cin, msg); cin.ignore(); //message to uppercase for(int i = 0; i < msg.length(); i++){ msg[i] = toupper(msg[i]); } string key; cout << "Enter key: "; getline(cin, key); cin.ignore(); //key to uppercase for(int i = 0; i < key.length(); i++){ key[i] = toupper(key[i]); } //mapping key to message string keyMap = ""; for (int i = 0,j...

  • #include <iostream> #include <cstdlib> #include <time.h> #include <string> using namespace std; int main() { srand(time (0));...

    #include <iostream> #include <cstdlib> #include <time.h> #include <string> using namespace std; int main() { srand(time (0)); int number, guess, response, reply; int score = 0 number = rand() % 100 + 1; do { do { cout << "Enter your guess "; cin >> guess; score++; if (guess < number) cout << guess << " is too low! Enter a higher number. "; else if (guess > number) cout << guess << " is too high! Enter a lower number....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT