adspace


reverse a number ( like 1234 into 4321) using malloc in C programming

Answer Posted / Arpana Bajpai

In C, you can reverse a number by allocating memory for the reversed number and filling it from right to left. Here's an example:n```n#include <stdio.h>n#include <stdlib.h>nnint reverse(int num, int *rev_num, int digits)n{n int i;n for (i = 0; i < digits; i++)n {n rev_num[i] = num % 10;n num /= 10;n if (num == 0 && i < digits - 1) return -1;n }n rev_num[i] = num;n return 0;n}nnint main()n{n int n = 1234;n int *rev_n = (int *)malloc(sizeof(int) * sizeof(int) * 4);n if (!rev_n)n {n printf Mistallocated memoryn return -1;n }n if (reverse(n, rev_n, 4))n {n printf Mistake in reversing the numbern free(rev_n);n return -1;n }n int i;n for (i = 0; i < 4; i++)n {n printf %d,n rev_n[3 - i];n }n free(rev_n);n return 0;n}n```

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is Campus selection process of patni at Adcet,Ashta at 24th dec 2010.Also give apti syllabus.

2508


what are the main points to be included in our introduction when asked in hr...??

2045


Please explain why you are suitable for the position that you have applied for?

2121


can you provide me uniken pvt ltd. technical interview question and answer?

3232


Why do you want to work here

1835


I am going to appear for ISRO written test on 26 th april 2009 .So kindly forward me sample previous question paper for ISRO for BE Computer Science . My mail id : jothychristiya@gmail.com

2675


Hi All... PLease send me if anyone have "Information Security Officer" Aptitude papers. Please Mail me at mannusanghi@gmail.com

1935


sir, plz send me the question regarding DMS gr1 questions of previous year as my exam is on 27/09/08...thanking u in advance...

2298


plz snd me sbi probationofficers papers to my id archpa@gmail.com

2278


what is total power generation capacity in india at present (as on 12-12-2011)

2192


Hi i'm richa piplani applied for ATC(IT) if u have the syllabus & old question papers forward me.I will be very much thankful to you. piplani.richa@gmail.com and sonu_pips@yahoo.co.in

2088


pls send me the e-book for verbal and non verbal reasoning of R S aggarwall

2313


please provide me the type of questions or question pattern of bally.

1967


consiteunts of indian railways

2413


plz send me interview questions & answers of Data Structure

2117