Write a c program to reverse a given number



#include<stdio.h>
int main(){
    int num,r,reverse=0;

    printf("Enter any number: ");
    scanf("%d",&num);

    while(num){
         r=num%10;
         reverse=reverse*10+r;
         num=num/10;
    }

    printf("Reversed of number: %d",reverse);
    return 0;
}

Sample output:
Enter any number: 12
Reversed of number: 21

Code 2:
Reverse very large or big numbers beyond the rang

Logic is we accept the number as string

#include<stdio.h>
#define MAX 1000

int main(){

    char num[MAX];
    int i=0,j,flag=0;

    printf("Enter any positive integer: ");
    scanf("%s",num);

    while(num[i]){
         if(num[i] < 48 || num[i] > 57){
             printf("Invalid integer number");
             return 0;
         }
         i++;
    }

    printf("Reverse: ");
    for(j=i-1;j>=0;j--)
         if(flag==0 &&  num[j] ==48){
         }
         else{
             printf("%c",num[j]);
             flag =1;
         }

    return 0;

Sample output:

Enter any positive integer: 234561000045645679001237800000000000
Reverse: 8732100976546540000165432

Code 3:
C program to reverse a number using for loop


#include<stdio.h>
int main(){
    int num,r,reverse=0;

    printf("Enter any number: ");
    scanf("%d",&num);

    for(;num!=0;num=num/10){
         r=num%10;
         reverse=reverse*10+r;
    }

    printf("Reversed of number: %d",reverse);
    return 0;
}

Sample output:
Enter any number: 123
Reversed of number: 321

Code 4:
1. C program to reverse a number using recursion

#include<stdio.h>
int main(){
    int num,reverse;

    printf("Enter any number: ");
    scanf("%d",&num);

    reverse=rev(num);
    printf("Reverse of number: %d",reverse);
    return 0;
}

int rev(int num){
    static sum,r;

    if(num){
         r=num%10;
         sum=sum*10+r;
         rev(num/10);
    }
    else
         return 0;

    return sum;
}

Sample output:
Enter any number: 456
Reverse of number: 654
Share on Google Plus

About Unknown

Hello, My name is Mou, I am from Dhaka, Bangladesh and who is trying keep her hand at blogging. I enjoy writing, anything from how to- pieces to endure to just anything at all. My blog will go into detail about all of article. Long time I work with adsense and blogging about 10 years. Doing some SEO work and like that. Please stay close for track down your article for your task and assignment. Feel free to contact with me at moujuena.mou@gmail.com if need. Thanks for visiting.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment