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.
Ya it does. Thanks alot but i came across different issue when i combine them together. 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 ;
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 ;
YES!! it works now... Thanks.. can you explain y it has to be 1 not 0? is it because i use x[20 + 1] ? Between, i still 1 and small issue, my prime number part coding it shows 1 has prime when entered. how shall i improvise tht?
constunsignedint size = 20;
int x[size];
for (unsignedint i = 0; i < size; ++i ) {
}
That is the idiomatic format of the for loop. Remember array subscripts start at zero, and finish at size -1. At the moment the array only has 19 usable numbers.
x[20 + 1] is a hack that caused problems. Also realise that just because a program works doesn't necessarily mean that is correct.
Good Luck !!
@closed account 5a8Ym39o6 (843)
With about 300 usable posts under your belt, you might not promote such solutions.