473,396 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

HI can give suggesstion

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
8 1857
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
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
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
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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Leonardo | last post by:
I have the following associative array: $user=1; $user=1; $user=0; $user=1; $user=0; $user=1; $user=1;
10
by: techy techno | last post by:
Hiii Hello.. I just wanted to know if anyone can tell me how can I give my website visitors the feature of "FRIENDLY PRINTING" through IE. I would definitely like to give a feature like...
3
by: Marc Walgren | last post by:
Greetings I have an ASP application to enter reservations. There are multiple user security settings that require some users to have a restricted list of client in a drop list on a form. I...
1
by: Az Tech | last post by:
Hi people, (Sorry for the somewhat long post). I request some of the people on this group who have good experience using object-orientation in the field, to please give some good ideas for...
6
by: vb. | last post by:
Why we give a function data type? when we declare a function we gave a name and datatype for that function what for? if i make a parameter i declare it and give it a datatype and if i use variabels...
11
by: Wayne | last post by:
I am a one man enterprise and have been asked by a prospective client what happens to their database regarding ongoing changes etc if I get hit by a bus. Obviously my databases are distributed...
25
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does parseInt('09') give an error? ----------------------------------------------------------------------- ...
0
by: aslamhenry | last post by:
aslamhe...@yahoo.com View profile More options Sep 18, 9:09 pm Newsgroups: comp.lang.c++ From: aslamhe...@yahoo.com Date: Tue, 18 Sep 2007 06:09:14 -0700 Local: Tues, Sep 18 2007 9:09 pm...
3
by: Charlotte | last post by:
Hello, info: I'me a rookie with IIS I have on a WinXP Pro the IIS installed, so I can test some pages before uploading to the hostserver online on the hostserver is a possibility (with the...
2
dlite922
by: dlite922 | last post by:
This is a totally n00b question but I'm saying good bye to Windows slowly and I've come across where I need to own a directory but I want to give write permission to Smarty (Apache user). Right now I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
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,...

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.