804 views
0 0 votes
Write a program which will find the sum of product of consecutive digits . Eg

 When the input is 23145 the output is 2×3+3×1+1×4+4×5 = 33

1 Answer

1 1 vote
#include<stdio.h>
int cProduct(int);

int main(){
    printf("%d\n", cProduct(123456));
}

int cProduct(int num){
    if(num<10)
        return num;
    int result = 0;
    while(num){
        /* Making use of the fact that multiplication and addition are commutative */
        result += num%10*((num%100)/10);
        num/=10;
    }
    return result;
}
Position:
Show:

No related questions found