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

HI can give suggesstion

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

Sep 18 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
In article <11**********************@22g2000hsm.googlegroups. com>,
<as********@yahoo.comwrote:
>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
>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.)
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell
Sep 18 '07 #2

P: n/a
as********@yahoo.com wrote:
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.
Sep 18 '07 #3

P: n/a
On Sep 18, 11:07 pm, Mark Bluemel <mark_blue...@pobox.comwrote:
aslamhe...@yahoo.com wrote:
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.
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

Sep 18 '07 #4

P: n/a
On 18 Sep, 15:59, aslamhe...@yahoo.com wrote:
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

--
Nick Keighley

Sep 19 '07 #5

P: n/a
Nick Keighley wrote:
aslamhe...@yahoo.com wrote:
.... snip ...
>
> for ( i=0 ; 5>i ; ++i){

loops 5 times. Why not use the usual idiom i < 5?
If you study it closely, he did :-)

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Sep 20 '07 #6

P: n/a
U
as********@yahoo.com 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 ***/
--
U

Sep 20 '07 #7

P: n/a
U wrote:
as********@yahoo.com 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:

/* 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 */

--
John J. Smith
Homework Expert
Sep 24 '07 #8

P: n/a
"John J. Smith" wrote:
U wrote:
> as********@yahoo.com 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.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Sep 25 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.