Currently my code is not working, Im trying to count the total of each remainder from the 20 numbers user inputs. How shall i improve it so that i can total them up using switch statement.
#include <iostream>
using namespace std;
int main()
{
int i, x[20], count;
cout << "Enter 20 integer numbers from 0 to 99: " <<endl;
for (i=1;i<=20;i++)
{
cout << "Input " << i <<":";
cin >> x[i];
}
double remainder ;
for(i=1; i<=20; i++)
{
switch (x[i] % 8)
{
case 0 :
remainder = x[i] % 8 ;
count++ ;
cout << "Total number with remainder zero is " << count << endl ;
break;
case 1 :
remainder = x[i] % 8 ;
count++ ;
cout << "Total number with remainder one is " << count << endl ;
break;
case 2 :
remainder = x[i] % 8 ;
count++ ;
cout << "Total number with remainder two is " << count << endl ;
break;
case 3 :
remainder = x[i] % 8 ;
count++ ;
cout << "Total number with remainder three is " << count <<endl ;
break;
default :
remainder = x[i] % 8 ;
count++ ;
cout << "Total number with remainder other than zero, one, two and three is " << count << endl ;
}
}
return 0 ;
}
Example how the output should look:
Total number with remainder 0 is 4.
Total number with remainder 1 is 6.
Total number with remainder 2 is 5.
Total number with remainder 3 is 3.
Total number of other remainder is 2.
There also shouldn't be 2 topics about essentially the same subject. There is a risk that the same things will be said in both, possibly making it a time waster for those who reply.
thanks for improving it. But now when i combine them with rest of my codes. it has some errors. First is the max number is giving some huge number like 1354628. second my prime number code is showing 1 has prime number where it should not. I hope someone can pin point where is the mistake
#include <iostream>
using namespace std;
int main()
{
int i,t, max, min, x[20 + 1], even, odd, prime;
even = 0;
odd = 0;
cout << "Enter 20 integer numbers from 0 to 99: "<<endl;
for (i=1;i<=20;i++)
{
cout << "Input " << i <<":";
cin >> x[i];
}
min=x[0];
max=x[0];
for(int i=0;i<20;i++)
{
if(min>x[i])
{
min=x[i];
}
else if(max<x[i])
{
max = x[i];
}
}
cout<<"\nMax is "<< max <<endl;
cout<<"\nMin is "<< min <<endl;
break;
default :
remainder = x[i] % 8 ;
remainder_other += 1;
}
}
cout << "Total number with remainder zero is " << remainder_zero << endl;
cout << "Total number with remainder one is " << remainder_one << endl;
cout << "Total number with remainder two is " << remainder_two << endl;
cout << "Total number with remainder three is " << remainder_three << endl;
cout << "Total number with remainder other than zero, one, two and three is " << remainder_other << endl ;