473,320 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

loop and switch.. help:)

houserocks13
im quite confused with loops and switch.. will you please help me:)
i have 3 problems..:)

1.Write a program that will ask for a long number and count how many digits are there in the number.
ex. Enter num: 10854
Output: 5

2.Write a program that will ask for a long number and count how many digits in the number are even and how many are odd.
ex. Enter num: 80572
Output: 3 digits are even
2 digits are odd

3.Write a program that asks for an input integer and displays the digits in reverse.
ex. Enter number: 3562 ex. Enter number: 10240
Output: 2653 Output: 04201
Nov 13 '06 #1
7 3233
horace1
1,510 Expert 1GB
implement and test part 1 first then deal with the others when that is working, i.e.

1.Write a program that will ask for a long number and count how many digits are there in the number.
ex. Enter num: 10854
Output: 5

probably the simplest way is to read the number into a char array and use the string processing functions in <string.h>
Nov 13 '06 #2
hehehe.. we havent tackeld arrays yet.. only loops.. so we are supposed to loop for this program.. my syntaz doesnt work..=( it doesnt display the 0 wahhh...
Nov 14 '06 #3
r035198x
13,262 8TB
hehehe.. we havent tackeld arrays yet.. only loops.. so we are supposed to loop for this program.. my syntaz doesnt work..=( it doesnt display the 0 wahhh...
Well have you done string manipulation then? You'd need to read in the number as a string and access each character of the string for manipulation using string methods
Nov 14 '06 #4
not yet=(

for this number
2.Write a program that will ask for a long number and sum the even and odd digits in the number.

my prof changed it

i have this code:
#include <iostream.h>


void main()
{
int num, r, evensum=0, oddsum=0;

cout<<"Enter number: "<< endl;
cin>>num;

r=num/10;
while (num>0)
{
if (r%2==0)
evensum=evensum+r;
else
oddsum=oddsum+r;
num--;
}
cout<<"even is: "<<evensum<< endl;
cout<< "odd is "<< oddsum<<endl;
}


but it wont print the sum.. huhu=(
Nov 14 '06 #5
horace1
1,510 Expert 1GB
you had a few errors
Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2.  
  3. int main()
  4. {
  5. int num, r, evensum=0, oddsum=0;
  6.  
  7. cout<<"Enter number: "<< endl;
  8. cin>>num;
  9.  
  10. while (num>0)           // loop thru all digits
  11. {
  12. if (num%2==0)           // check if even number
  13. evensum=evensum+1;
  14. else
  15. oddsum=oddsum+1;
  16. num=num/10;             // check next digit
  17. }
  18. cout<<"even is: "<<evensum<< endl;
  19. cout<< "odd is "<< oddsum<<endl;
  20. }
  21.  
  22.  
you should now be able to extend this to solve your other problems
Nov 15 '06 #6
koder
23
implement and test part 1 first then deal with the others when that is working, i.e.

1.Write a program that will ask for a long number and count how many digits are there in the number.
ex. Enter num: 10854
Output: 5

probably the simplest way is to read the number into a char array and use the string processing functions in <string.h>
better use '%' (Modulus operator) ok?
Nov 15 '06 #7
horace1
1,510 Expert 1GB
better use '%' (Modulus operator) ok?
Usually there are serveral ways of implementing an algorithm which vary in simplicity, efficency (run time or memeory required), etc.
For example, using a char array to count the number of even and odd digits entered at the keyboard
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int evensum=0, oddsum=0;
  7. char number[50];
  8. cout<<"Enter number: "<< endl;
  9. cin>>number;                           // read in as characters
  10.  
  11. for(int i=0; number[i] != '\0'; i++)   // loop thru all digits
  12.   if ((number[i] & 1) == 0)            // check if even number
  13.        evensum=evensum+1;
  14.    else
  15.        oddsum=oddsum+1;
  16. cout<<"even is: "<<evensum<< endl;
  17. cout<< "odd is "<< oddsum<<endl;
  18. }
  19.  
This uses the & operator to determine if a character is odd or even.
It can deal with a larger number of digits than reading an int and at the end of this code we still have the original data in number[].
It all depends what the specification is.
Nov 15 '06 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Francis Bell | last post by:
Hello, I've got a 25 line file with lines of data like this: sp/spinnerbait/AAA Lures/Mad Phil/silver/bass/1/1 The first field is the code that determines what to do. I need to loop through this...
2
by: Alan Silver | last post by:
Hello, I have code like the following... foreach (Control ctl in Page.Controls) { if (ctl.ID.StartsWith("X_")) { // do stuff } }
7
by: steve marchant | last post by:
trying to learn VB6. Simple counting loop which counts to 8 in 1 sec intervals, then starts from 1 again and repeats. Have two Command buttons on the form. Cmd1 starts the counting, and I need to...
5
by: mturner64 | last post by:
Good day. I am working on a program that will allow a user to enter an undefined series of numbers and enter -99 when finished. The program should output the Greatest and Least of the series...
5
by: justbovo | last post by:
Hello, My name is Justin. I am working on a part of a Find Memory module for a program. Here's how it works.. I enter '13' at the main menu which branches out to my Find Memory module. I ask...
0
by: game8424 | last post by:
How do you in a loop or with an array go from Code: Dim Variable1 As String Call ibrd(udDevice, Variable1) Dim VariableDbl1 As Double VariableDbl1 = CDbl(Variable1)
2
by: perkykoala | last post by:
I apologize in advance for being REALLY detailed/verbose. It's the result of staring/tweaking code for too long. Using VB 2005: I need to design a multiple choice test (unfortunately, I can't...
4
by: charmeda103 | last post by:
i working on a program in c++ and my program is to read data from a file. I using a while loop. the while loop is not reading the dates of each line. mm/dd/yyyy everything in the file is reading...
2
by: thoffman | last post by:
Hello again everyone! I have an issue that I can't seem to find a straight answer for anywhere... I have a For loop that sends a file name to a background worker that contains some long running...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.