Integer Program

E

emmx

Hi
I am new to c. I am trying to write a simple program which will
prompt the user to enter an integer between 1 and 9 and the output
will be produced as follows (eg. 5)

1
22
333
4444
55555

I have been trying to write this with a while loop but I have gotten
stuck. I am using the book, "C how to program 4th edition", but there
is no answer for this question and there are no resources for this
edition on the dietel site.

If someone could look at the code that I have so far (it is not much -
please remember I have only been learning this a few weeks) and offer
a suggestion as to how I can get the program to print only the numbers
up to the integer entered this would be appreciated.

#include <stdio.h> /* for library function: printf */

/* function main begins program execution */
main()
{
char string[]; /* initialisation */

printf ( "Enter an integer between 1 and 9:\n" );
scanf ( "%s", string );

/* loop until user keys end-of-file sequence */
while ( string >= 1 ) && ( string <= 9);




} /* end while */
} /* end function main */

Thankyou
 
C

code_wrong

emmx said:
Hi
I am new to c. I am trying to write a simple program which will
prompt the user to enter an integer between 1 and 9 and the output
will be produced as follows (eg. 5)

1
22
333
4444
55555

I have been trying to write this with a while loop but I have gotten
stuck. I am using the book, "C how to program 4th edition", but there
is no answer for this question and there are no resources for this
edition on the dietel site.

If someone could look at the code that I have so far (it is not much -
please remember I have only been learning this a few weeks) and offer
a suggestion as to how I can get the program to print only the numbers
up to the integer entered this would be appreciated.

#include <stdio.h> /* for library function: printf */

/* function main begins program execution */
main()
{
char string[]; /* initialisation */

printf ( "Enter an integer between 1 and 9:\n" );
scanf ( "%s", string );

/* loop until user keys end-of-file sequence */
while ( string >= 1 ) && ( string <= 9);




} /* end while */
} /* end function main */

Here is one solution:

#include <stdio.h> /* for library function: printf */

/* function main begins program execution */
main()
{
int input=0,i=1,j; /* initialisation */

printf ( "Enter an integer between 1 and 9:\n" );
fflush(stdout);
scanf ( "%i", &input);
getchar(); /*absorb the newline character lurking in stdin*/

while (i<=input)
{
for(j=0;j<i;j++)
{
printf("%i",i);
}
printf("\n");
i++;
}
printf("press return to exit");
getchar(); /*hold the consol window open*/

} /* end function main */

Of course it does not deal with incorrect user input and may behave
differently on your compiler ..
 
G

Greg P.

This is the simplest way I could think of for showing you. I'm glad you made
the effort as some people do not before asking how to do something (on this
newsgroup):
--------------------------------
#include <stdio.h> /* For printf() and scanf() */

/* main() must be of type int, and return an int */
int main()
{
int entry; /* Primitive type to hold input */

printf ( "Enter an integer between 1 and 9: " );

/* scanf() gets a decimal ("%d") and puts it into entry (need to use the
address &) */
scanf ( "%d", &entry );

/* is entry between 1 and 9? */
if ( ( entry >= 1) && ( entry <= 9 ) )
{
/* count through num until we reach the value of entry-1 */
for ( int num = 0; num <= entry; ++num)
{
/* print out the number in num until the value of num is reached
*/
for ( int iter = 0; iter < num; ++iter)
printf ( "%d", num );

printf ("\n");
/* fflush() insures that the output is shown */
fflush(stdout);
}
}

/* entry was not between 1 and 9, print error message */
else
printf("Invalid Number!\n");

/* always return 0...for now */
return(0);
}
 
M

Martin Ambuhl

Greg P. wrote:

printf ( "Enter an integer between 1 and 9: " );

/* scanf() gets a decimal ("%d") and puts it into entry (need to use the
address &) */
scanf ( "%d", &entry );

Just a note: You are wanting a 'fflush(stdout);' statement between the
'\n'-less printf() and the scanf().
 
S

Steve Zimmerman

emmx said:
Hi
I am new to c. I am trying to write a simple program which will
prompt the user to enter an integer between 1 and 9 and the output
will be produced as follows (eg. 5)

1
22
333
4444
55555

I have been trying to write this with a while loop but I have gotten
stuck. I am using the book, "C how to program 4th edition", but there
is no answer for this question and there are no resources for this
edition on the dietel site.

If someone could look at the code that I have so far (it is not much -
please remember I have only been learning this a few weeks) and offer
a suggestion as to how I can get the program to print only the numbers
up to the integer entered this would be appreciated.

#include <stdio.h> /* for library function: printf */

/* function main begins program execution */
main()
{
char string[]; /* initialisation */

printf ( "Enter an integer between 1 and 9:\n" );
scanf ( "%s", string );

/* loop until user keys end-of-file sequence */
while ( string >= 1 ) && ( string <= 9);




} /* end while */
} /* end function main */

Thankyou

You've made a good try. Here's a program that works:

#include <stdio.h>

int main()
{
int entered;
int i;
int counter;

printf("Please enter an integer between 1 and 9: ");
scanf("%d", &entered); /* You must use "&" with scanf. */

counter = 1;
while (counter <= entered) {
for (i = 0; i < counter; i++)
printf("%d", counter);
printf("\n");
counter++;
}

printf("\n");

return 0;
}
 
S

Simon Biber

Steve Zimmerman said:
int main()
{
int entered;
int i;
int counter;

printf("Please enter an integer between 1 and 9: ");
scanf("%d", &entered); /* You must use "&" with scanf. */

counter = 1;
while (counter <= entered) {
for (i = 0; i < counter; i++)
printf("%d", counter);
printf("\n");
counter++;
}

printf("\n");

return 0;
}

Please use spaces rather than tabs for indenting code when
posting to newsgroups. I recommend using two, three or four
spaces per indentation level. Certainly not eight.
 
R

Randy Howard

You've made a good try. Here's a program that works:
Really?

#include <stdio.h>

int main()
{
int entered;
int i;
int counter;

printf("Please enter an integer between 1 and 9: "); fflush(stdout);
scanf("%d", &entered); /* You must use "&" with scanf. */
/* what happens if the user enters
* -16 (exits with no output at all other than the \n)
* 31435 (runs a painfully long time)
* "fish market" (random results with varying degrees of excitement)
* using the return value from scanf and verifying the data
* is in the range 1-9 is worthwhile, if not expected.
*/
 
S

Steve Zimmerman

Randy said:
/* what happens if the user enters
* -16 (exits with no output at all other than the \n)
* 31435 (runs a painfully long time)
* "fish market" (random results with varying degrees of excitement)
* using the return value from scanf and verifying the data
* is in the range 1-9 is worthwhile, if not expected.
*/


You make a good point, Randy. I've reworked the program:

#include <stdio.h>

int main()
{
int entered;
int i;
int countered = 1;

printf("Enter an integer between 1 and 9, inclusive\n"
"All other inputs exit the program: ");
scanf("%d", &entered);

if (entered < 1 || entered > 9)
exit(1);

while (counter <= entered) {
for (i = 0; i < counter; i++)
printf("%d", counter);
printf("\n");
counter++;
}

printf("\n");

return 0;
}
 
S

Steve Zimmerman

Steve said:
You make a good point, Randy. I've reworked the program:

#include <stdio.h>

int main()
{
int entered;
int i;
int counter = 1;

printf("Enter an integer between 1 and 9, inclusive\n"
"All other inputs exit the program: ");
scanf("%d", &entered);

if (entered < 1 || entered > 9)
exit(1);

while (counter <= entered) {
for (i = 0; i < counter; i++)
printf("%d", counter);
printf("\n");
counter++;
}

printf("\n");

return 0;
}

Sorry. The variable "countered" should be "counter"

(Line 7).



Steve
 
L

LibraryUser

Steve said:
.... snip ...

You make a good point, Randy. I've reworked the program:

#include <stdio.h>

int main()
{
int entered;
int i;
int countered = 1;

printf("Enter an integer between 1 and 9, inclusive\n"
"All other inputs exit the program: ");
scanf("%d", &entered);

if (entered < 1 || entered > 9)
exit(1);

while (counter <= entered) {
for (i = 0; i < counter; i++)
printf("%d", counter);
printf("\n");
counter++;
}
printf("\n");
return 0;
}

I think you miss the point. You have still not checked the
return value from scanf.
 
N

Neil Cerutti

You make a good point, Randy. I've reworked the program:

#include <stdio.h>

#include said:
int main()

int main(void) is preferable to unspecified args.
{
int entered;
int i;
int countered = 1;

printf("Enter an integer between 1 and 9, inclusive\n"
"All other inputs exit the program: ");
scanf("%d", &entered);

if (entered < 1 || entered > 9)
exit(1);

Verify scanf stores something in entered before using its value.

if (scanf("%d", &entered) != 1 || entered < 1 || entered > 9) {
return EXIT_FAILURE;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,077
Messages
2,570,568
Members
47,204
Latest member
abhinav72673

Latest Threads

Top