1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Help: C program

Discussion in 'Programming' started by mounaime10, Dec 12, 2015.

  1. #1
    Hello everybody,

    I am beginner in C programming and I want to improve this program by asking the user to enter an integer N and then the program make the sum of its digits and if the result of this sum is greater than a one digit we are remaking the sum of these digits.

    example: N= 12345 -----> S= 1+2+3+4+5 ---------> S= 15 ------------> S=6

    #include <stdio.h>
    main()
    {
    int T[50];
    int N;
    int I;
    long S;
    printf("D (max.50) : ");
    scanf("%d", &N );
    for (I=0; I<N; I++)
    {
    printf("E %d : ", I);
    scanf("%d", &T);
    }
    printf("T :\n");
    for (I=0; I<N; I++)
    printf("%d ", T);
    printf("\n");
    for (S=0, I=0; I<N; I++)
    S += T;
    printf("Sum : %ld\n", S);
    return 0;
    }

    Thanks in advance ;)
     
    mounaime10, Dec 12, 2015 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #2
    Your program has a lot of the telltales of faulty education or broken books; the instructors I had 30 years ago would have smacked me across the knuckles with a yardstick for being so needlessly cryptic, and for overthinking the solution.

    First, get some VERBOSE variable names in there instead of the cryptic single letters that mean dick all to other programmers. You're in a compiled language in 2015, there is no reason for the one letter variables that nobody can make sense out of.

    Second there are two operations that can greatly simplify what you are asking for; "while" and "%", the latter called a "modulo", which basically returns the remainder from a division operation.

    The best approach would be to use "while" to loop so long as your value is greater than ten. Inside that loop you set a holder "calc" variable to zero, then add "value % 10" to it, to then /= value after so long as value > 0. Once the value hits zero you copy that "calc" into value so the test can loop again.

    Something like (C isn't always C, much of your code would be invalid in the C compilers I use):

    #include <stdio.h>
    
    void main() {
    
    	int value, calc;
    		
    	printf("Value to checksum:");
    	scanf("%u", value);
    
    	printf("\ncalculating");
    	while (value > 9) {
    		calc = 0;
    		do {
    			calc += value % 10;
    			value /= 10;
    		} while (value > 0);
    		value = calc;
    	}
    
    	printf("\nSum: %l\n", value);
    	
    }
    Code (markup):
    Far simpler, single user input, can handle any valid unsigned integer of whatever width the default INT is for your compiler. Laughably this is one of the many stunning examples of a situation where C is absurdly inefficient compared to certain other languages -- it's the one I usually shove in halfwits faces when they make the BS claims about how C is "closer to assembly" than other languages.

    Since only people who know jack **** about assembly could make such a claim! :p

    See, in x86 machine language / assembler, a divide always creates a modulo, something most C compilers are too stupid to realize.
     
    deathshadow, Dec 12, 2015 IP
  3. mounaime10

    mounaime10 Greenhorn

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    Thank you for your quick response, that's very nice of you and it is a great pleasure to have a response someone like you. about your solution I'll try and I'll put you informed.
    I apologize for my English and thank you again
     
    mounaime10, Dec 13, 2015 IP