help to solve my problem

Discussion in 'C' started by dhruvil shah, Feb 8, 2011.

  1. dhruvil shah

    dhruvil shah New Member

    Joined:
    Jan 29, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    hey hi...i was supposed to read a 4 digit number and then find tje sum of its digts.....tell me what went wrong.....keep it too simple.....
    Code:
    #include<stdio.h>
    main()
    {
    int num,a,b,c,d,e;
    printf("enter the number");
    scanf("%d",&num);
    a=(num/1000);
    b=num/100;
    c=num/10;
    d=num%1000;
    e=a+b+c+d;
    printf("\n%d",&a);
    printf("\n%d",&b);
    printf("\n%d",&c);
    printf("\n%d",&d);
    printf("the sum is %d",&e);
    return(0);
    }
     
  2. Zoldat

    Zoldat New Member

    Joined:
    Feb 9, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hi!

    1. You use address of variables when you print numbers (&).
    2. Take any number and try to calculate manually using your formula. It doesn't work.

    Try to use loop to calculate the sum.
     
  3. pragnya

    pragnya New Member

    Joined:
    Feb 26, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Location:
    ujjain MP INDIA
    i think this would help u :-
    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int num;
    int a,b,c,d,e,f,g,h;
    printf("enter a no");
    scanf("%d",&num);
    a=num/1000;
    printf("d is %d \n",a);
    b=num/100;
    e=b%10;
    printf("e is %d \n",e);
    c=num/10;
    f=c%10;
    printf("b is %d \n ",f);
    d=num%1000;
    g=d%10;
    printf("a is % d \n",g);
    h=a+e+f+g;
    printf ("the sum is%d ",h);
    getch();
    }
     
  4. manoj kummar

    manoj kummar New Member

    Joined:
    Mar 6, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    hello... it is the solution for your problem...
    Code:
    #include<stdio.h>
    #include<conio.h>
    void main(void)
    {
    clrscr();
    float num,a,b,c,d,e;
    printf("enter the number");
    scanf("%f", &num);
    a= num/1000;
    b=num/100;
    c=num/10;
    d=num/1000;
    e=a+b+c+d;
    printf("\n%1f", a);
    printf("\n%1f", b);
    printf("\n%1f", c);
    printf("\n%1f", d);
    printf("\nthe sum is %f", e);
    getch();
    }
     
  5. mthushara

    mthushara New Member

    Joined:
    Aug 4, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #include<stdio.h>
    #include<conio.h>
    void main(void)
    {
    clrscr();             
                 int num,a,b,c,d,e;
    
    	printf("enter a 4 digit number : ");  //1234
    	
    	scanf("%d",&num);
    
    	a=num/1000;                                       //1
    	b=(num - (a*1000))/100;                       //(1234 - 1*1000)/100 = 234/100 = 2
    	c=(num - (a*1000) - (b*100))/10;          //(1234 - 1*1000 - 2*100)/10 = 34/10 = 3 
    	d=(num - (a*1000) - (b*100) - (c*10))/1; //(1234 - 1*1000 - 2*100 - 3*10)/1 = 34/1 = 1
    
    	e=a+b+c+d;
    	
    	printf("\n%d",a);
    	printf("\n%d",b);
    	printf("\n%d",c);
    	printf("\n%d",d);
    	printf("\nthe sum is %d\n",e);
    
                return 0;
    }
     
  6. manoj kummar

    manoj kummar New Member

    Joined:
    Mar 6, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    its the solution.......!!!

    Code:
    #include<stdio.h>
    #include<conio.h>
    void main(void)
    {
    int a,b,c,d,e,num;
    clrscr();
    printf("enter a 4 digit number : ");
    scanf("%d",&num);
    a=num/1000;
    b=(num - (a*1000))/100;
    c=(num - (a*1000) - (b*100))/10;
    d=(num - (a*1000) - (b*100) - (c*10))/1;
    
    e=a+b+c+d;
    
    printf("\n%d",a);
    printf("\n%d",b);
    printf("\n%d",c);
    printf("\n%d",d);
    printf("\nthe sum is %d\n",e);
    
    getch();
    }
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice