Quick actions

cmd+k|ctrl+k

Navigation

Languages

CS50_lecture1

Snippet info

Language

C

Visibility

public

Author

homex

Created

2022-09-23T10:12:08.621002Z

Updated

2022-09-23T10:57:57.49953Z

#include <stdio.h>
#include <math.h>

int judge(long a, int len);

int main(void) 
{
    long num;
    printf("Number: ");
    scanf("%li", &num);
    
    // Claim a new variable "check" to see how length the num is
    long check = num;
    int count = 0;
    while (check)
    {
        check = round(check / 10);
        count++;
    }
    printf("Length: %d\n", count);

    int sum = judge(num, count);
    printf("sum = %d\n", sum);
}


int judge(long a, int len)
{
    long even = 0, odd = 0, temp;
    
    for (int i = 1; i <= len; i++)
    {
        // odd terms
        if (i % 2 != 0)
        {
            odd += fmod(a, pow(10, i)) / pow(10, i - 1);  
            printf("odd: %li\n", odd);    
        }
        // even terms
        else
        {
            temp = fmod(a, pow(10, i)) / pow(10, i - 1) * 2;
            // temp *= 2;
            printf("temp: %li\n", temp);
            
            even += temp / 10 + temp % 10;

            printf("even: %li\n", even);
        }

    }
    return odd + even;
    
}
INFO