C++: #CoolCodez

Situation: User types in a number and the program gives the number reversed.

#include<iostream.h>
#include<conio.h>
void main()
{ 
    clrscr();
    int num, rev=0, rem;
   
    cout<<"\n\t Enter a number: ";
    cin>>num;
    
    while (num>0)
    {
          rem=num%10;
          rev=rev*10+rem;
          num=num/10;
    }

    cout<<rev;
    getch();

}

 

I had initially written this code for the same situation, but I realized that there was a quicker way to do this. I am still going to put the longer code below just so you can see how crazy programming can be: you can be super concise and succinct or you can take the longest route possible, either which ways you get the same program.

#include<iostream.h>
#include<conio.h>
void main()
{ 
   clrscr();
   int num, a=1, b, count=0, num1=0, ans=0, rem=0, dig1, count1, c;
   
   cout<<"\n\t Enter a number: ";
   cin>>num;
 
   while (num>0);
   {
           num=num/10;
           count++;
   }

   rem=num1%10;
   dig1=rem;
   count1=count;
   
   while (count>1)
   {
          rem=rem*10;
          count--;
   }
  
   while (count1>1)
   {
         a=a*10;
         count1--;
   }
   
   b=num1%a;
   c=num%a;
  
   ans=((rem+b)-dig1)+c;
  
   cout<<ans;
   getch();
  
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s