*Ask the user to input a number.
*If the number is
< 10 print “Number is less than 10”
< 20 print “Number is less than 20 but greater than 10”
< 30 print “Number is less than 30 but greater than 20”
< 40 print “Number is less than 40 but greater than 30”
< 50 print “Number is less than 50 but greater than 40”
< 50 print “Number is greater than 50”
*Use a loop to repeat this process until the user inputs -1.
Output:
Input a number (-1 to exit): 5
Number is less than 10_
Input a number (-1 to exit): 19
Number is less than 20 but greater than 10.
Input a number (-1 to exit): 25
Number is less than 30 but greater than 20.
Input a number (-1 to exit): 55
Number is greater than 50.
Input a number (-1 to exit): -1
Press any key to continue…
Please note, that this is not a homework site. We won't do your homework for you. The purpose of homework is that you learn by doing. However we are always willing to help solve problems you encountered, correct mistakes you made in your code and answer your questions.
We didn't see your attempts to solve this problem youself and so we cannot correct mistakes you didn't made and answer questions you didn't ask. To get help you should do something yourself and get real problems with something. If your problem is "I don't understand a thing", then you should go back to basics and study again. As it is impossible to find deriviative of function without knowledge in ariphmetics, you cannot do more complex tasks in programming without clear understanding of basics. ————————————
What did you try and where did you fail? What part of this assigment gives you problems?
#include <stdio.h>
int main(void)
{
int a=0;
printf("please enter a number : \n");
scanf("%i",&a);
switch(a)
{
if (a<=10);
printf("Number is less than 10");
break;
if (a<=20 && a>10);
printf("Number is less than 20 but greater than 10");
break;
if (a<=30 && a>20);
printf("Number is less than 30 but greater than 20");
break;
if (a<=40 && a>30);
printf("Number is less than 40 but greater than 30");
break;
if (a<=49 && a>40);
printf("Number is less than 50 but greater than 40");
break;
if (a>=50);
printf("Number is greater than 50");
break;
return 0;
}
}
Get rid of switch. It is not needed here. You just need a if-elseif chain here:
1 2 3 4 5 6 7
if (a < 10) {
//Handle case a<10 here
} elseif (a < 20) {
//Handle case a<20 here
//a which are less than 10 will not be handled by this routine,
//as they are catched by previous if. So no extra condition needed
} elseif /*...*/
#include<stdio.h>
int main(void)
{
int a;
printf("please enter a number : \n");
scanf("%i",&a);
switch(a)
{
if (a<=10){
printf("Number is less than 10");
break;
} elseif (a<=20 && a>10){
printf("Number is less than 20 but greater than 10");
break;
} elseif (a<=30 && a>20){
printf("Number is less than 30 but greater than 20");
break;
} elseif (a<=40 && a>30){
printf("Number is less than 40 but greater than 30");
break;
} elseif (a<=49 && a>40){
printf("Number is less than 50 but greater than 40");
break;
} elseif (a>=50){
printf("Number is greater than 50");
break;
}
return 0;
}
}
#include<stdio.h>
int main(void)
{
int a;
printf("please enter a number : \n");
scanf("%i",&a);
{
if (a<=10){
printf("Number is less than 10");
} elseif (a<=20 && a>10){
printf("Number is less than 20 but greater than 10");
} elseif (a<=30 && a>20){
printf("Number is less than 30 but greater than 20");
} elseif (a<=40 && a>30){
printf("Number is less than 40 but greater than 30");
} elseif (a<=49 && a>40){
printf("Number is less than 50 but greater than 40");
} elseif (a>=50){
printf("Number is greater than 50");
}
return 0;
}
}
#include<iostream>
usingnamespace std;
int main ()
{
int a;
while (true){
cout <<"Input a Number (-1 to exit): ";
cin >> a;
if (a<=10 && a>0)
cout << "Number is less than 10 \n";
elseif (a<=20 && a>10)
cout << "Number is less than 20 but greater than 10 \n";
elseif (a<=30 && a>20)
cout << "Number is less than 30 but greater than 20 \n";
elseif (a<=40 && a>30)
cout << "Number is less than 40 but greater than 30 \n";
elseif (a<=50 && a>40)
cout << "Number is less than 50 but greater than 40 \n";
else
cout << "Number is greater than 50 \n";}
system ("pause");
return 0;
}
my output must be like this:
Input a number (-1 to exit): 5
Number is less than 10_
Input a number (-1 to exit): 19
Number is less than 20 but greater than 10.
Input a number (-1 to exit): 25
Number is less than 30 but greater than 20.
Input a number (-1 to exit): 55
Number is greater than 50.
Input a number (-1 to exit): -1
Press any key to continue…
I also tried this one, and the output are correct until i put -1 but when the display said
"Press any key to continue..." and i press any key the loop still continue the process..
#include<iostream>
usingnamespace std;
int main ()
{
int a;
while (true){
cout <<"Input a Number (-1 to exit): ";
cin >> a;
if (a<=10 && a>0)
cout << "Number is less than 10 \n";
elseif (a<=20 && a>10)
cout << "Number is less than 20 but greater than 10 \n";
elseif (a<=30 && a>20)
cout << "Number is less than 30 but greater than 20 \n";
elseif (a<=40 && a>30)
cout << "Number is less than 40 but greater than 30 \n";
elseif (a<=50 && a>40)
cout << "Number is less than 50 but greater than 40 \n";
elseif (a>50)
cout << "Number is greater than 50 \n";
else
system ("pause");
}
return 0;
}
#include<iostream>
usingnamespace std;
int main ()
{
int a;
bool run = true;
while (run){
cout <<"Input a Number (-1 to exit): ";
cin >> a;
if(a==-1) // Checks now for -1 and exits the while loop
{
cout << "Okay, we're finished..\n";
run = false;
}
elseif (a>=0 && a < 10)
cout << "Number is less than 10 \n";
elseif (a<20 && a>=10)
cout << "Number is less than 20 but greater than 10 \n";
elseif (a<30 && a>=20)
cout << "Number is less than 30 but greater than 20 \n";
elseif (a<40 && a>=30)
cout << "Number is less than 40 but greater than 30 \n";
elseif (a<50 && a>=40)
cout << "Number is less than 50 but greater than 40 \n";
elseif (a>=50)
cout << "Number is greater than 50 \n";
}
system ("pause"); // Not really a good idea to use system calls
return 0;
}