Прямо сейчас этот код берет все продукты введенной серии цифр и печатает их. Я хочу иметь возможность добавлять эти продукты вместе, но я борюсь с тем, как их структурировать, учитывая условия if else.
Необходимо иметь возможность выводить итоговые продукты каждого второго ди git, начиная со второго последнего, а затем добавлять эти продукты.
Любые советы о том, как структурировать это высоко ценится.
// Define libraries
#include <cs50.h>
#include <stdio.h>
#include <math.h>
int main(void)
{
// Declare Variables
long int number;
// Defining the function and storing it in a variable
// Creating do while loop to ensure the digit amount is valid
do
{
number = get_long("Credit Card Number: ");
}
while(number < 0);
// Creating variable to store every 2nd digit of credit card number
int second_last = (number % 100)/ 10;
int fourth_last = (number % 10000)/ 1000;
int sixth_last = (number % 1000000)/ 100000;
int eighth_last = (number % 100000000)/ 10000000;
int tenth_last = (number % 10000000000)/ 1000000000;
int twelfth_last = (number % 1000000000000)/ 100000000000;
int fourteenth_last = (number % 100000000000000)/ 10000000000000;
int sixteenth_last = (number % 10000000000000000)/ 1000000000000000;
// Multiplying every 2nd digit by 2 starting from the last digit
// Multiplying every 2nd digit by 2 starting from the last digit
int second_times_two = (second_last * 2);
//finding the product of every 2nd last digit
if (second_times_two >= 10)
{
int product_second_digit = (second_times_two % 10);
int remaining_digits_second = second_times_two - 10;
int prod_second_last = (product_second_digit + 1);
printf("%i\n", prod_second_last);
}
else
{
printf("%i\n", second_times_two);
}
int fourth_times_two = (fourth_last * 2);
//finding the product of every 4th last digit
if (fourth_times_two >= 10)
{
int product_fourth_digit = (fourth_times_two % 10);
int remaining_digits_fourth = fourth_times_two - 10;
int prod_fourth_last = (product_fourth_digit + 1);
printf("%i\n", prod_fourth_last);
}
else
{
printf("%i\n", fourth_times_two);
}
int sixth_times_two = (sixth_last * 2);
//finding the product of every 6th last digit
if (sixth_times_two >= 10)
{
int product_sixth_digit = (sixth_times_two % 10);
int remaining_digits_sixth = sixth_times_two - 10;
int prod_sixth_last = (product_sixth_digit + 1);
printf("%i\n", prod_sixth_last);
}
else
{
printf("%i\n", sixth_times_two);
}
int eighth_times_two = (eighth_last * 2);
//finding the product of every eight last digit
if (eighth_times_two >= 10)
{
int product_eighth_digit = (eighth_times_two % 10);
int remaining_digits_eighth = eighth_times_two - 10;
int prod_eighth_last = (product_eighth_digit + 1);
printf("%i\n", prod_eighth_last);
}
else
{
printf("%i\n", eighth_times_two);
}
int tenth_times_two = (tenth_last * 2);
//finding the product of every tenth last digit
if (tenth_times_two >= 10)
{
int product_tenth_digit = (tenth_times_two % 10);
int remaining_digits_tenth = tenth_times_two - 10;
int prod_tenth_last = (product_tenth_digit + 1);
printf("%i\n", prod_tenth_last);
}
else
{
printf("%i\n", tenth_times_two);
}
int twelfth_times_two = (twelfth_last * 2);
//finding the product of every twelfth last digit
if (twelfth_times_two >= 10)
{
int product_twelfth_digit = (twelfth_times_two % 10);
int remaining_digits_twelfth = twelfth_times_two - 10;
int prod_twelfth_last = (product_twelfth_digit + 1);
printf("%i\n", prod_twelfth_last);
}
else
{
printf("%i\n", twelfth_times_two);
}
int fourteenth_times_two = (fourteenth_last * 2);
//finding the product of every fourtenth last digit
if (fourteenth_times_two >= 10)
{
int product_fourteenth_digit = (fourteenth_times_two % 10);
int remaining_digits_fourteenth = fourteenth_times_two - 10;
int prod_fourteenth_last = (product_fourteenth_digit + 1);
printf("%i\n", prod_fourteenth_last);
}
else
{
printf("%i\n", fourteenth_times_two);
}
int sixteenth_times_two = (sixteenth_last * 2);
//finding the product of every sixteenth last digit
if (sixteenth_times_two >= 10)
{
int product_sixteenth_digit = (sixteenth_times_two % 10);
int remaining_digits_sixteenth = sixteenth_times_two - 10;
int prod_sixteenth_last = (product_sixteenth_digit + 1);
printf("%i\n", prod_sixteenth_last);
}
else
{
printf("%i\n", sixteenth_times_two);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
}