sum-digits function - C language - c

Digits Sum function
I have to write a function - digits sum,
for ex.: the number : 9887, 9+8+8+7= 32 , 3+2= 5
Will this function work well?
int sum_digits(int num){
if (num < 10)
return num;
int a = sum_digits (num / 10) + num % 10;
if (a > 9)
a = sum_digits (a / 10) + a % 10;
return a;
}

int sum_digits(int num){
if (num < 10)
return num;
int a = sum_digits (num / 10) + num % 10;
if (a > 10)
a += sum_digits (a / 10) + a % 10;
return a;
}
This will work.
Note that a= is replaced by a+= and a>9 is replaced by a>10

Related

Recurrence relation of recursive function that contains while loop

I have a recursive function int digit_sum(int number); that returns the sum of all the digits in the number. For example, digit_sum(159) = 1 + 5 + 9 = 15.
Here is the function:
int digit_sum(int n)
{
if (n < 0)
n = -n;
if (n < 10)
return n % 10;
while (n % 10 == 0 && n > 0)
n = n / 10;
return n % 10 + digit_sum(n - (n % 10));
}
I'm not sure exactly how to write a recurrence relation for this function. I know that T(0) is the sum of the first two if statement constants. However, with T(n), I am unsure how to express the while loop term and T(n-k).
The modulo operators are throwing me off. This is a guess, and I'm fairly certain this is wrong:
T(n) = c_1 + c_2 + c_3*n (while loop) + (n mod 10 + T(n - (n mod 10))) for n >= 10
I know that the entire T(n-k) term is wrong.
Here is a possible solution:
#include <stdio.h>
int digit_sum(int n)
{
if (n < 0)
return digit_sum(-n);
if (n < 10)
return n;
return (n % 10) + digit_sum(n / 10);
}
int main(int argc, char *argv[])
{
printf(" 9->%d\n", digit_sum(9));
printf(" 59->%d\n", digit_sum(59));
printf(" 159->%d\n", digit_sum(159));
printf("-159->%d\n", digit_sum(-159));
}

Changing a value of an integer in a function

I have a problem with the function 'multiply2' which doesn't change values of ccn1 to ccn8. It has to multiply them by 2, and if the result is equal or more than 10, than take products of this equation and add them together (i.e. 7 * 2 = 14, so 1 + 4 = 5). The whole thing would work, but I've noticed that the function 'multiply2' keeps the value of pX to itself, and I'd like to overwrite values of ccn1 to ccn8, and then sum all values together (ccn1 to ccn16). Please help :(
PS: I'm new at this so please be gentle, I'm sure it looks like a mess to you.
The program compiles as it is with no errors, mind you, it does include cs50.h library for "get_long_long" function which prompts a user for a credit card number
I've tried to use pointers but clearly I don't fully understand the concept! I've managed to dereference *pX which assigned values of ccn1, ccn2, etc. but they still don't update in main.
the whole problem described here: https://docs.cs50.net/2018/x/psets/1/credit/credit.html#tl-dr
Thank you!
# include <cs50.h>
# include <stdio.h>
# include <math.h>
void multiply2 (int * pX);
long long ccn;
//a program to check the credit card number and print out if it's American Express, Visa, or MasterCard. if else - ILVALID.
int main(void)
{
//prompt user for a credit card number
ccn = get_long_long("Number: ");
//every second digit starting from second to last
int ccn1 = (ccn % 100) / 10;
int ccn2 = (ccn % 10000) / 1000;
int ccn3 = (ccn % 1000000) / 100000;
int ccn4 = (ccn % 100000000) / 10000000;
int ccn5 = (ccn % 10000000000) / 1000000000;
int ccn6 = (ccn % 1000000000000) / 100000000000;
int ccn7 = (ccn % 100000000000000) / 10000000000000;
int ccn8 = (ccn % 10000000000000000) / 1000000000000000;
//printf("%i\n%i\n%i\n%i\n%i\n%i\n%i\n%i\n", ccn1, ccn2, ccn3, ccn4, ccn5, ccn6, ccn7, ccn8);
//all the other digits
int ccn9 = (ccn % 10);
int ccn10 = (ccn % 1000) / 100;
int ccn11 = (ccn % 100000) / 10000;
int ccn12 = (ccn % 10000000) / 1000000;
int ccn13 = (ccn % 1000000000) / 100000000;
int ccn14 = (ccn % 100000000000) / 10000000000;
int ccn15 = (ccn % 10000000000000) / 1000000000000;
int ccn16 = (ccn % 1000000000000000) / 100000000000000;
//printf("%i\n%i\n%i\n%i\n%i\n%i\n%i\n%i\n", ccn9, ccn10, ccn11, ccn12, ccn13, ccn14, ccn15, ccn16);
if (((ccn >= 340000000000000 && ccn <= 349999999999999) || (ccn >= 370000000000000 && ccn <= 379999999999999)) ||
((ccn >= 5100000000000000 && ccn <= 5199999999999999) || (ccn >= 5500000000000000 && ccn <= 5599999999999999)) ||
((ccn >= 4000000000000 && ccn <= 4999999999999) || (ccn >= 4000000000000000 && ccn <= 4999999999999999)))
{
multiply2(&ccn1);
multiply2(&ccn2);
multiply2(&ccn3);
multiply2(&ccn4);
multiply2(&ccn5);
multiply2(&ccn6);
multiply2(&ccn7);
multiply2(&ccn8);
int sum = (ccn1 + ccn2 + ccn3 + ccn4 + ccn5 + ccn6 + ccn7 + ccn8 + ccn9 + ccn10 + ccn11 + ccn12 + ccn13 + ccn14 + ccn16);
printf("%i\n", sum);
}
/NOTHING FROM ABOVE - INVALID
else
{
printf("INVALID\n");
}
}
void multiply2 (int * pX)
{
if ((*pX *2) >= 10)
{
*pX = ((*pX * 2) % 10) + 1;
printf("%i\n", *pX);
}
else
{
printf("%i\n", (*pX * 2));
}
}
Your function multiply2 changes the value of the passed parameter only in case its value is at least 5.
Try this:
void multiply2(int* pX)
{
*pX = *pX * 2; /* Multiply value by 2 */
if (*pX >= 10)
*pX = (*pX % 10) + 1; /* Adjust if value greater than or equal to 10 */
printf("%d\n", *pX);
}

How can I specifically take out numbers in an integer, in C?

Lets say I have an integer called SIN and the scanf input receives 193456787.
so SIN = 193456787;
What I want to do is add up all the other numbers after the first digit.
So 9 + 4 + 6 + 8 = 27
Can somebody please explain to a beginner how to do this?
Print the number and then sum every other digit
int sum_every_other_digit_after_first(unsigned long long x) {
char buf[sizeof x * CHAR_BIT];
sprintf(buf, "%llu", x);
char *p = buf;
int sum = 0;
while (*p) {
p++; // Skip digit
if (*p) {
sum += *p++ - '0';
}
}
return sum;
}
or as inspired by #PageNotFound
int sum_every_other_digit_after_first(unsigned long long x) {
int esum = 0;
int osum = 0;
while (x > 0) {
esum += x%10;
x /= 10;
if (x == 0) {
return osum;
}
osum += x%10;
x /= 10;
}
return esum;
}
or for fun, a recursive solution
int sum_every_other_digit_after_first_r(unsigned long long x, int esum, int osum) {
if (x >= 100) {
int digit2 = x % 100;
esum += digit2 % 10;
osum += digit2 / 10
return sum_every_other_digit_after_first_r(x / 100, esum, osum);
}
if (x >= 10) {
return esum + x % 10;
}
return osum;
}
sum_every_other_digit_after_first_r(1234567,0,0) --> 12
My solution
#include <stdio.h>
int main()
{
int SIN = 193456787;
int a = 0, b = 0, cnt = 0;
while (SIN > 0) {
if (cnt % 2) b += SIN % 10;
else a += SIN % 10;
cnt++;
SIN /= 10;
}
printf("%d\n", cnt%2 ? b : a);
return 0;
}
Note: Please comment if this is not what you intended, as your question is a little ambigous.
#include <stdio.h>
int main() {
unsigned number;
scanf("%u\n", &number);
unsigned result = 0;
unsigned tmp = number;
unsigned numberOfDigits = 0;
do
numberOfDigits++;
while((tmp /= 10) != 0);
if(numberOfDigits % 2 != 0)
number /= 10;
while(number >= 10) {
result += number % 10;
number /= 100; // Skip two digits
}
printf("%u\n", result);
}

C program not displaying value

when i call my multi() it will calculate the binary number just fine, but won't calculate the adding(). If i switch the two in order, it will calculate the adding() and not calculate the multi(). Is there a reason why the second function call isn't displaying its value? It will print the printf, but not the value. Any help would be great.
#include <stdio.h>
long binary1, binary2, binary3, binary4, multiply = 0;
int binaryproduct(int, int);
int digit, factor = 1;
int multi();
int adding();
int main()
{
binary3 = binary1;
binary4 = binary1;
printf("Enter the first binary number: ");
scanf("%ld", &binary1);
printf("Enter the second binary number: ");
scanf("%ld", &binary2);
multi();
printf("\n");
adding();
}
int adding()
{
int i = 0, remainder = 0, sum[20];
while (binary3 != 0 || binary4 != 0) {
sum[i++] =(binary3 % 10 + binary4 % 10 + remainder) % 2;
remainder =(binary3 % 10 + binary4 % 10 + remainder) / 2;
binary1 = binary3 / 10;
binary2 = binary4 / 10;
}
if (remainder != 0) {
sum[i++] = remainder;
}
--i;
printf("Sum of two binary numbers: ");
while (i >= 0) {
printf("%d", sum[i--]);
}
return 0;
}
int multi()
{
while (binary2 != 0) {
digit = binary2 % 10;
if (digit == 1) {
binary1 = binary1 * factor;
multiply = binaryproduct(binary1, multiply);
} else {
binary1 = binary1 * factor;
}
binary2 = binary2 / 10;
factor = 10;
}
printf("Product of two binary numbers: %ld", multiply);
return 0;
}
int binaryproduct(int binary3, int binary4)
{
int i = 0, remainder = 0, sum[20];
int binaryprod = 0;
while (binary1 != 0 || binary2 != 0) {
sum[i++] =(binary1 % 10 + binary2 % 10 + remainder) % 2;
remainder =(binary1 % 10 + binary2 % 10 + remainder) / 2;
binary1 = binary1 / 10;
binary2 = binary2 / 10;
}
if (remainder != 0) {
sum[i++] = remainder;
}
--i;
while (i >= 0) {
binaryprod = binaryprod * 10 + sum[i--];
}
return binaryprod;
}
Your globals aren't being initialized. In you adding() function you use:
binary1 = binary3/10
binary2 = binary4/10
and in the binaryproduct function you have two local ints named binary3, binary4 that you don't use.
You should be using unique names, initializing you variables, use local variables in places where globals are unnecessary.

My program stops looping and i cannot figure out why

if the user inputs the "value" as 1223445 the output should read as follows:
After change #1: 12235
After change #2: 135
the code is meant to take out two consecutive numbers with the same value. the first loop works but then it stops and I cannot figure why. here is the code:
{
int count;
int y;
int z;
int b;
int c;
int d;
int a;
int ct;
y = 0;
z = 1;
d = 0;
count = 0;
if (value > 0)
while ((z * 10 + z) != (y % 100))
{
y = value % 10 + y * 10;
z = y % 10;
value /= 10;
count = count + 1;
}
value = value * pow(10, count - 2);
y = y / 100;
count = count - 3;
while(y > 0)
{
b = y % 10;
c = pow(10, count);
d = d + c * b;
y = y / 10;
count = count - 1;
}
value = value + d;
ct = 1;
printf("After change #%d: %d\n", ct, value);
a = value;
while (a > 1)
{
if((a % 100) - (a % 10) - (10 * (a % 10)) == 0)
Change(value);
else
a = a / 10;
}
return;
}
Here's a simple solution: (not sure how you want to handle odd numbered repeats e.g. '111', '11111')
public static long strip(long value, long sum) {
if(value==0) return sum;
long tens = value % 100;
long ones = value % 10;
if(ones*10 == tens-ones) {
return strip(value /100, sum);
}
if(sum==0) sum +=(value%10);
else {
long x = (long)Math.ceil((Math.log10(sum)));
sum =(long) (Math.pow(10,x) * ones + sum);
}
return strip(value /10, sum);
}

Resources