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
What is Campus selection process of patni at Adcet,Ashta at 24th dec 2010.Also give apti syllabus.
what are the main points to be included in our introduction when asked in hr...??
Please explain why you are suitable for the position that you have applied for?
can you provide me uniken pvt ltd. technical interview question and answer?
Why do you want to work here
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
Hi All... PLease send me if anyone have "Information Security Officer" Aptitude papers. Please Mail me at mannusanghi@gmail.com
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...
plz snd me sbi probationofficers papers to my id archpa@gmail.com
what is total power generation capacity in india at present (as on 12-12-2011)
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
pls send me the e-book for verbal and non verbal reasoning of R S aggarwall
please provide me the type of questions or question pattern of bally.
consiteunts of indian railways
plz send me interview questions & answers of Data Structure