Basic calculator using do while

How to solve it with do while loop

hi @kumarakash121005
refer this -->

#include<iostream>
using namespace std;
int main() {
	int a,b,c;
	char ch;
	do
	{
		cin>>ch;
        if((ch=='X')||(ch=='x'))
        {
            break;
        }
        else if(ch=='+' || ch=='-' || ch=='*' || ch=='/' || ch=='%')
        {
			cin>>a>>b;
		switch(ch)
		{
			case '+':c=a+b;
			break;
			case '-':c=a-b;
			break;
			case '*':c=a*b;
			break;
			case '/':c=a/b;
			break;
			case '%':c=a%b;
			break;
		}
		cout<<c<<endl;
        }
		else
		{
			cout<<"Invalid operation. Try again."<<endl;
		}
	}
	while(1);
	return 0;
}

basic

after trying my own code i paste your code but it shows tle


its working perfectly fine and passing all test cases…

i don’t know but it shows tle

are bro firse run krke dekho na… bolne se phle check kiya kro 2 bar… code submit kro… i can’t see any submission from ur side… click on submit button…

now it works thanks for the solution

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

sir please explain while(1)

it means we are running this loop till its true, ie till we are reading input… when we encounter x, we break from loop…

ok sir …
one more thing is it necessary that in switch case we have to use the default case??

No it’s not necessary

1 Like