have to identify odd num and print them

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ravali
    New Member
    • Apr 2013
    • 1

    have to identify odd num and print them

    Code:
    #include<stdio.h>
    #define TRUE 1
    
    int formOddInt(int n);
    
    int main() {
    
    	int num;
    
    	do {
    
    			printf("Enter a positive integer\n");
    			scanf("%d", &num);
    		} while (num <= 0);
    
    	printf("Num is = %d \n The new number is %d \n", num,formOddInt(num));
    
    	return TRUE;
    }
    
     int formOddInt(int n)
    {
    int i=0,d[5],count=0;
    while(n>0) {
    d[i]=n%10;
    n=n/10;
    i++ ;
    count++ ;
    }
    for(i=0;i<count;i++)
    printf("count=%d\td[i]=%d\n",count,d[i]);
    for(i=count;i>0;i--)
    {
     if((d[i]%2)!=0)
     {
    	return(d[i]);
       }
    }
       }
    Last edited by Rabbit; Apr 10 '13, 05:58 AM. Reason: Please use code tags when posting code.
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    You did not tell your problem. You should not expect that people would read your code and figure out your problem.

    Comment

    • vijay6
      New Member
      • Mar 2010
      • 158

      #3
      Hey ravali, the logical error is in line number 32 in your code. Inside the for loop assign value for 'i' as 'count - 1' instead of 'count'.

      Code:
      for( i = count - 1 ; i > 0 ; i-- ) // Line number 32

      And after 38th line write 'return -1' which means if input number don't have odd number then it'll print -1.

      Code:
      return -1; // After line number 38

      Note: Next time post/ask your question along with your code.

      Comment

      • gaurav moury
        New Member
        • Apr 2013
        • 1

        #4
        hi m gaurav..
        Code:
        // identify odd no. and print them
        #include<iostream.h>
        void main()
        { int a[10],b;
        cout<<"enter how many nos.";
        cin>>b;
        for(int i=0;i<=b;i++)
        { cout<<"enter "<<i+1<<"element";
          cin>>a[i];
        }
        cout<<"the odd nos. are  ";
        for(i=0;i<=b;i++)
        {if(a[i]!%2)
        cout<<a[i];
        }
        }
        //i wish this code would b understood by u..
        Last edited by Rabbit; Apr 11 '13, 04:23 AM. Reason: Please use code tags when posting code.

        Comment

        • fishirboy
          New Member
          • Mar 2013
          • 48

          #5
          Ya C++ with #include <iostream> but some people learn different and some times there way may be obsolete but they know it best and can use it the same as the new stuff, it's just harder.

          Comment

          • johny10151981
            Top Contributor
            • Jan 2010
            • 1059

            #6
            iostream.h is obsolete, but in South Asia region people start with turbo c++ 3.0, a 20 years old compiler. I believe their teacher should not suggest their pupil this compiler......

            Comment

            Working...