More Homework ??

This is a discussion on More Homework ?? within the C Programming forums, part of the General Programming Boards category; My next assignment was to take in a positive integer of up to 10 digits (that was the curveball) and ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    76

    More Homework ??

    My next assignment was to take in a positive integer of up to 10 digits (that was the curveball) and display them vertically from right to left. It wasnít bad, except for the 10 digit part. Seems like type int runs out of steam somewhere between 9 and 10 digits and since Iím using modulus, int seems like the logical choice. I found long long int which solved my problem but I have doubts as to if that is what my teacher is looking for, because we havenít learned that yet (never heard of it until today). My question, is there a better or another way to do this? Here is the code:

    Code:
    #include <stdio.h>
    
    int main (void)
    
    {
    	int loop, rem;
    	long long int number, max;
    
    	max = 9999999999;
    
    	printf ("Please enter a number of 10 digits or less: ");
    	scanf ("%lld", &number);
    
    	if (number == 0)
    	{
    		printf ("%lld\n", number);
    	}
    	else
    	{
    
    		while (number > max || number < 0)
    		{
    			printf ("Your number is either too large or negative, please try again: ");
    			scanf ("%lld", &number);
    		}
    
    		for (loop = 0; number != 0; loop++)
    		{	
    			
    			rem = number % 10;
    			number = number / 10;
    			printf ("%d\n", rem);
    		}	
    	}
    }
    This program runs and works fine, just curious about a different method, although long long int seems like a good way to get around my issue.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,481
    Read it into a char array using scanf with a %s or fgets. If the strlen is over 10 chars, or any character they entered is not a digit (use isdigit), it is invalid input. Then, traverse the string from right to left, printing a number on each line.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by dolfaniss View Post
    My next assignment was to take in a positive integer of up to 10 digits (that was the curveball) and display them vertically from right to left. It wasn’t bad, except for the 10 digit part. Seems like type int runs out of steam somewhere between 9 and 10 digits and since I’m using modulus, int seems like the logical choice. I found long long int which solved my problem but I have doubts as to if that is what my teacher is looking for, because we haven’t learned that yet (never heard of it until today). My question, is there a better or another way to do this?
    I don't see a problem with you getting ahead of your instructor. In his place, I would congratulate your intiative.
    Learning isn't about being spoon fed information. It's about taking the challenge to understand a new topic.


    This program runs and works fine, just curious about a different method, although long long int seems like a good way to get around my issue.
    Well, you can always take anduril's trick and give it a try. (That would theoretically work for hundreds, even thousands of digits, however you should be aware that it's operating on a string... not a number).

    You could also take each digit as a separate member in an int, short or char array (using getchar() or such as your input function) and print the elements 1 per line.

    There's lots of ways to solve this particular problem. I would personally favor your current method because it shows both intiative and understanding of math.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    76
    Thanks guys, I will keep it as is.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by dolfaniss View Post
    Thanks guys, I will keep it as is.
    I would create 2 versions of the solution - one current that uses long long int's and another as suggested - using strings. So the tutor could select whichever he likes more
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with C++ Homework
    By TheTaoOfBill in forum C++ Programming
    Replies: 3
    Last Post: 10-08-2007, 07:17 PM
  2. C++ Homework Help!!!!
    By bar5037 in forum C++ Programming
    Replies: 12
    Last Post: 10-01-2007, 01:21 PM
  3. Homework?
    By Cloud6 in forum C++ Programming
    Replies: 19
    Last Post: 07-13-2006, 07:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21