473,396 Members | 2,038 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

reverse

i have to input a number and print the reverse of the number. and also the sum of all the numbers which is given as input i.e. the input is 123. the sum is 6 and multiplication of the numbers i.e. 123. the multiplication is 6.

similarly i have to input a text and print the reverse of it i.e. abc. the output should be cba. plz if anyone have their solution then reply with their logics
Oct 4 '06 #1
2 3263
ltgbau
41
Expand|Select|Wrap|Line Numbers
  1.     // in this code i treat the input number as a string
  2.     // and i assume that the input is valid (it means the input is a number
  3.     // and it doesn't contant any non-numeric character...
  4.     cout<<"input a number:";
  5.     char strNum[10]="";
  6.     cin>>strNum;
  7.     cout<<endl;
  8.     // calculate sum and mul of all figures to form the input number
  9.     int sum=0;
  10.     int mul=1;
  11.     int len=(int)strlen(strNum);
  12.     if(len<=0)
  13.         return;
  14.     char strTemp[2]="";
  15.     int val;
  16.     for(int i=0; i<len; i++)
  17.     {
  18.         strTemp[0]=strNum[i];
  19.         val=atoi(strTemp);
  20.         sum+=val;
  21.         mul*=val;
  22.     }
  23.     cout<<"Sum="<<sum<<endl<<"Mul="<<mul<<endl;    // done
  24.     // print reversed number
  25.     strrev(strNum);
  26.     cout<<"Reversed number="<<strNum;
  27.  
Oct 5 '06 #2
koder
23
Hi,posting the necessery kode,for what u askd,try out the string reversal by your self,if u find difficulties,please do not hesitate to ask

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4.  
  5.  
  6. int get_sum(int);
  7. int get_prod(int);
  8. int get_rev(int);
  9.  
  10. main()
  11.  {
  12.    int pnum=0,rev_num,i,prod,sum;
  13.    int  ch=0;
  14.    printf("\n enter the number");
  15.    scanf("%d",&pnum);
  16.  
  17.     while(1)
  18.      {
  19.         printf("\n enter the operation ");
  20.         printf("\n 1:reverse \t 2:sum \t 3:multiplication");
  21.         scanf("%d",&ch);
  22.         printf("\n u choosed the  option :%d",ch);
  23.  
  24. int get_rev(int pnum)
  25.   {
  26.    int kum=0,rev=0,pum=pnum;
  27.    while(pnum!=0)
  28.     {
  29.    kum=pnum%10;
  30.    rev=rev*10+kum;
  31.    pnum=pnum/10;
  32.    }
  33.   return rev;
  34.  }
  35.  
  36. int get_prod(int pnum)
  37.   {
  38.    int kum=0,prod=1,pum=pnum;
  39.    while(pnum!=0)
  40.     {
  41.    kum=pnum%10;
  42.    prod=prod*kum;
  43.    pnum=pnum/10;
  44.    }
  45.  
  46.    return prod;
  47.  }
  48. int get_sum(int pnum)
  49.   {
  50.    int kum=0,sum=0,pum=pnum;
  51.    while(pnum!=0)
  52.     {
  53.    kum=pnum%10;
  54.    sum=sum+kum;
  55.    pnum=pnum/10;
  56.    }
  57.   return sum;
  58.  }
  59.  
  60.    sum=sum+kum;
  61.    pnum=pnum/10;
  62.    }
  63.   return sum;
  64.  }
compiled and tested with gcc.
Oct 5 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

35
by: Raymond Hettinger | last post by:
Here is a discussion draft of a potential PEP. The ideas grew out of the discussion on pep-284. Comments are invited. Dart throwing is optional. Raymond Hettinger ...
59
by: Raymond Hettinger | last post by:
Please comment on the new PEP for reverse iteration methods. Basically, the idea looks like this: for i in xrange(10).iter_backwards(): # 9,8,7,6,5,4,3,2,1,0 <do something with i> The...
14
by: ford_desperado | last post by:
Why isn't ALLOW REVERSE SCANS the default? Why do we have to - drop PK - create an index - recreate PK What are the advantages of indexes that do not allow reverse scans?
15
by: Fady Anwar | last post by:
Hi while browsing the net i noticed that there is sites publishing some software that claim that it can decompile .net applications i didn't bleave it in fact but after trying it i was surprised...
41
by: rick | last post by:
Why can't Python have a reverse() function/method like Ruby? Python: x = 'a_string' # Reverse the string print x Ruby: x = 'a_string' # Reverse the string
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.