reverse

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shahrukh
    New Member
    • Sep 2006
    • 4

    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
  • ltgbau
    New Member
    • Sep 2006
    • 41

    #2
    Code:
    	// in this code i treat the input number as a string
    	// and i assume that the input is valid (it means the input is a number
    	// and it doesn't contant any non-numeric character...
    	cout<<"input a number:";
    	char strNum[10]="";
    	cin>>strNum;
    	cout<<endl;
    	// calculate sum and mul of all figures to form the input number
    	int sum=0;
    	int mul=1;
    	int len=(int)strlen(strNum);
    	if(len<=0)
    		return;
    	char strTemp[2]="";
    	int val;
    	for(int i=0; i<len; i++)
    	{
    		strTemp[0]=strNum[i];
    		val=atoi(strTemp);
    		sum+=val;
    		mul*=val;
    	}
    	cout<<"Sum="<<sum<<endl<<"Mul="<<mul<<endl;	// done
    	// print reversed number
    	strrev(strNum);
    	cout<<"Reversed number="<<strNum;

    Comment

    • koder
      New Member
      • Sep 2006
      • 23

      #3
      Hi,posting the necessery kode,for what u askd,try out the string reversal by your self,if u find difficulties,pl ease do not hesitate to ask

      Code:
      #include<stdio.h>
      #include<string.h>
      
      
      
      int get_sum(int);
      int get_prod(int);
      int get_rev(int);
      
      main()
       {
         int pnum=0,rev_num,i,prod,sum;
         int  ch=0;
         printf("\n enter the number");
         scanf("%d",&pnum);
      
          while(1)
           {
              printf("\n enter the operation ");
              printf("\n 1:reverse \t 2:sum \t 3:multiplication");
              scanf("%d",&ch);
              printf("\n u choosed the  option :%d",ch);
      
      int get_rev(int pnum)
        {
         int kum=0,rev=0,pum=pnum;
         while(pnum!=0)
          {
         kum=pnum%10;
         rev=rev*10+kum;
         pnum=pnum/10;
         }
        return rev;
       }
      
      int get_prod(int pnum)
        {
         int kum=0,prod=1,pum=pnum;
         while(pnum!=0)
          {
         kum=pnum%10;
         prod=prod*kum;
         pnum=pnum/10;
         }
      
         return prod;
       }
      int get_sum(int pnum)
        {
         int kum=0,sum=0,pum=pnum;
         while(pnum!=0)
          {
         kum=pnum%10;
         sum=sum+kum;
         pnum=pnum/10;
         }
        return sum;
       }
      
         sum=sum+kum;
         pnum=pnum/10;
         }
        return sum;
       }
      compiled and tested with gcc.

      Comment

      Working...