print 5 numbers and return corresponding asterisks - C program - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

print 5 numbers and return corresponding asterisks - C program

raeikoraeiko Posts: 17Member
Hello everyone,

once more i need you help! I need to write a C program that imputs 5 numbers between 1 and 30 and returns the corresponding number of adjacent asterisks. I cannot use neither the do...while nor the array functions because i haven't studied them yet.

I have tried varius combinations like the for loop followed by nested if statements to test if the numbers are included between 1 and 30,but it doesn't work.

I don't get how to connect the testing part with the printing of the asterisks.

I don't want you to do my homework, i would only like you to give me some hints on how to proceed after the for loop statement.

Here is what i have (just the beginning,i know...):
[code]#include

int main( void ) /* function main begins program execution */
{
int num1, num2, num3, num4, num5, x;

printf( "Enter five numbers between 1 and 30:" );
scanf_s( "%d%d%d%d%d", num1, num2, num3, num4, num5 );

for ( x = 1; x <= 5; x++ );



return 0;
}[/code]

Thank you in advance for your help,

raeiko

Comments

  • anuragmsinghanuragmsingh Posts: 1Member
    [code]#include

    main()
    {
    int a,b,c,d,e,i;

    printf("Enter the numbers:-");
    scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);

    printf("The output is:-

    %d: ",a);
    for(i=1;i<=a;i++)
    {
    printf("*");
    }
    printf("

    %d: ",b);
    for(i=1;i<=b;i++)
    {
    printf("*");
    }
    printf("

    %d: ",c);
    for(i=1;i<=c;i++)
    {
    printf("*");
    }
    printf("

    %d: ",d);
    for(i=1;i<=d;i++)
    {
    printf("*");
    }
    printf("

    %d: ",e);
    for(i=1;i<=e;i++)
    {
    printf("*");
    }

    }

    [/code]
  • raeikoraeiko Posts: 17Member
    Thanks a million!!!

    I have rearranged what i did on the bases of what you have suggested me and here is what i've got:

    [code]#include

    int main( void ) /* function main begins program execution */
    {
    int num1, num2, num3, num4, num5, x; /* declare variables */

    printf("Enter 5 numbers between 1 and 30:"); /* prompt for input */
    scanf("%d%d%d%d%d", &num1, &num2, &num3, &num4, &num5 ); /* read numbers from user */

    for ( x = 1; x <= num1; x++){ /* associate numbers to asterisks and show output */
    printf("*");
    }
    printf( "
    " );

    for ( x = 1; x <= num2; x++){
    printf("*");
    }
    printf( "
    " );

    for ( x = 1; x <= num3; x++){
    printf("*");
    }
    printf( "
    " );

    for ( x = 1; x <= num4; x++){
    printf("*");
    }
    printf( "
    " );

    for ( x = 1; x <= num5; x++){
    printf("*");
    }
    printf( "
    " );


    /* test if input numbers are equal to 0 or greater than 30 and display error message */
    if ( num1 == 0 || num1 > 30 )
    printf( "Incorrect numbers. Please enter 5 numbers between 1 and 30:
    " );

    else if ( num2 == 0 || num2 > 30 )
    printf( "Incorrect numbers. Please enter 5 numbers between 1 and 30:
    " );

    else if ( num3 == 0 || num3 > 30 )
    printf( "Incorrect numbers. Please enter 5 numbers between 1 and 30:
    " );

    else if ( num4 == 0 || num4 > 30 )
    printf( "Incorrect numbers. Please enter 5 numbers between 1 and 30:
    " );

    else if ( num5 == 0 || num5 > 30 )
    printf( "Incorrect numbers. Please enter 5 numbers between 1 and 30:
    " );


    return 0;
    }[/code]


    It works now:-)

    Thanks again,

    raeiko
Sign In or Register to comment.