HI can give suggesstion

A

aslamhenry

how to make these program huhu

please any key in 5 digit : 56789

ouput

5678 9
567 89
56 789
5 6789

take a look at my coding...why the ouput is rubbish huhuhu

#include <stdio.h>

int main(void)
{
float num;
int i, j, x ;

printf("please key in any 5 digit number:");
scanf("%f",&num);




for ( i=0 ; 5>i ; ++i){

for(j=0 ; i>j ; ++j){
printf(" ") ;
}

for( x = j+1 ; 5 >= x ; ++x){

printf("%.0f", num);
}
putchar('\n');
}

return 0;
}
 
W

Walter Roberson

how to make these program huhu
please any key in 5 digit : 56789

5678 9
567 89
56 789
5 6789
take a look at my coding...why the ouput is rubbish huhuhu
printf("please key in any 5 digit number:");
scanf("%f",&num);

Don't read the number as floating point: read it as a pure string
of characters. You don't ever need to know the numeric value:
you just need to manipulate the characters the person typed in.
With the requirements you have presented, the code you will
arrive out should pretty much be able to handle input strings
such as Kv8$} and do the staggered printing, if not for the
checking that you will do to ensure that the user only entered
digits (and exactly 5 of them.)
 
M

Mark Bluemel

how to make these program huhu

please any key in 5 digit : 56789

ouput

5678 9
567 89
56 789
5 6789

take a look at my coding...why the ouput is rubbish huhuhu

I'll give you a hint. The processing you need would be no different if
you wanted to enter "abcde" and get
abcd e
abc de
....

This is string processing, not numeric processing.
 
A

aslamhenry

I'll give you a hint. The processing you need would be no different if
you wanted to enter "abcde" and get
abcd e
abc de
...

This is string processing, not numeric processing.

but the question ask to do in digit take a look at the question

Write a program in C that reads any five digit number and displays the
number in two parts diagonally as shown in the user interface screen
as shown below

Please key in any 5 digit number : 56789

5678 9
567 89
56 789
5 6789


The number displayed is separated into two parts beginning with the
rightmost unit digit.The process continues untill leftmost digit is
reached
 
W

Walter Roberson

but the question ask to do in digit take a look at the question
Write a program in C that reads any five digit number and displays the
number in two parts diagonally as shown in the user interface screen
as shown below

Well, if you *insist* (and I really *really* think you should reconsider):


Read the number as a number, to satisfy what you feel to be
criteria that you work with a "number". Now loop 5 times, each time
forming a new number from the original number divided by 10 to the
power of the loop index (i.e., divide by 10 when the index is 1, divide
by 100 when the index is 2, divide by 1000 when the index is 3, and so on.)

Write out the new number to a character buffer, using code similar to

sprintf(tmpbuffer, "%5.*f", loopindex, modifiednumber)

The * means that the number of digits after the decimal places is to
be taken from the value of the next argument in the list, which is the
loopindex in this case. So the first loop trip would use %5.1f
and store the string with one decimal place, the second loop trip
would use %5.2f and store the string with two decimal places, etc..

Now, still inside the loop, find the decimal place in the temporary
buffer and replace it with a space. Now print out the temporary buffer
and return for the next iteration of the loop.


But I really do recommend that you recognize that just because
the problem statement says that you must read a 5 digit number,
it doesn't mean that you have to work with the input as
a numeric value. They just throw that bit about "number" in,
in order to confuse people who haven't figured out the difference
between the -value- of a number and the way it is -represented-.
 
A

aslamhenry

Well, if you *insist* (and I really *really* think you should reconsider):

Read the number as a number, to satisfy what you feel to be
criteria that you work with a "number". Now loop 5 times, each time
forming a new number from the original number divided by 10 to the
power of the loop index (i.e., divide by 10 when the index is 1, divide
by 100 when the index is 2, divide by 1000 when the index is 3, and so on.)

Write out the new number to a character buffer, using code similar to

sprintf(tmpbuffer, "%5.*f", loopindex, modifiednumber)

The * means that the number of digits after the decimal places is to
be taken from the value of the next argument in the list, which is the
loopindex in this case. So the first loop trip would use %5.1f
and store the string with one decimal place, the second loop trip
would use %5.2f and store the string with two decimal places, etc..

Now, still inside the loop, find the decimal place in the temporary
buffer and replace it with a space. Now print out the temporary buffer
and return for the next iteration of the loop.

But I really do recommend that you recognize that just because
the problem statement says that you must read a 5 digit number,
it doesn't mean that you have to work with the input as
a numeric value. They just throw that bit about "number" in,
in order to confuse people who haven't figured out the difference
between the -value- of a number and the way it is -represented-.

thx for the hint...but can u give me some example..i mean a bit
only....the rest i do by myself
 
N

Nick Keighley

how to make these program huhu

what does "huhu" mean?

please any key in 5 digit : 56789

ouput

5678 9
567 89
56 789
5 6789

take a look at my coding...why the ouput is rubbish huhuhu

what is the output?

#include <stdio.h>

int main(void)
{
float num;

don't use float to hold an integer
int i, j, x ;

printf("please key in any 5 digit number:");

fflush(stdout) to ensure output appears
scanf("%f",&num);

scanf() is tricky to use correctly. Consider fgets()
followed by sscanf(). Check the return value of any
scanf() type call
for ( i=0 ; 5>i ; ++i){

loops 5 times. Why not use the usual idiom i < 5?
for(j=0 ; i>j ; ++j){
printf(" ") ;
}

outputs i spaces

for( x = j+1 ; 5 >= x ; ++x){
??



printf("%.0f", num);

what do you *think* this does?
}
putchar('\n');
}

return 0;
}

if you *insist* on reading an integer you could turn
it back into characters using sprintf(). To put put the "broken"
number. On the nth line

output first n characters
output a space
output rest of chars

or

for all characters
output char
if break point
output space
 
U

U

(e-mail address removed) wrote:

[
looking for solution to the following problem
|
| how to make these program huhu
|
| please any key in 5 digit : 56789
|
| ouput
|
| 5678 9
| 567 89
| 56 789
| 5 6789
|
]
but can u give me some example..i mean a bit
only....the rest i do by myself

Here you go...

/*** begin foo.c ***/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int new_number(long **ppnumber)
{
char foo[256];
size_t bar;
*ppnumber = NULL;
fprintf(stdout, "please any key in 5 digit : "); fflush(stdout);
if(!fgets(foo, 256, stdin)) { if(feof(stdin)) return 1; else return 2; }
bar = strlen(foo)-1; if(foo[bar] == '\n') bar[foo] = '\0'; else bar++;
if(bar != 5) return 3;
for(bar = 0; bar < 5; bar++) if(!isdigit(*(foo+bar))) return 4;
if((*ppnumber = malloc(sizeof * ppnumber)) == NULL) return 5;
**ppnumber = strtol(foo, (char **)NULL, 0x0A);
return 0;
}

void delete_number(long **ppnumber)
{
free(*ppnumber);
*ppnumber = NULL;
}

void fail(int y)
{
char *f;
switch(y) {
default: return;
case 1: f = "EOF"; break;
case 2: f = "Read error"; break;
case 3:
case 4: f = "Not a 5-digit number"; break;
case 5: f = "Out of memory"; break;
}
fprintf(stderr, "\n*** ERROR: %s!!!\n\n\n", f);
exit(EXIT_FAILURE);
}

signed main(void)
{
long *pnumber;
int jupiter[5], v3, v4;

if((v3 = new_number(&pnumber)) == 0)
{
fprintf(stdout, "\nouput\n\n");
for(v3 = 0; v3 < 5; v3++) {
jupiter[5-v3-1] = *pnumber % 10 - 1;
*pnumber /= 10;
}
for(v3 = 0; v3 < 4; v3++) {
for(v4 = 0; v4 < v3; v4++)
fputc(' ', stdout);
for(v4 = 0; v4 < 5; v4++) {
fputc('1' + jupiter[v4], stdout);
if(v4 == 3-v3)
fputc(' ', stdout);
}
fputc('\n', stdout);
}
delete_number(&pnumber);
} else {
fail(v3);
}
return EXIT_SUCCESS;
}

/*** end foo.c ***/
 
J

John J. Smith

U said:
(e-mail address removed) wrote:

[
looking for solution to the following problem
|
| how to make these program huhu
|
| please any key in 5 digit : 56789
|
| ouput
|
| 5678 9
| 567 89
| 56 789
| 5 6789
|
]
but can u give me some example..i mean a bit
only....the rest i do by myself

Here you go...

/*** begin foo.c ***/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int new_number(long **ppnumber)
{
char foo[256];
size_t bar;
*ppnumber = NULL;
fprintf(stdout, "please any key in 5 digit : "); fflush(stdout);
if(!fgets(foo, 256, stdin)) { if(feof(stdin)) return 1; else return 2; }
bar = strlen(foo)-1; if(foo[bar] == '\n') bar[foo] = '\0'; else bar++;
if(bar != 5) return 3;
for(bar = 0; bar < 5; bar++) if(!isdigit(*(foo+bar))) return 4;
if((*ppnumber = malloc(sizeof * ppnumber)) == NULL) return 5;
**ppnumber = strtol(foo, (char **)NULL, 0x0A);
return 0;
}

void delete_number(long **ppnumber)
{
free(*ppnumber);
*ppnumber = NULL;
}

void fail(int y)
{
char *f;
switch(y) {
default: return;
case 1: f = "EOF"; break;
case 2: f = "Read error"; break;
case 3:
case 4: f = "Not a 5-digit number"; break;
case 5: f = "Out of memory"; break;
}
fprintf(stderr, "\n*** ERROR: %s!!!\n\n\n", f);
exit(EXIT_FAILURE);
}

signed main(void)
{
long *pnumber;
int jupiter[5], v3, v4;

if((v3 = new_number(&pnumber)) == 0)
{
fprintf(stdout, "\nouput\n\n");
for(v3 = 0; v3 < 5; v3++) {
jupiter[5-v3-1] = *pnumber % 10 - 1;
*pnumber /= 10;
}
for(v3 = 0; v3 < 4; v3++) {
for(v4 = 0; v4 < v3; v4++)
fputc(' ', stdout);
for(v4 = 0; v4 < 5; v4++) {
fputc('1' + jupiter[v4], stdout);
if(v4 == 3-v3)
fputc(' ', stdout);
}
fputc('\n', stdout);
}
delete_number(&pnumber);
} else {
fail(v3);
}
return EXIT_SUCCESS;
}

/*** end foo.c ***/


Way too complicated. Also, the output does not match the template
in the OP.

OP wanted:

| please any key in 5 digit : 56789
|
| ouput
|
| 5678 9
| 567 89
| 56 789
| 5 6789

Your program:

| please any key in 5 digit : 56789
|
| ouput
|
| 5678 9
| 567 89
| 56 789
| 5 6789

Here's my take:

/* BEGIN bar.c */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


void main()
{
char number_buffer[4096];
long number;
printf("please any key in 5 digit : ");
fflush(stdin);
fflush(stdout);
gets(number_buffer);
if(strlen(number_buffer) != 5)
goto error;
for(number=0; number<5; number++)
if(number_buffer[number] < '0' || number_buffer[number] > '9')
goto error;
number=atol(number_buffer);
printf("\nouput\n\n"
"%04li %01li\n"
" %03li %02li\n"
" %02li %03li\n"
" %01li %04li\n",
number/10, number%10,
number/100, number%100,
number/1000, number%1000,
number/10000, number%10000);
return;

error:
printf("invalid input.\n");
return;
}
/* END bar.c */
 
U

user923005

U said:
(e-mail address removed) wrote:
[
looking for solution to the following problem
|
| how to make these program huhu
|
| please any key in 5 digit : 56789
|
| ouput
|
| 5678 9
| 567 89
| 56 789
| 5 6789
|
]
<snippage>
but can u give me some example..i mean a bit
only....the rest i do by myself
Here you go...
/*** begin foo.c ***/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int new_number(long **ppnumber)
{
char foo[256];
size_t bar;
*ppnumber = NULL;
fprintf(stdout, "please any key in 5 digit : "); fflush(stdout);
if(!fgets(foo, 256, stdin)) { if(feof(stdin)) return 1; else return 2; }
bar = strlen(foo)-1; if(foo[bar] == '\n') bar[foo] = '\0'; else bar++;
if(bar != 5) return 3;
for(bar = 0; bar < 5; bar++) if(!isdigit(*(foo+bar))) return 4;
if((*ppnumber = malloc(sizeof * ppnumber)) == NULL) return 5;
**ppnumber = strtol(foo, (char **)NULL, 0x0A);
return 0;
}
void delete_number(long **ppnumber)
{
free(*ppnumber);
*ppnumber = NULL;
}
void fail(int y)
{
char *f;
switch(y) {
default: return;
case 1: f = "EOF"; break;
case 2: f = "Read error"; break;
case 3:
case 4: f = "Not a 5-digit number"; break;
case 5: f = "Out of memory"; break;
}
fprintf(stderr, "\n*** ERROR: %s!!!\n\n\n", f);
exit(EXIT_FAILURE);
}
signed main(void)
{
long *pnumber;
int jupiter[5], v3, v4;
if((v3 = new_number(&pnumber)) == 0)
{
fprintf(stdout, "\nouput\n\n");
for(v3 = 0; v3 < 5; v3++) {
jupiter[5-v3-1] = *pnumber % 10 - 1;
*pnumber /= 10;
}
for(v3 = 0; v3 < 4; v3++) {
for(v4 = 0; v4 < v3; v4++)
fputc(' ', stdout);
for(v4 = 0; v4 < 5; v4++) {
fputc('1' + jupiter[v4], stdout);
if(v4 == 3-v3)
fputc(' ', stdout);
}
fputc('\n', stdout);
}
delete_number(&pnumber);
} else {
fail(v3);
}
return EXIT_SUCCESS;
}
/*** end foo.c ***/

Way too complicated. Also, the output does not match the template
in the OP.

OP wanted:

| please any key in 5 digit : 56789
|
| ouput
|
| 5678 9
| 567 89
| 56 789
| 5 6789

Your program:

| please any key in 5 digit : 56789
|
| ouput
|
| 5678 9
| 567 89
| 56 789
| 5 6789

Here's my take:

Please tell me that this is a gag post.
/* BEGIN bar.c */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main()

1.25b: What's the right declaration for main()?
Is void main() correct?

A: See questions 11.12a to 11.15. (But no, it's not correct.)
{
char number_buffer[4096];
long number;
printf("please any key in 5 digit : ");
fflush(stdin);

12.26: How can I flush pending input so that a user's typeahead isn't
read at the next prompt? Will fflush(stdin) work?

A: fflush() is defined only for output streams. Since its
definition of "flush" is to complete the writing of buffered
characters (not to discard them), discarding unread input
would
not be an analogous meaning for fflush on input streams.

There is no standard way to discard unread characters from a
stdio input stream, nor would such a way necessarily be
sufficient, since unread characters can also accumulate in
other, OS-level input buffers. You may be able to read and
discard characters until \n, or use the curses flushinp()
function, or use some system-specific technique. See also
questions 19.1 and 19.2.

References: ISO Sec. 7.9.5.2; H&S Sec. 15.2.
fflush(stdout);
gets(number_buffer);

12.23: Why does everyone say not to use gets()?

A: Unlike fgets(), gets() cannot be told the size of the buffer
it's to read into, so it cannot be prevented from overflowing
that buffer. As a general rule, always use fgets(). See
question 7.1 for a code fragment illustrating the replacement
of
gets() with fgets().

References: Rationale Sec. 4.9.7.2; H&S Sec. 15.7 p. 356.
if(strlen(number_buffer) != 5)
goto error;
for(number=0; number<5; number++)
if(number_buffer[number] < '0' || number_buffer[number] > '9')
goto error;
number=atol(number_buffer);
printf("\nouput\n\n"
"%04li %01li\n"
" %03li %02li\n"
" %02li %03li\n"
" %01li %04li\n",
number/10, number%10,
number/100, number%100,
number/1000, number%1000,
number/10000, number%10000);
return;

error:
printf("invalid input.\n");
return;}

/* END bar.c */
 
C

CBFalconer

John J. Smith said:
U said:
(e-mail address removed) wrote:

[
looking for solution to the following problem
|
| how to make these program huhu
|
| please any key in 5 digit : 56789

This joker is a troll. He keeps reposting this and ignoring
answers. So ignore him.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top