#include <stdio.h>
int main()
{
int remain = 0, reverse = 0;
int num, savenum, count;
printf("Enter a integer: ");
scanf("%d", &num);
savenum = num;
count = 0;
do
{
remain = num % 10;
reverse = reverse * 10 + remain;
num /= 10;
printf ("Digit (%d): %d\n", ++count, remain);
}
while(num != 0);
printf("Digit (%d) :\t%d\n",savenum, reverse);
return 0;
}
Notes: the above solution do not require much additional space, to implement the following quesiton, additional space is necessary, to store each element. And to make it in order, we should use the stack-- last in fist out: -- use a linked list-- do not have to compute the size of the Number that requried, and insert each on at the head
Similar Question:
Write a function that takes an integer and prints out the digits separated by commas. Example, pass in 345 print out 3,4,5
Void parseDigits(int Number)
{
string format = ""';
while(num > 0)
{
quotient = num%10;
if(num > 9)
format = format + quotient.ToString() + ",";
else
format = format + quotient.ToString();
num = num/10;
}
Format.Reverse(); //should give desired number with digits seperated with comma;
}
# include <string>
# include <stdlib.h>
# include <iostream>
struct node{
int data;
node * next;
};
struct node* parseIntDigt(int Num)
{
head = (struct node) malloc(sizeof(struct node)); // head = new node;
head--> data = Num%10;
head-->next = NULL;
Num/=10;
while(Num>0)
{
newNode = (Struct node) malloc(sizeof(Struct node)); // head = new node;
newNode--> data = Num%10;
newNode--> next = head;
head = newNode;
Num/=10;
}
return head;
}
int main()
{
int Num;
node * listhead;
printf("Enter your digits: ");
scanf("%d", &Num);
listhead = parseIntDigt(Num);
while(listhead!=NULL)
{
printf("%d", listhead-->data);
listhead = listhead-->next;
}
}
Note ::
---------------------
In C -> use <stdlib.h> <stdio>
---------
In C++-> use <cstdlib> <cstdio>
---------
Note ::
For the dynamic memory allocation we use New and malloc. Both are used to allocate memory But the major difference between New and Malloc are. 1. New is a operator where as Malloc is a function. 2.In case of Malloc we need to specify the size of memory for our data types. But in case of New we do not need to specify the size. ex. int *a; a=(int*)malloc(sizeof(int));
int *a; a=new int;
one more difference is malloc return void* by default ,we need to typecast pointer returned from malloc but in case of new there is no need to typecast ex: int *a; a=(int*)malloc(sizeof(int)); a= new int;
Note: http://stackoverflow.com/questions/184537/in-what-cases-do-i-use-malloc-vs-new
Unless you are forced to use C, you should never use malloc. Always use new. If you need a big chunk of data just do something like:
char *pBuffer = new char[1024];
Be careful though this is not correct:
//This is incorrect - may delete only one element, may corrupt the heap, or worse...
delete pBuffer;
Instead you should do this when deleting an array of data:
//This deletes all items in the array
delete[] pBuffer;
The new keyword is the C++ way of doing it, and it will ensure that your type will have their constructor called. The new keyword is also more type safe whereas malloc is not typesafe at all.
The only way I could think that would be beneficial to use malloc would be if you needed to change the size of your buffer of data. The new keyword does not have an analogous way like realloc. The realloc function might be able to extend the size of a chunk of memory for you more efficiently.
It is worth mentioning that you cannot mix new/free and malloc/delete.
Note, some answers in this question are invalid.
int* p_scalar = new int(5);//Does not create 5 elements, but initializes to 5
int* p_array = new int[5];//Creates 5 elements
In computing,
malloc
is a subroutine for performing dynamic memory allocation in the C and C++ programming languages, though its use in C++ has been largely superseded by operators new
and new[]
. malloc
is part of the standard library for both languages and is declared in the stdlib.h
header although it is also declared within the std
namespace via the C++'s cstdlib
header.http://www.glassdoor.com/Interview/Write-a-function-that-takes-an-integer-and-prints-out-the-digits-separated-by-commas-Example-pass-in-345-print-out-3-4-5-QTN_128794.htm
No comments:
Post a Comment