Have a Read!

Hi Everyone.
The idea for this page is to provide new c users simple c tutorials to learn how to develop the logic and more importantly correct way to write syntax.
ENJOY !!!

Wednesday, January 26, 2011

C PROGRAM TO REVERSE 5 DIGIT NUMBER

/*this program will reverse the five digit number*/
#include<stdio.h>
int main()
{
int no,b,c,d,e,rev; /*no is the number entered by user rev is the reverse number which will be displayed at the end*/
printf("enter a five digit no.\n");
scanf("%d",&no);
if(no<=99999 && no>=10000)        /*this if statement is to make sure that user enters a five digit number
                                    otherwise the program will display "you have not entered a five digit number"*/
{
  b=no/10;      /*int  is used so number after decimal will be neglected*/
  c=b/10;
  d=c/10;
  e=d/10;
  rev=(no-10*b)*10000;      /*this formula will calculate unit digit of number entered by user and multiply by 10000
                              in this way it will become first digit of reverse number*/  
  rev=rev+((b-10*c)*1000); /*this formula will calculate the tens digit of number entered by user and multiply by 1000
                             the resultant will be added to previous calculated no. i.e rev so first two digits are
                             reversed.in the similar way all digits will be reversed*/
  rev=rev+((c-10*d)*100);
  rev=rev+((d-10*e)*10);
  rev=rev+e;
  printf("%d\n",rev);
}
else          /*this else is with the first if statement*/
printf("you have not entered a five digit no.\n");
return 0;
}

3 comments:

  1. #include
    int main(){
    int num,d1,d2,d3,d4,d5,renum,sum;
    printf("Enter a five digit number:");
    scanf("%d",&num);
    d1=num/10000;
    d2=(num%10000)/1000;
    d3=(num%1000)/100;
    d4=(num%100)/10;
    d5=num%10;
    renum=d5*10000+d4*1000+d3*100+d2*10+d1;
    sum=d1+d2+d3+d4+d5;
    printf("Reverse number:%d\n",renum);
    printf("Sum:%d",sum);
    getch();
    }

    ReplyDelete

Related Posts with Thumbnails