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



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

Answer / 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

More Engineering AllOther Interview Questions

#include<stdio.h> int fn(int v); main() { printf("%d\n",fn(7)); } int fn(int v) { if(v==1 || v==0) return 1; if(v%2==0) return fn(v/2)+2; else return fn(v-1)+3; }

1 Answers  


A rectangular beam section of 300 mm width and 500 mm effective depth is reinforced with 4 bars of 20 mm diameter, what shear reinforcement is required to resist 200 kN shear (use working stress method)?

1 Answers  


how we can improve mobile ad,hoc network.

1 Answers  


Including stoppage speed of a bus is 54km/hr while excluding stoppage of bus is 45km/hr.Find the time that the bus has been spread on stoppage?

5 Answers   IFFCO, Mu Sigma,


could u please tell me where the click view tool coaching in hyderabad

1 Answers  


#include<stdio.h> void main() { int x,y,z; x=1; y=1; z=1; z=++x||++y&&++z; printf("%d%d%d",x, y ,z); }

1 Answers  


Write a program in ā€˜C’ language that will perform the following operation on static stack. 1. push 2. pop 3. Display 4. Exit Create separate functions for each operation. The push() should accept one argument of structure type and will not return any thing. The pop() function will not receive any argument & return structure type value. The stack & top must be declare as external variable. Each element of stack will contain the following information Roll No, Std Name, Course. Use do-while loop & switch case for generating the above menu. The format of the output should is given below: S.No. Roll No. Student Name Course 1 cse01 Anil Singh B.Tech

1 Answers  


What is Landscape in Sap BW?

1 Answers  


Dear frds In my office we are using chiller . in that chiller inlet and outlet water diff. is 2.5 decgree always. if i intduce the cooling tower can i get good result in chiller Inlet water. reply plz.

1 Answers  


How to write a test case for an extra Commerce website can any one tell this with the help of a table

1 Answers   TresBizz,


If it requires 8 men to build a wall in 10 days then what time will it take to build by 4 men?

2 Answers  


Hi l, I want to install a water purifier (UV) at my home. the distance of water purifier from water tank [1000 lt ] is 7 ft. Is it sufficient for electrical water purifier? I want to know what is the water pressure from 1000 lt tank (the pipe used from water tank to machine is of inch 1-1/4") please note minimum water pressure is required for UV is - Water Pressure Min: 0.4 kg/sq. cm.Max: 2.0 kg/sq. cm. Please advise me on the following: 1) What is the water pressure generate in above condition. 2) What is the required height from the UV machine to water tank for the above condition? Thanks is advance.

1 Answers  


Categories
  • Civil Engineering Interview Questions Civil Engineering (5086)
  • Mechanical Engineering Interview Questions Mechanical Engineering (4453)
  • Electrical Engineering Interview Questions Electrical Engineering (16638)
  • Electronics Communications Interview Questions Electronics Communications (3918)
  • Chemical Engineering Interview Questions Chemical Engineering (1095)
  • Aeronautical Engineering Interview Questions Aeronautical Engineering (239)
  • Bio Engineering Interview Questions Bio Engineering (96)
  • Metallurgy Interview Questions Metallurgy (361)
  • Industrial Engineering Interview Questions Industrial Engineering (259)
  • Instrumentation Interview Questions Instrumentation (3014)
  • Automobile Engineering Interview Questions Automobile Engineering (332)
  • Mechatronics Engineering Interview Questions Mechatronics Engineering (97)
  • Marine Engineering Interview Questions Marine Engineering (124)
  • Power Plant Engineering Interview Questions Power Plant Engineering (172)
  • Textile Engineering Interview Questions Textile Engineering (575)
  • Production Engineering Interview Questions Production Engineering (25)
  • Satellite Systems Engineering Interview Questions Satellite Systems Engineering (106)
  • Engineering AllOther Interview Questions Engineering AllOther (1379)