By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,187 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

reverse

P: 4
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
Share this Question
Share on Google+
2 Replies


P: 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

P: 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

Post your reply

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