Untitled
unknown
plain_text
21 days ago
17 kB
4
Indexable
#include <stdio.h> int main() { char sent[64]; int index = 0; printf("Write a sentence?\n"); scanf(" %64[^\n]", sent); printf("Look up character at index?\n"); scanf("%d", &index); printf("\nIn \"%s\" index %d is the character '%c'.\n", sent, index, sent[index]); return 0; } /////////////////////// #include <stdio.h> int main() { float num[5], sum = 0, average; printf("Please enter five real numbers:\n"); for (int i = 0; i < 5; i++) { if (i == 0) { printf("First number: \n"); } else if (i == 1) { printf("Second number: \n"); } else if (i == 2) { printf("Third number: \n"); } else if (i == 3) { printf("Fourth number: \n"); } else { printf("Fifth number: \n\n"); } scanf("%f", &num[i]); sum += num[i]; } average = sum/5; printf("The user entered:\n"); for (int i = 0; i < 5; i++) { printf(" %f", num[i]); if (i < 4) { printf(" then,\n"); } else { printf("\n\n"); } } printf("The average of these five numbers is: %f", average); return 0; } /////////////////// #include <stdio.h> int main() { int base; printf("Please enter a base:\n"); scanf("%d", &base); printf("The powers of %d are: \n\n", base); int result = 1; //start with power 0 which is always 1 for (int i = 0; i <= 8; i++) { printf("%d ^ %d is %d\n", base, i, result); result *= base; //Update the result by multiplying it by the base for the next power } return 0; } ////////////////////////////////// #include <stdio.h> int main() { char digits[9]; for (int i = 0; i < 9; i++) { digits[i] = '\0'; } printf("Input 8 digits or less:\n"); scanf("%8s", digits); for (int i = 0; i < 9; i++) { printf("digits[%d] is ASCII char: %c (%d) \n", i, digits[i], digits[i]); } return 0; } ////////////////// #include <stdio.h> #include <string.h> int main() { char first_name[50], last_name[50], full_name[100], l_f[100]; printf("First name?\n"); scanf("%50s", first_name); printf("Last name?\n\n"); scanf("%50s", last_name); strcpy(full_name, first_name); //copy first_name to full_name strcat(full_name, " "); //add a space strcat(full_name, last_name); //concatenates last_name to full_name printf("Full name: %s\n\n", full_name); strcpy(l_f, last_name); strcat(l_f, ", "); strcat(l_f, first_name); printf("Last, First: %s", l_f); return 0; } ////////////////////////////// #include <stdio.h> int main () { float vector[3], scalar; for (int i = 0; i < 3; i++) { if (i == 0) { printf("vx?\n"); } else if (i == 1) { printf("vy?\n"); } else { printf("vz?\n\n"); } scanf("%f", &vector[i]); } printf("v = < %f, %f, %f >\n\n", vector[0], vector[1], vector[2]); printf("Scalar Multiplier?\n"); scanf("%f", &scalar); for (int i = 0; i < 3; i++) { vector[i] = vector[i]*scalar; } printf("%fv = < %f, %f, %f >", scalar, vector[0], vector[1], vector[2]); return 0; } /////////////////// #include <stdio.h> int main() { char first, last; printf("What is the first letter of your first name?\n"); scanf(" %c", &first); printf("What is the first letter of your last name?\n"); scanf(" %c", &last); printf("Your initials are: %c%c", first, last); return 0; } ///////////////////// #include <stdio.h> int main() { char first[32], last[32], course[64]; printf("First name?\n"); scanf("%32s", first); printf("Last name?\n"); scanf("%32s", last); printf("Course name?\n"); scanf(" %64[^\n]", course); printf("\n%s %s is studying %s.", first, last, course); return 0; } /////////////////////// #include <stdio.h> int main() { char c; printf("Please input a letter, digit or symbol:\n"); scanf(" %c", &c); printf("The ASCII value for %c in base-10 is %d\n", c, c); printf("The ASCII value for %c in base-16 is 0x%x", c, c); return 0; } ///////////////////////// #include <stdio.h> int main() { float num; char string[20]; printf("Enter a real number:\n"); scanf("%f", &num); printf("Converting float input into ASCII C-String array...\n\n"); sprintf(string, "%f", num); for (int i = 0; i <= 7; i++) { printf("Element %d is: '%c' which is ASCII %d\n", i, string[i], string[i]); } return 0; } /////////////////////////////// #include <stdio.h> int main() { char ch; printf("Alphabet Facts!\n"); printf("^^^^^^^^^^^^^^^\n\n"); printf("Please input an uppercase letter:\n"); scanf(" %c", &ch); printf("Some interesting facts:\n\n"); printf(" 1) The lowercase version of the letter is '%c'.\n\n", ch + 32); printf(" 2) The letter '%c' comes before '%c' in the alphabet.\n\n", (char)(ch - 1), ch); printf(" 3) The letter '%c' comes after '%c' in the alphabet.\n\n", (char)(ch + 1), ch); printf(" 4) %c is letter number %d in the alphabet!", ch, ch - 'A' + 1); return 0; } ///////////////////// #include <stdio.h> #include <string.h> int main() { char original[6], copy[6]; printf("Type in a lowercase five letter word:\n\n"); scanf(" %5c", original); strcpy(copy, original); printf("Original: %s\n\n", copy); for (int i = 0; i < 5; i++) { copy[i] = copy[i] - ('a' - 'A'); } printf("Copy: %s", copy); return 0; } ///////////////////// #include <stdio.h> #include <math.h> int main () { float angle, hypotenuse, opposite, adjacent, angle_radians; printf("What is the angle in degrees?\n"); scanf("%f", &angle); printf("What is the length of the hypotenuse?\n\n"); scanf("%f", &hypotenuse); angle_radians = angle*(3.14159f / 180.0f); opposite = sinf(angle_radians)*hypotenuse; adjacent = cosf(angle_radians)*hypotenuse; printf("The length of the opposite is: %f\n", opposite); printf("The length of the adjacent is: %f", adjacent); return 0; } ///////////////////////// #include <stdio.h> int main() { char colour[10]; printf("What is your favourite colour? \n"); scanf("%9s", colour); printf("You like the colour %s.", colour); return 0; } ///////////////////////// #include <stdio.h> #include <math.h> int main() { float a, o, h; printf("What is the length of the opposite side?\n"); scanf("%f", &o); printf("What is the length of the adjacent side?\n"); scanf("%f", &a); h = sqrt(o*o + a*a); printf("The length of the hypotenuse is: %f", h); return 0; } ///////////////////////////// #include <stdio.h> #include <stdlib.h> int main() { int whole_number; char text[80]; whole_number = 0; printf("> "); scanf("%79s", text); whole_number = atoi(text); printf("C-String: "); printf("%s", text); printf("\n"); printf("int: "); printf("%d", whole_number); return 0; } //////////////////// #include <stdio.h> #include <math.h> // For log10f and floor functions int main() { float number; int num_digits; // Ask the user to input a number printf("Input a number:\n"); scanf("%f", &number); // Handle the case where the number is zero if (number == 0) { num_digits = 1; // Zero has 1 digit } else if (number > 0) { // Calculate the number of digits using log10f num_digits = (int)(floor(log10f(number))) + 1; } else { // If the number is negative, work with the absolute value num_digits = (int)(floor(log10f(fabs(number)))) + 1; } // Output the number of digits printf("%.0f is %d digit(s).", number, num_digits); return 0; } //////////////////////// #include <stdio.h> int main() { double numbers[7], product = 1.0; // Input the 7 real numbers for (int i = 0; i < 7; i++) { printf("Input real number %d: \n", i + 1); scanf("%lf", &numbers[i]); } // Print the numbers in the specified format printf("\nInput was: { "); for (int i = 0; i < 7; i++) { printf("%.2f", numbers[i]); if (i < 6) printf(", "); // Print a comma between numbers, but not after the last number } printf(" }\n"); // Calculate the product of the numbers for (int i = 0; i < 7; i++) { product *= numbers[i]; } // Print the product printf("\nThe product of the numbers input is: %.2f\n", product); return 0; } ///////////////////////// #include <stdio.h> int main() { int whole_number; char ascii_char; float real_number; // Prompt for whole number printf("Whole number? \n"); scanf("%d", &whole_number); // Prompt for ASCII character printf("ASCII character? \n"); scanf(" %c", &ascii_char); // Note the space before %c to consume any lingering newline character // Prompt for real number printf("Real number? \n"); scanf("%f", &real_number); // Echo inputs back to user with exact formatting printf("\nThe user's input was:\n\n"); printf("Whole number is: %d\n", whole_number); // 5 spaces before the number printf("ASCII character is: %c\n", ascii_char); // 3 spaces before the character printf("Real number is: %.6f\n", real_number); // 6 spaces before the real number, 6 decimal places return 0; } ///////////////// #include <stdio.h> int main() { int firstArray[3], secondArray[3], sum[3], diff[3], prod[3]; // First array setup printf("First Array Setup...\n\n"); for (int i = 0; i < 3; i++) { printf(" Please enter the %d%s value:\n", i + 1, (i == 0) ? "st" : (i == 1) ? "nd" : "rd"); scanf("%d", &firstArray[i]); } printf("\nFirst array is: { %d, %d, %d }\n", firstArray[0], firstArray[1], firstArray[2]); // Second array setup printf("\nSecond Array Setup...\n"); for (int i = 0; i < 3; i++) { printf(" Please enter the %d%s value:\n", i + 1, (i == 0) ? "st" : (i == 1) ? "nd" : "rd"); scanf("%d", &secondArray[i]); } printf("\nSecond array is: { %d, %d, %d }\n", secondArray[0], secondArray[1], secondArray[2]); // Calculations printf("\n C..a..l..c..u..l..a..t..i..n..g..\n\n"); for (int i = 0; i < 3; i++) { sum[i] = firstArray[i] + secondArray[i]; diff[i] = firstArray[i] - secondArray[i]; prod[i] = firstArray[i] * secondArray[i]; } // Results printf("Adding corresponding elements: { %d, %d, %d }\n\n", sum[0], sum[1], sum[2]); printf("Subtracting corresponding elements: { %d, %d, %d }\n\n", diff[0], diff[1], diff[2]); printf("Multiplying corresponding elements: { %d, %d, %d }\n", prod[0], prod[1], prod[2]); return 0; } ////////////////////////// #include <stdio.h> int main() { char lowerChar, upperChar; // Prompt for lower case character printf("Input a lower case character:\n"); scanf("%c", &lowerChar); // Read the lower case character // Clear the input buffer to prevent skipping the next input getchar(); // Prompt for upper case character printf("Input an upper case character:\n"); scanf("%c", &upperChar); // Read the upper case character // Convert lower case to upper case using ASCII values char convertedLower = lowerChar - 32; // Convert lower to upper // Convert upper case to lower case using ASCII values char convertedUpper = upperChar + 32; // Convert upper to lower // Display the conversion results exactly as required printf("\nlower %c equivalent upper is %c.\n", lowerChar, convertedLower); printf("upper %c equivalent lower is %c.\n", upperChar, convertedUpper); return 0; } ////////////////////////// #include <stdio.h> #include <math.h> // For sqrtf function int main() { float adjacent, opposite, hypotenuse; // Take input for adjacent and opposite sides printf("Enter the length of the adjacent side: \n"); scanf("%f", &adjacent); printf("Enter the length of the opposite side: \n"); scanf("%f", &opposite); // Calculate the hypotenuse using the Pythagorean Theorem hypotenuse = sqrtf((adjacent * adjacent) + (opposite * opposite)); // Display the hypotenuse length printf("\nThe hypotenuse is %.6f in length.\n\n", hypotenuse); // Display an ASCII triangle with labels printf(" +\n"); printf(" |\\\n"); printf(" | \\\n"); printf(" %.6f| \\ %.6f\n", opposite, hypotenuse); printf(" | \\\n"); printf(" | \\\n"); printf(" +-----+\n"); printf(" %.6f\n\n", adjacent); // Note about scale printf("Note: Right-angle triangle is not drawn to scale!\n"); return 0; } ///////////////////// #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char input[100]; int r = -1, g = -1, b = -1, count = 0; printf("Input colour:\n"); scanf("%s", input); // Parse RGB values if (strstr(input, "r")) { r = atoi(strstr(input, "r") + 1); if (r >= 0 && r <= 255) count += 2; } if (strstr(input, "g")) { g = atoi(strstr(input, "g") + 1); if (g >= 0 && g <= 255) count += 2; } if (strstr(input, "b")) { b = atoi(strstr(input, "b") + 1); if (b >= 0 && b <= 255) count += 2; } printf("\nSuccessfully parsed %d out of 6 items.\n", count); if (r != -1 && g != -1 && b != -1) { printf("\nColour channels detected: ( r, g, b )\n\n"); printf("r channel's value is %d (0x%x)\n", r, r); printf("g channel's value is %d (0x%x)\n", g, g); printf("b channel's value is %d (0x%x)\n", b, b); } else { printf("Invalid input.\n"); } return 0; } //////////////////////////// #include <stdio.h> #include <stdlib.h> // Include for atof() int main() { double real_number = 0.0; // Step 2: Declare and initialize the real_number char text[80]; // Step 3: Declare the char array for input // Step 4: Prompt the user printf("> "); // Step 5: Read input text from the user scanf("%79s", text); // Step 6: Convert the ASCII text to a floating-point number using atof real_number = atof(text); // Step 7: Print out the C-String printf("C-String: "); // Step 8: Print the input text printf("%s\n", text); // Step 9: Newline after the string output // Step 10: Print "double: " printf("double: "); // Step 11: Print the floating-point number printf("%f\n", real_number); return 0; } ///////////////////// #include <stdio.h> int main() { int number; char numberStr[9]; // Array to store the number as a C-String (8 digits + null terminator) // Step 1: Prompt the user to input a whole number (8 digits or less) printf("Enter a whole number (eight digits or less): \n"); scanf("%d", &number); // Step 2: Convert the integer to a C-String with leading zeros (using %08d) printf("Converting int input into ASCII C-String array...\n\n"); sprintf(numberStr, "%08d", number); // Step 3: Print the individual characters (ASCII values) of the numberStr array for (int i = 0; i < 8; i++) { printf("Element %d is: '%c' which is ASCII %d\n", i, numberStr[i], numberStr[i]); } return 0; } /////////////////////
Editor is loading...
Leave a Comment