473,666 Members | 2,138 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Integer Program

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
10 2884

"emmx" <kr******@emmx. com> wrote in message
news:65******** *************** ***@posting.goo gle.com...
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
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
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
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
"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
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
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
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
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
9143
by: GS | last post by:
The stdint.h header definition mentions five integer categories, 1) exact width, eg., int32_t 2) at least as wide as, eg., int_least32_t 3) as fast as possible but at least as wide as, eg., int_fast32_t 4) integer capable of holding a pointer, intptr_t 5) widest integer in the implementation, intmax_t Is there a valid motivation for having both int_least and int_fast?
9
38504
by: Dante | last post by:
I'm converting a C# program to VB.net, but i'm having problems converting a integer to a byte in the same way as the c# program does. //C# program int i = 137694; byte b = (byte) i; //b returns as value 222 'VB program
1
7414
by: redpayne | last post by:
Okay, I finally got this program to run according to what the book had us build it as. Now prof wants case 2 and case 3 to prompt again for input, check input to see if it is the correct type, then display a "correct" message if is correct. So, If you choose 2 from the original pane, you would be prompted to enter an integer. The program would check to see if you input an integer and if it was then you would get a "correct" message. If not...
232
13242
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first set of examples, after decoding the HTML FORM contents, merely verifies the text within a field to make sure it is a valid representation of an integer, without any junk thrown in, i.e. it must satisfy the regular expression: ^ *?+ *$ If the...
20
12415
by: chutsu | last post by:
I'm trying to compare between pointer and integer in an "IF" statement how do I make this work? if(patient.id != NULL){ } Thanks Chris
1
3770
by: haelly | last post by:
write a program that prompts the user to enter three different positive integer values.If the values are not different, the program prints a message"equal value" and terminates(hint:use the return statement).If eithere of the value is negative, the program prints "negative input" and terminates.Otherwise it prints the value the user entered. After that,the program compares he first integer and the second integer and prints either, "the first...
30
4285
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
Let's say we had a simple function for returning the amount of days in a month: unsigned DaysInMonth(unsigned const month) { switch (month) { case 8: case 3: case 5:
12
3284
by: lithiumcat | last post by:
Hi, I bothered you a while back about storing integer values in void*. Now in a completely unrelated context, I'm trying to store pointer values in an integer type. So the basic question is, is it possible to convert a pointer into an integer, and then later (but on the same execution environment, ie the program has not exited, thus it's the same architecture, same compiler, same binary representations and so on) retrieve from the
20
2276
by: Robbie Hatley | last post by:
I needed a quick program called "random" that gives a random positive integer from n1 to n2. For example, if I type "random 38, 43", I want the program to print a random member of the set {38, 39, 40, 41, 42, 43}. Also, I read in my compiler's documentation the following: To get a random number in the range 0..N, use rand()%(N+1). Note that the low bits of the rand's return value are not very random, so rand()%N for small values of N...
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8551
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8640
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7386
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.