Discuss..thx

A

aslamhenry

please key in any 5 digits number : 56789

and the ouput is
5678 9
567 89
56 789
5 6789


how to write those program......


my idea is like this...
#include <stdio.h>


void main()
{
int num;
float num1,num2,num3,num4;
float r1,r2,r3,r4;


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


num1=num/10;
num2=num/100;
num3=num/1000;
num4=num/10000 ;


r1=num%10;
r2=num%100 ;
r3=num%1000 ;
r4=num%10000 ;


printf("\n %.0f %.0f",num1,r1);
printf(" \n %.0f %.0f",num2,r2);
printf(" \n %.0f %.0f",num3,r3);
printf(" \n %.0f %.0f",num4,r4);


}


___________________________________________________________________________­
___

the problem occur when i entered nnumber 56789.It output become
rubbish..
but if i key in 12345 the program excute nicely.....


anyone can tell what wrong with my coding...


and i really appriciate if someone can make it more efficient....
 
V

Victor Bazarov

please key in any 5 digits number : 56789

and the ouput is
5678 9
567 89
56 789
5 6789


how to write those program......


my idea is like this...
#include <stdio.h>


void main()
{
int num;
float num1,num2,num3,num4;
float r1,r2,r3,r4;

Change 'float' to 'int' and the formats to %i.
printf("please key in any 5 digit number:");
scanf("%d",&num);


num1=num/10;
num2=num/100;
num3=num/1000;
num4=num/10000 ;


r1=num%10;
r2=num%100 ;
r3=num%1000 ;
r4=num%10000 ;


printf("\n %.0f %.0f",num1,r1);
printf(" \n %.0f %.0f",num2,r2);
printf(" \n %.0f %.0f",num3,r3);
printf(" \n %.0f %.0f",num4,r4);


}


___________________________________________________________________________­
___

the problem occur when i entered nnumber 56789.It output become
rubbish..

Could it be that your 'int' has only 16 bits? Then the largest number
it is going to handle is 32767.
but if i key in 12345 the program excute nicely.....


anyone can tell what wrong with my coding...


and i really appriciate if someone can make it more efficient....

Efficient? Your code is efficient enough. It's not generic enough,
so it can't do the same output for any number (two-digit to fifty-digit
ones, for example), but that's not the goal, right?

V
 
P

Phlip

aslamhenry said:
void main()

'void main' makes Comedy Central into the only real journalism on the air.
Please always use 'int main'.
printf("\n %.0f %.0f",num1,r1);
printf(" \n %.0f %.0f",num2,r2);
printf(" \n %.0f %.0f",num3,r3);
printf(" \n %.0f %.0f",num4,r4);

Your professor is asking you to learn loop statements. num1 should be
num[0], hence num[index].

Then, after you add a 'for' loops, learn to use this: printf(" - %*s - ",
42, ""). The * will substitute your index in, and provide those spaces. The
rest is math.

Next, printf() is generally only useful in C. This group discusses C++,
which modern students should generally learn first before C. So if you
signed up for C++, but your professor is teaching C first, you should start
learning C++ on your own. The book /Accellerated C++/ is about using C++ as
a simple starter language.
 

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

Similar Threads

sob..Someone can help me????plsss 40
Check this Out!!! 2
HI can give suggesstion 11
problem in Loop 4
hi can give me idea 11
Can't figure out what the error is here 4
correction 28
HI can give suggesstion 0

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top