1. a + b_samebase #include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> int verifyInteger(){ int keeptrying = 1; int i=0; int isFloat=0; int isChar=0; int intNum; char str[100]; do{ fflush(stdin); i=0; isFloat=0; isChar=0; intNum = 0; scanf("%s", str); for(i=0; i<strlen(str);i++){ if (!((str[i]>='0')&&(str[i]<='9'))){ if (str[i]=='.'){ isFloat++; } else{ isChar++; } } } // printf("\nfloat: %d, char: %d", isFloat, isChar); intNum=atoi(str); if ((isChar || isFloat>0)||(intNum < 0)){ printf("Invaild input, try again: "); } else { // printf("This is valid int.\n"); // printf("input as int %lld\n", intNum); return intNum; keeptrying = 0; } }while(keeptrying == 1); } int convertToDecimal(int num, int base) { int decimalNum = 0, remainder, count = 0; while (num != 0) { remainder = num % 10; num /= 10; decimalNum += remainder * pow(base, count); count++; } return decimalNum; } int convertToBase(int num, int base) { int baseNum = 0, remainder, count = 0; while (num != 0) { remainder = num % base; num /= base; baseNum += remainder * pow(10, count); count++; } return baseNum; } int main() { int base, numA, numB; int check = 0; printf("Enter the base(base <= 10): "); do{ base = verifyInteger(); if(base <= 10){ check=1; }else{ printf("The base must be smaller or equal to 10, try again: "); check=0; } }while(check==0); printf("Enter number A: "); numA = verifyInteger(); printf("Enter number B: "); numB = verifyInteger(); int decimalA = convertToDecimal(numA, base); int decimalB = convertToDecimal(numB, base); int sum = decimalA + decimalB; int baseSum = convertToBase(sum, base); printf("The sum of %d and %d in base %d is: %d\n", numA, numB, base, baseSum); return 0; } 2. Encode: #include <stdio.h> #include <ctype.h> char encrypt(char plaintext) { if (plaintext >= 'A' && plaintext <= 'Z') { int p = plaintext - 'A'; int encrypted = (3 * p + 7) % 26; char ciphertext = encrypted + 'A'; return ciphertext; } return plaintext; } int main() { char plaintext[100]; printf("Enter the plaintext string: "); fgets(plaintext, sizeof(plaintext), stdin); char ciphertext[sizeof(plaintext)]; for (int i = 0; plaintext[i] != '\0'; i++) { ciphertext[i] = encrypt(toupper(plaintext[i])); } ciphertext[sizeof(plaintext) - 1] = '\0'; printf("Plaintext: %s\n", plaintext); printf("Ciphertext: %s\n", ciphertext); } 3. Decode #include <stdio.h> #include <ctype.h> char decrypt(char ciphertext) { if (ciphertext >= 'A' && ciphertext <= 'Z') { int c = ciphertext - 'A'; int inverse = 9; // 3^-1 = 9 (mod 26) int decrypted = (inverse * (c - 7 + 26)) % 26; char plaintext = decrypted + 'A'; return plaintext; } return ciphertext; } int main() { char ciphertext[100]; printf("Enter the ciphertext string: "); fgets(ciphertext, sizeof(ciphertext), stdin); char decryptedtext[sizeof(ciphertext)]; for (int i = 0; ciphertext[i] != '\0'; i++) { decryptedtext[i] = decrypt(toupper(ciphertext[i])); } decryptedtext[sizeof(ciphertext) - 1] = '\0'; printf("Decrypted text: %s\n", decryptedtext); return 0; }