Positional notation of numbers
#include <>
#include <>
#define PACKAGE "digits"
int number_of_digits(int x);
void print_help(void);
int main(int argc, char *argv[]) {
int n; /* input; the number to convert */
short base; /* input; base to which we will convert n */
short rhdigit; /* right-hand digit of n-prime */
int power; /* loop */
if(argc != 3) {
print_help();
return 1;
} else {
sscanf(argv[1], "%d", &n);
if(n < 1) {
fprintf(stderr, "%s: Error: Number must be greater than 0.\n", PACKAGE);
return 1;
}
sscanf(argv[2], "%hi", &base);
if(base <> 10) {
fprintf(stderr, "%s: Error: Base is not in required range.", PACKAGE);
return 1 ;
}
} /* else */
printf("The number %d, is %d wide, ", n, number_of_digits(n));
printf("and contains the numbers: ");
/*
// Generate digits of converted number, right to left.
*/
for(power = 0; n != 0; power++) {
rhdigit = n % base; /* Isolate right-hand digit of n. */
n /= base; /* then eliminate right-hand digit. */
printf("%hi ", rhdigit);
}
printf("\n");
return 0;
}
int number_of_digits(int x) {
return x ? (int)(log10((double)(abs(x))))+1 : 1;
}
void print_help(void) {
fprintf(stdout, "Usage : %s [NUMBER] [BASE]\n", PACKAGE);
fprintf(stdout, "Example: %s 4591 10\n", PACKAGE);
}
Labels
- ajax (1)
- Assembly Programming (3)
- Books on C++ (6)
- C Programs (35)
- Challenging Question (1)
- Class (1)
- Complete Script (1)
- Computer Graphics (1)
- CSS (2)
- Datastructure (6)
- Download (3)
- e-books (8)
- Errors (1)
- FTP (2)
- HTML (3)
- Interview Questions (8)
- Introduction (1)
- Methods (2)
- OOP (1)
- Operator Overloading (1)
- Operators (1)
- Others (5)
- PHP (4)
- PHP Errors (1)
- PHP Script (1)
- Programming (3)
- Question Bank (6)
- Short Questions (1)
- SQL (1)
- Tips (1)
- Tricks (1)
- Tricky Programs in C (2)
- Useful Scripts (1)
- WAP (1)
- Web (3)
- XHTML (1)
Positional notation of numbers in c
Wednesday, August 25, 2010Posted by Xofth at 10:04 AM
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment