1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Help me

Discussion in 'C' started by zachry00, Sep 29, 2011.

  1. zachry00

    zachry00 New Member

    Joined:
    Jul 20, 2011
    Messages:
    30
    Likes Received:
    0
    Trophy Points:
    0
    Sum of even digits:

    Code:
    #include <stdio.h>
    
    void even(int num)
    {
      int index, num, sum = 0;
      for(index = 0; index <= num; index += 2)
        if(num % 2 == 0)
          sum += 1;
          printf("The sum of even numbers is: %d", sum);
    }
    
    void main()
    {
        int num;
        clrscr();
     
        printf("\nEnter Number: ");
        scanf("%d", &num);
    
        even(num);
    
        getch();
    }
    what wrong in this code. the answer is always 0 what is the correct program to get this sample run:

    Enter number: 12345
    Even Number: 6

    please help me
     
  2. gpk kishore

    gpk kishore New Member

    Joined:
    Jun 30, 2011
    Messages:
    82
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    void even(int num,int p)
    {
     int index,sum=0,r,s,q;
     s=p;
    for(index=1;index<=s;index++,p--)
    {
    r=num/pow(10,(p-1));
    q=num-r*pow(10,(p-1));
    num=q;
    if(index%2==0)
    {
    sum+=r;
    }
    }
    
    printf("%d",sum);
    }
    void main()
    {
    int num,c,p=0;
    clrscr();
    printf("Enter number");
    scanf("%d",&num);
    c=num;
    while(c>0)
    {
    c=c/10;
    p++;
    }
    even(num,p);
    getch();
    }
     

Share This Page