You must Sign In to post a response.
  • C program to reverse the given number using function.


    Preparing for MSBTE diploma in Computer engineering exams? Have a query about preparing for C programming subject? check out this page and get program solution for reversing a given number using function.

    Following is a question that was asked in the question paper of Diploma in Computer Engineering (Summer 2019)conducted by MSBTE Mumbai, for subject Programming in C(22226).

    This question is asked for 4 marks and hence requires some explanation along with the program code. Please let me know the solution.

    Q) Write a C program to reverse the number using function. (e.g. if the given number is 1234 then converted number is 4321) [4 Marks]
  • Answers

    2 Answers found.
  • Following is the program to reverse the given number using function. Please include stdio.h and conio.h header files before declaration of main function.

    //PROGRAM TO REVERSE GIVEN NUMBER USING FUNCTION
    #include
    #include
    void main()
    {
    long int revnum(long int);
    long int num,res;
    clrscr();
    printf("Enter the number\n");
    scanf("%ld",&num);
    res=revnum(num);
    printf("\n%ld is original number an %ld is reverse of the given number",num,res);\
    getch();
    }
    long int revnum(long int n)
    {
    long int m=n,s=0,i,c=0;
    while(m>0)
    {
    c++;
    m=m/10;
    }
    m=n;
    while(m>0)
    {
    if(c==1)
    {
    s=s+(m%10);
    m=m/10;
    c=c-1;
    }
    else
    {
    s=(s+(m%10))*10;
    m=m/10;
    c=c-1;
    }
    }
    return(s);
    }

  • Here is my code:

    #include

    int getReverse(int numbers)
    {
    int rev = 0, rem;
    while(numbers!=0)
    {
    rem = numbers % 10;
    numbers = numbers / 10;
    rev = rev * 10 + rem;
    }
    return rev;
    }

    int main(void) {
    // your code goes here
    int num, revOfNum;
    scanf("%d", &num);
    revOfNum = getReverse(num);
    printf("%d", revOfNum);
    return 0;
    }


  • Sign In to post your comments