By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,192 Members | 1,783 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,192 IT Pros & Developers. It's quick & easy.

Integer Program

P: n/a
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
Nov 13 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a

"emmx" <kr******@emmx.com> wrote in message
news:65**************************@posting.google.c om...
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 ..
Nov 13 '05 #2

P: n/a
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);
}
Nov 13 '05 #3

P: n/a
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().
--
Martin Ambuhl

Nov 13 '05 #4

P: n/a
emmx wrote:
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;
}

Nov 13 '05 #5

P: n/a
"Steve Zimmerman" <st******@sonic.net> wrote :
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.

--
Simon.
Nov 13 '05 #6

P: n/a
In article <3F**************@sonic.net>, st******@sonic.net says...
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.
*/ counter = 1;
while (counter <= entered) {
for (i = 0; i < counter; i++)
printf("%d", counter);
printf("\n");
counter++;
}

printf("\n");

return 0;
}


--
Randy Howard _o
2reply remove FOOBAR \<,
______________________()/ ()______________________________________________
SCO Spam-magnet: po********@sco.com
Nov 13 '05 #7

P: n/a
Randy Howard wrote:
In article <3F**************@sonic.net>, st******@sonic.net says...
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.
*/
counter = 1;
while (counter <= entered) {
for (i = 0; i < counter; i++)
printf("%d", counter);
printf("\n");
counter++;
}

printf("\n");

return 0;
}

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;
}



Nov 13 '05 #8

P: n/a
Steve Zimmerman wrote:
Randy Howard wrote:
In article <3F**************@sonic.net>, st******@sonic.net says...
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.
*/
counter = 1;
while (counter <= entered) {
for (i = 0; i < counter; i++)
printf("%d", counter);
printf("\n");
counter++;
}

printf("\n");

return 0;
}

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



Nov 13 '05 #9

P: n/a
Steve Zimmerman wrote:
.... 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.

--
Replies should be to the newsgroup
Chuck Falconer, on vacation
Nov 13 '05 #10

P: n/a
In article <3F**************@sonic.net>, Steve Zimmerman wrote:
Randy Howard wrote:
In article <3F**************@sonic.net>, st******@sonic.net says...
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.
*/
counter = 1;
while (counter <= entered) {
for (i = 0; i < counter; i++)
printf("%d", counter);
printf("\n");
counter++;
}

printf("\n");

return 0;
}

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

#include <stdio.h>


#include <stdlib.h>
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;
}
while (counter <= entered) {
for (i = 0; i < counter; i++)
printf("%d", counter);
printf("\n");
counter++;
}

printf("\n");

return 0;
}


--
Neil Cerutti
Nov 13 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.