I am new to c language, I got a question: how to prints the reversal of a two-digit number without using arithmetic to split the number into digits. could anyone give me a hint? thanks
Printable View
I am new to c language, I got a question: how to prints the reversal of a two-digit number without using arithmetic to split the number into digits. could anyone give me a hint? thanks
don't store the number as a number, use a string or a character array (same thing) and use the subscript operator to output it
string thingey = "23";
cout << thingey[1] << thingey[0];
The best way i know is by using a string for storing your number:
(this is c++ not c)
int string[20];
cin.get(string,20);
cout<<strrev(string);
there is another way you can use:
(this is in c)
#include<stdio.h>
#include<conio.h>
void main(){
int num,digit=0;
clrscr();
while(1){
printf("\nenter a number:\n");
scanf("%d",&num);
printf("\nInverse=");
do{
digit=num%10;
printf("%d",digit);
num/=10;
}while(num!=0);
}
}
The above was compiled and debuged.it works fine but you'll never see a pro coder
use such thing in his work.
i recommend reading your number from a file,storing it in a string,reversing it
using the strrev function.
note:if you want to turn your string to an int use the following command:
#include <stdlib.h>
int atoi(const char *s);
example
----------------------------------------------------------------------------------------------
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int n;
char *str = "12345.67";
n = atoi(str);
printf("string = %s integer = %d\n", str, n);
return 0;
}
------------------------------------------------------------------------------------------------------------------------
As far as the laws of mathematics refer to reality, they are not certain, and as far as they are certain, they do not refer to reality.-- Albert Einstein
This is a cheeky one..mind u its just for fun ...............
use gotoxy and print the number twice consecutevly........ 7272 now overwrite the first and last digit by a space......and what u see is a magic!!!!!!!!!!
No Strings Attached :)
most general methods u have seen and there are plenty of methods if u involve methematics
Haha, lol coolnads :)
That's cheezy indeed :)
thanks you all.
but as you mentioned by using "String" way. how could I let user input a two-digit nubmer and then reverse it?
how do i read a string, such as "56", seperately? I mean, let a integer x = 5, and another int y = 6. thanks again
All I mean is using c language. I tried many times, mession failed!! (Mession Impossible 3!! ^&^)
I found this by going to google and inputing "c/c++ reversing numbers"
this was the first non advertised link! youd be much better off learning to search before you learn to program!
****************************
/* array2.c -- Read/writing/reversing integer arrays
*/
#include <stdio.h>
#define NMAX 10
void intSwap(int *x, int *y);
int getIntArray(int a[], int nmax, int sentinel);
void printIntArray(int a[], int n);
void reverseIntArray(int a[], int n);
int main(void) {
int x[NMAX];
int hmny;
hmny = getIntArray(x, NMAX, 0);
printf("The array was: \n");
printIntArray(x,hmny);
reverseIntArray(x,hmny);
printf("after reverse it is:\n");
printIntArray(x,hmny);
}
void intSwap(int *x, int *y)
/* It swaps the content of x and y */
{
int temp = *x;
*x = *y;
*y = temp;
}
void printIntArray(int a[], int n)
/* n is the number of elements in the array a.
* These values are printed out, five per line. */
{
int i;
for (i=0; i<n; ){
printf("\t%d ", a[i++]);
if (i%5==0)
printf("\n");
}
printf("\n");
}
int getIntArray(int a[], int nmax, int sentinel)
/* It reads up to nmax integers and stores then in a; sentinel
* terminates input. */
{
int n = 0;
int temp;
do {
printf("Enter integer [%d to terminate] : ", sentinel);
scanf("%d", &temp);
if (temp==sentinel) break;
if (n==nmax)
printf("array is full\n");
else
a[n++] = temp;
}while (1);
return n;
}
void reverseIntArray(int a[], int n)
/* It reverse the order of the first n elements of a */
{
int i;
for(i=0;i<n/2;i++){
intSwap(&a[i],&a[n-i-1]);
}
}
********************************
i still like coolnads better!
thanks, I went to google and try to find what I need, but I didn't. that's all not what I want.
I want let a user input a string from keyboard, such as "56". then I need to assign a integer x = 5, and another Integer y = 6!! How could I do that? thanks
I got some idea. what do you think?
you will soon learn what buffer overflow means as soon as the user inputs somthing like
44444444444444444444 or aaaaaaaaaaaaaaaaaaa !
you have got to read some books brah..
------------------------------------------------------------------------------------------------------------------------
Never judge a hacker by the color of his hat
DS:5395 2816 3933 319 8968 2208 6325 9399 3663 1821 4916 2139 9300 3748 5119 1140
badapple :
The logical approach to this would have the user input a number(string), after the string has been stored, get the lenght of the string and stroe it. Now, create a function that take the number of characters (or digits in this case), and creates a string (or char) for every character(digit). So now we've got a bunch of characters that need to be converted to 'int'. Honestly, I totally forget the API function that converts the string to int (something like atoi_64(*string). but anyways once converted into "int". You can change or jirate them any way you please. To find that function, refer to "msnd.microsoft.com" .
sandman