-2

Given one integer N, for each digit from the end of N, print a line of stars which has number of stars equal to that digit C++

Example Input: 2863

Example Output:

***
******
********
**

my code work in inputs like 12345,..,etc. but not work in 2863 !! i dont why and how can i edit my code to handle this problem !

#include <stdio.h>

int main()
{
    int b,d,j=0,num,num1,length=0,length1,x[100];
    float a,c;

    scanf("%d",&num);

    num1=num;

    while(num!=0){
        num/=10;
        length++;
    }

    length1=length;

    for(int i=0; length!=0; i++){
        a=(float)num1/10;
        b=num1/10;
        c=a-(float)b;
        d=c*10;
        x[i]=d;
        length--;
        num1=b;printf("%d\n",d);
    }

    while(length1!=0){
        while(x[j]!=0){
            printf("*");
            x[j]--;
        }
        length1--;
        j++;
        printf("\n");
    }
}
4
  • Example Output: *** ****** ******** ** Jul 7, 2014 at 18:11
  • Have you learned about the % (remainder) operator?
    – Ben Voigt
    Jul 7, 2014 at 18:13
  • Yes.. %10 returns the last digit of number but how can i return the original number without last digit ?! Jul 7, 2014 at 18:17
  • With / 10, just like your code already has.
    – Ben Voigt
    Jul 7, 2014 at 18:20

2 Answers 2

0

Try this:

void print(int num)
{
    while (num > 0)
    {
        int digit = num%10;
        for (int i=0; i<digit; i++)
            printf("*");
        printf("\n");
        num /= 10;
    }
}

int main()
{
    int num;
    scanf("%d",&num);
    print(num);
    return 0;
}
0

My five cents.

#include <iostream>
#include <iomanip>


int main() 
{
    std::cout << std::setfill( '*' );

    while ( true )
    {
        const unsigned int Base10 = 10;

        std::cout << "Enter non-negative number (0 - exit): ";
        unsigned int n = 0;
        std::cin >> n;

        if ( !n ) break;

        std::cout << std::endl;

        do
        {
            std::cout << std::setw( n % Base10 + 1 ) << '\n';
        } while ( n /= Base10 );

        std::cout << std::endl;
    }

    return 0;
}
6
  • Nifty way of making the standard library do the work. I'm a little surprised that setw affects operator<<(char), though.
    – Ben Voigt
    Jul 7, 2014 at 18:21
  • Looking at OP's code, I would say that he or she are kind of... well... not quite ready for it just yet. Perhaps something a little more basic would be a little more suitable to the current knowledge at hand... Jul 7, 2014 at 18:23
  • @barak manos When I show a more basic code then at once some idiots appear that down vote my post because it as they think is primitive. Jul 7, 2014 at 18:29
  • Wow, I could not have agreed with you more!!! Especially with regards to using functions like printf when the tag says C++. As if that's the main issue of the question at hand. Jul 7, 2014 at 18:49
  • Here is one such example when I gave an answer to a question which although was tagged as C++, you could easily notice that OP's issue had nothing to do with a specific syntax unique to C++, but something completely functional. I gave this answer: stackoverflow.com/a/21246119/1382251, and used printf just in order to give a usage example. Then, as you may see in the comments, one of those idiots that you mention immediately started putting me off about it being C not C++ (including down-votes and everything). Jul 7, 2014 at 18:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.