├── Learning ├── 1. Functions │ ├── READme │ ├── compute-factorial.c │ ├── largest-number-from-3.c │ ├── odd-or-even.c │ └── sum-of-first-n.c ├── 2. Arrays │ ├── READme │ ├── array-as-function-argument.c │ ├── largest-array-element.c │ ├── multiply-array-by-10.c │ ├── smallest-array-element.c │ └── student-grades.c ├── 3. Strings │ ├── READme │ ├── copyString-without-strcpy().c │ ├── frequency-of-char.c │ └── stringLength-without-strlen().c ├── 4. Pointers │ ├── READme │ ├── add10-to-arrayElements.c │ ├── divide-floatingPoint-numbers.c │ ├── find-square.c │ ├── multipy-arrayElement-byN.c │ └── small-element-pointers.c ├── 5. Structs │ ├── READme │ ├── add-complex-numbers.c │ ├── add-complex-numbers2.c │ ├── add-complex-numbers3.c │ ├── addingMass-with-struct.c │ ├── area-rectangle.c │ ├── perimeter-rectangle.c │ ├── print-struct-values.c │ ├── store-player-info.c │ ├── struct-functionArgument.c │ ├── structs&-pointers.c │ └── subtract-complex-numbers.c ├── 6. Additional topics │ ├── READme │ ├── aoc-with-macro.c │ ├── array-increase-realloc.c │ ├── enumeration-switch-case.c │ ├── malloc.c │ ├── nested-loop.c │ ├── prime-numbers.c │ └── subtract-2-matrices.c └── 7. Challenges │ ├── 1. Intro │ ├── READme │ ├── calculate-cost-price.c │ ├── calculate-profit.c │ ├── cm-to-ft.c │ ├── cube-volume.c │ ├── divide-remainder.c │ ├── fahrenheit-to-celsius.c │ ├── find-rectangle-area.c │ ├── km-to-mi.c │ ├── max-handshakes.c │ ├── pounds-to-kilos.c │ └── swap-2-numbers.c │ ├── 2. Control Flow │ ├── READme │ ├── add-numbers-condition.c │ ├── calculate-employee-bonus.c │ ├── calculator.c │ ├── can-vote.c │ ├── check-abundant-number.c │ ├── check-armstrong-number.c │ ├── check-leap-year.c │ ├── check-negative-number.c │ ├── check-prime-number.c │ ├── compute-student-grades.c │ ├── count-digits.c │ ├── factor-of-number.c │ ├── find-factorial.c │ ├── find-youngest-brother.c │ ├── greatest-number-factor.c │ ├── greatest-number-multiple.c │ ├── harshad-number.c │ ├── input-print.c │ ├── internal-triangle-angles.c │ ├── is-char-alphabet.c │ ├── natural-num-sum.c │ ├── number-guessing-game.c │ ├── odd-or-even.c │ ├── power-of-number.c │ ├── print-3-sentences.c │ ├── print-multiplication-table.c │ ├── print-with-condition.c │ └── sum-of-digits.c │ ├── 3. Functions │ ├── GCD-two-numbers.c │ ├── LCM-two-numbers.c │ ├── READme │ ├── area-of-circle.c │ ├── armstrong-numbers.c │ ├── calculate-simple-interest.c │ ├── check-palindrome-number.c │ ├── check-perfect-number.c │ ├── prime-numbers.c │ └── self-dividing-number.c │ ├── 4. Arrays │ ├── READme │ ├── compute-standard-deviation.c │ ├── even-array-numbers.c │ ├── multiply-array.c │ ├── player-mean-height.c │ ├── smallest-array-element.c │ ├── standard-decviation-improved.c │ └── student-percentage-marks.c │ ├── 5. Strings │ ├── READme │ ├── char-frequency.c │ ├── chars-in-string.c │ ├── count-vowels.c │ ├── find-longer-string.c │ └── join-two-strings.c │ └── READme └── Projects ├── READme └── rock-paper-scissors.c /Learning/1. Functions/READme: -------------------------------------------------------------------------------- 1 | Hi there! Here you will find basic code as I learn C programming. We all have to start somewhere :) 2 | -------------------------------------------------------------------------------- /Learning/1. Functions/compute-factorial.c: -------------------------------------------------------------------------------- 1 | //Create a program to compute the factorial of a number using a function. 2 | 3 | 4 | #include 5 | 6 | int computeFactorial(int n){ 7 | int factorial = 1; 8 | 9 | for (int i = 0; i <= n; ++i){ 10 | factorial = factorial * i; 11 | 12 | } 13 | 14 | return factorial; 15 | } 16 | 17 | int main() { 18 | 19 | int number; 20 | scanf("%d", &number); 21 | 22 | int total = computeFactorial(number); 23 | printf("%d", total); 24 | 25 | return 0; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /Learning/1. Functions/largest-number-from-3.c: -------------------------------------------------------------------------------- 1 | // Create a program that prints the largest number among three numbers by creating a function. 2 | 3 | #include 4 | 5 | int getLargestNumber(int n1, int n2, int n3){ 6 | 7 | if (n1>n2 && n1>n3) { 8 | largestNumber = n1; 9 | } 10 | 11 | else if (n2>n1 && n2>n3){ 12 | largestNumber = n2; 13 | } 14 | 15 | else { 16 | largestNumber = n3; 17 | } 18 | 19 | return largestNumber; 20 | 21 | } 22 | 23 | int main() { 24 | 25 | int largestNnumber = getLargestNumber(15, 12, 30) // output = 30 26 | printf("%d", largestNumber); 27 | 28 | largestNnumber = getLargestNumber(12, 120, -30) // output = 120 29 | printf("%d", largestNumber); 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Learning/1. Functions/odd-or-even.c: -------------------------------------------------------------------------------- 1 | // Create a program to check whether a number is odd or even by creating a function. 2 | 3 | 4 | #include 5 | 6 | int oddEven(int number){ 7 | 8 | if (number % 2 ==0){ 9 | return 1; 10 | } 11 | 12 | else{ 13 | return 0; 14 | } 15 | } 16 | 17 | int main(){ 18 | 19 | int n; 20 | printf("Enter a number: "); 21 | scanf("%d", &n); 22 | 23 | int result = oddEven(n); 24 | 25 | if (result == 1){ 26 | printf("%d is Even", n); 27 | } 28 | 29 | else { 30 | printf("%d is Odd", n); 31 | } 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Learning/1. Functions/sum-of-first-n.c: -------------------------------------------------------------------------------- 1 | // Create a program to find the sum of first n natural numbers 2 | 3 | #include 4 | 5 | int computeSum(int number){ 6 | int total = 0; 7 | 8 | for (int i = 0; i<=number; i++){ 9 | total = total + i; 10 | } 11 | 12 | return total; 13 | 14 | } 15 | 16 | int main() { 17 | 18 | int result = computeSum(15); 19 | printf("%d\n", result); // Output: 120 20 | 21 | result = computeSum(120); 22 | printf("%d", result); // Output: 7260 23 | 24 | return 0; 25 | } 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /Learning/2. Arrays/READme: -------------------------------------------------------------------------------- 1 | Hi there! Here you will find basic code as I learn C programming. We all have to start somewhere :) 2 | -------------------------------------------------------------------------------- /Learning/2. Arrays/array-as-function-argument.c: -------------------------------------------------------------------------------- 1 | //Create a program to find the average marks obtained by a student using a function. 2 | 3 | #include 4 | 5 | double findAverage(double marks[5]) { 6 | double sum = 0.0; 7 | 8 | for (int i = 0; i < 5; ++i) { 9 | sum = sum + marks[i]; 10 | } 11 | 12 | double average = sum / 5; 13 | 14 | return average; 15 | } 16 | 17 | int main() { 18 | 19 | double marks[5] = {54.8, 59.8, 48.7, 42.6, 60.1}; 20 | 21 | double result = findAverage(marks); 22 | printf("%.2lf", result); // Output = 53.20 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Learning/2. Arrays/largest-array-element.c: -------------------------------------------------------------------------------- 1 | // Print the largest element of the array 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | int numbers[5] = {55, 64, 75, 80, 65}; 8 | 9 | int largest = numbers[0]; 10 | 11 | for (int i = 1; i < 5; ++i) { 12 | if (largest < numbers[i]) { 13 | largest = numbers[i]; 14 | } 15 | } 16 | 17 | printf("Largest: %d", largest); 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Learning/2. Arrays/multiply-array-by-10.c: -------------------------------------------------------------------------------- 1 | //Create a program to multiply each element of an array by 10 2 | 3 | #include 4 | 5 | int main(){ 6 | 7 | 8 | int numbers[4] = {43, 78, 23, 45}; 9 | int newNumbers[4]; 10 | 11 | for (int i = 0; i<4; i++){ 12 | newNumbers[i] = numbers[i]*10; 13 | } 14 | 15 | for (int i = 0; i<4; i++){ 16 | printf("%d\n", newNumbers[i]); 17 | } 18 | 19 | return 0; 20 | 21 | } -------------------------------------------------------------------------------- /Learning/2. Arrays/smallest-array-element.c: -------------------------------------------------------------------------------- 1 | // Create a program to find the smallest element of the array 2 | 3 | #include 4 | 5 | int findSmallest(int array[5]) { 6 | int smallest = array[0]; 7 | 8 | for (int i=1; i<5; i++){ 9 | if (smallest > array[i]){ 10 | smallest = array[i]; 11 | } 12 | } 13 | 14 | 15 | return smallest; 16 | } 17 | 18 | int main() { 19 | 20 | int numbers[5] = {55, 64, 75, 80, 65}; 21 | 22 | int smallest = findSmallest(numbers); 23 | printf("%d", smallest); 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Learning/2. Arrays/student-grades.c: -------------------------------------------------------------------------------- 1 | // Assign grades to students based on their average scores 2 | 3 | #include 4 | 5 | double getAverageScore(double scores[5]){ 6 | 7 | double score = 0; 8 | 9 | for (int i=0; i<5; i++){ 10 | score = score + scores[i]; 11 | } 12 | double average = score / 5; 13 | 14 | return average; 15 | } 16 | 17 | char computeGrade(double averageScore){ 18 | char grade; 19 | 20 | if (averageScore >= 80){ 21 | grade = 'A'; 22 | } 23 | 24 | else if (averageScore >= 60) 25 | { 26 | grade = 'B'; 27 | } 28 | 29 | else if (averageScore >= 50) 30 | { 31 | grade = 'C'; 32 | } 33 | 34 | else { 35 | grade = 'F'; 36 | } 37 | 38 | return grade; 39 | } 40 | 41 | 42 | int main(){ 43 | 44 | double scores[5]; 45 | printf("Enter array element\n"); 46 | for (int i = 0; i<5; i++){ 47 | scanf("%lf", &scores[i]); 48 | } 49 | 50 | 51 | double averageScore = getAverageScore(scores); 52 | char grade = computeGrade(averageScore); 53 | printf("Average score is: %.2lf\n", averageScore); 54 | printf("Average grade is: %c", grade); 55 | 56 | return 0; 57 | } -------------------------------------------------------------------------------- /Learning/3. Strings/READme: -------------------------------------------------------------------------------- 1 | Hi there! Here you will find basic code as I learn C programming. We all have to start somewhere :) 2 | -------------------------------------------------------------------------------- /Learning/3. Strings/copyString-without-strcpy().c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int int main() 5 | { 6 | char source [100]; 7 | char destination [100]; 8 | 9 | int i; 10 | 11 | printf("Enter the source string: "); 12 | fgets(source, sizeof(source)); 13 | 14 | for(i = 0; i strlen(source); i++){ 15 | destination[i] = source[i] 16 | 17 | } 18 | 19 | destination[i] = '\0'; 20 | printf("%s", destination); 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Learning/3. Strings/frequency-of-char.c: -------------------------------------------------------------------------------- 1 | // Given a character & string find the number of times this character is present in the string 2 | 3 | #include 4 | #include 5 | 6 | int main() { 7 | 8 | char text[] = "C programming is my second language."; 9 | char ch = 'e'; 10 | 11 | int freq = 0; 12 | 13 | for (int i = 0; i < strlen(text); i++){ 14 | 15 | if (ch == text[i]){ 16 | ++freq; 17 | } 18 | 19 | printf("%d", freq); 20 | } 21 | 22 | -------------------------------------------------------------------------------- /Learning/3. Strings/stringLength-without-strlen().c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | 5 | char text[] = "I love C"; 6 | 7 | int length = 0; 8 | 9 | while (text[length] != '\0'){ 10 | ++length 11 | } 12 | 13 | printf("Length: %d", length); 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Learning/4. Pointers/READme: -------------------------------------------------------------------------------- 1 | Hi there! Here you will find basic code as I learn C programming. We all have to start somewhere :) 2 | -------------------------------------------------------------------------------- /Learning/4. Pointers/add10-to-arrayElements.c: -------------------------------------------------------------------------------- 1 | // Add 10 to each element of array 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | int numbers[4] = {8, 7, 21, 13}; 8 | 9 | for (int i = 0; i < 4; ++i) { 10 | *(numbers + i) = *(numbers + i) + 10; 11 | } 12 | 13 | printf("Array Elements: "); 14 | for (int i = 0; i < 4; ++i) { 15 | printf("%d ", *(numbers + i)); 16 | } 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Learning/4. Pointers/divide-floatingPoint-numbers.c: -------------------------------------------------------------------------------- 1 | // Divide Two Floating Point Numbers 2 | 3 | #include 4 | 5 | double* divideNumbers(double* n1, double* n2, double* result){ 6 | *result = *n1 / *n2; 7 | return result; 8 | } 9 | 10 | int main(){ 11 | double number1; 12 | double number2; 13 | scanf("%lf %lf", &number1, &number2); 14 | 15 | double result; 16 | 17 | double* value = divideNumbers(&number1, &number2, &result); 18 | printf("%.2lf", *value); 19 | 20 | return 0; 21 | 22 | } -------------------------------------------------------------------------------- /Learning/4. Pointers/find-square.c: -------------------------------------------------------------------------------- 1 | //Create a program to find the square of a number using pointers 2 | 3 | #include 4 | 5 | void findSquare(int* n) { 6 | int square = (*n * *n); //Prev. int square; square = *n**n; 7 | 8 | 9 | *n = square; 10 | } 11 | 12 | int main() { 13 | int number; 14 | scanf("%d", &number); 15 | 16 | 17 | findSquare(&number); //Pass address to call function 18 | 19 | printf("%d", number); 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Learning/4. Pointers/multipy-arrayElement-byN.c: -------------------------------------------------------------------------------- 1 | //Multiply array elements by N 2 | #include 3 | 4 | int main() { 5 | 6 | int primeNumbers[5] = {2, 3, 5, 7, 11}; 7 | 8 | int number; 9 | scanf("%d", &number); 10 | 11 | int* pn = &number; 12 | 13 | for (int i = 0; i < 5; ++i) { 14 | *(primeNumbers + i) = *(primeNumbers + i) * *pn; 15 | } 16 | 17 | 18 | printf("Array Elements: "); 19 | for (int i = 0; i < 5; ++i) { 20 | printf("%d\n", *(primeNumbers + i)); 21 | } 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Learning/4. Pointers/small-element-pointers.c: -------------------------------------------------------------------------------- 1 | // Create a program to find the smallest element of the array using pointers given int numbers[5] = {55, 64, 75, 80, 65}; 2 | 3 | #include 4 | 5 | int main() 6 | { 7 | int numbers[5] = {55, 64, 75, 80, 65}; 8 | 9 | int smallest = *numbers; 10 | 11 | for (int i = 1; i < 5; i++){ 12 | if (smallest > *(numbers + i)){ 13 | smallest = *(numbers + i); 14 | } 15 | } 16 | 17 | printf("%d\n", smallest); 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Learning/5. Structs/READme: -------------------------------------------------------------------------------- 1 | Hi there! Here you will find basic code as I learn C programming. We all have to start somewhere :) 2 | -------------------------------------------------------------------------------- /Learning/5. Structs/add-complex-numbers.c: -------------------------------------------------------------------------------- 1 | //add two complex numbers 2 | 3 | #include 4 | 5 | typedef struct Complex { 6 | double real; 7 | double imag; 8 | } complex; 9 | 10 | int main() { 11 | 12 | complex c1 = {.real = 1.2, .imag = 1.4}; 13 | complex c2 = {.real = 2.9, .imag = 2.3}; 14 | complex result; 15 | 16 | result.real = c1.real + c2.real; 17 | result.imag = c1.imag + c2.imag; 18 | 19 | printf("Result: %.2lf + %.2lfi", result.real, result.imag); 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Learning/5. Structs/add-complex-numbers2.c: -------------------------------------------------------------------------------- 1 | //return struct from function 2 | 3 | #include 4 | 5 | typedef struct Complex { 6 | double real; 7 | double imag; 8 | } complex; 9 | 10 | complex addNumbers(complex c1, complex c2) { 11 | complex sum; 12 | sum.real = c1.real + c2.real; 13 | sum.imag = c1.imag + c2.imag; 14 | return sum; 15 | } 16 | 17 | int main() { 18 | 19 | complex c1 = {.real = 1.4, .imag = 4.5}; 20 | complex c2 = {.real = 5.4, .imag = -3.4}; 21 | complex result; 22 | 23 | result = addNumbers(c1, c2); 24 | printf("Sum: %.2lf + %.2lfi", result.real, result.imag); 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Learning/5. Structs/add-complex-numbers3.c: -------------------------------------------------------------------------------- 1 | // add complex numbers with input 2 | #include 3 | 4 | typedef struct Complex { 5 | double real; 6 | double imag; 7 | } complex; 8 | 9 | int main() { 10 | 11 | complex c1, c2, result; 12 | 13 | scanf("%lf %lf ", &c1.real, &c1.imag); 14 | 15 | scanf("%lf %lf ", &c2.real, &c2.imag); 16 | 17 | result.real = c1.real + c2.real; 18 | result.imag = c1.imag + c2.imag; 19 | 20 | printf("%.2lf + %.2lfi", result.real, result.imag); 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Learning/5. Structs/addingMass-with-struct.c: -------------------------------------------------------------------------------- 1 | // Add 2 masses with different units using struct 2 | 3 | #include 4 | 5 | typedef struct Mass { 6 | int stone; 7 | int pounds; 8 | } mass; 9 | 10 | int main(){ 11 | 12 | mass m1; 13 | mass m2; 14 | mass result; 15 | 16 | printf("Enter first distances\n"); 17 | printf("Enter stone and pounds: "); 18 | scanf("%d %d", &m1.stone, &m1.pounds); 19 | 20 | printf("Enter second distances\n"); 21 | printf("Enter stone and pounds: "); 22 | scanf("%d %d", &m2.stone, &m2.pounds); 23 | 24 | result.stone = m1.stone + m2.stone; 25 | result.pounds = m1.pounds + m2.pounds; 26 | 27 | result.stone = result.stone + (result.pounds / 14); 28 | result.pounds = result.pounds % 14; 29 | 30 | printf("Sum is %d stone and %d pounds", result.stone, result.pounds); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Learning/5. Structs/area-rectangle.c: -------------------------------------------------------------------------------- 1 | // area of rectangle 2 | 3 | #include 4 | 5 | struct rectangle{ 6 | int length; 7 | int width; 8 | }; 9 | 10 | void findArea(struct rectangle rec1) { 11 | int area = rec1.length * rec1.width; 12 | printf("Area: %d", area); 13 | } 14 | 15 | int main() { 16 | 17 | struct rectangle rec = {.length = 4, .width = 2}; 18 | 19 | findArea(rec); 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Learning/5. Structs/perimeter-rectangle.c: -------------------------------------------------------------------------------- 1 | // perimiter of rectangle 2 | 3 | #include 4 | 5 | struct Rectangle { 6 | int length; 7 | int breadth; 8 | }; 9 | 10 | void findPerimeter(struct Rectangle rec1){ 11 | int perimeter = 2*rec1.length + 2*rec1.breadth; 12 | printf("%d", perimeter); 13 | } 14 | 15 | int main(){ 16 | struct Rectangle rec1; 17 | scanf("%d %d ", &rec1.length, &rec1.breadth); 18 | 19 | findPerimeter(rec1); 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Learning/5. Structs/print-struct-values.c: -------------------------------------------------------------------------------- 1 | // Print values of struct 2 | 3 | #include 4 | 5 | struct Employee{ 6 | int age; 7 | char name[50]; 8 | }; 9 | 10 | 11 | int main() { 12 | 13 | struct Employee employee1; 14 | 15 | scanf("%d ", &employee1.age); 16 | 17 | fgets(employee1.name, sizeof(employee1.name), stdin); 18 | 19 | printf("%s", employee1.name); 20 | printf("%d", employee1.age); 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Learning/5. Structs/store-player-info.c: -------------------------------------------------------------------------------- 1 | // Store player info 2 | 3 | #include 4 | 5 | struct Player { 6 | char name[50]; 7 | int strength; 8 | double stamina; 9 | } player1; 10 | 11 | int main() { 12 | 13 | printf("Enter name: "); 14 | fgets(player1.name, sizeof(player1.name), stdin); 15 | 16 | printf("Enter strength: "); 17 | scanf(" %d", &player1.strength); 18 | 19 | printf("Enter stamina: "); 20 | scanf(" %lf", &player1.stamina); 21 | 22 | 23 | // print student info 24 | printf("Name: %s", player1.name); 25 | printf("Strength: %d\n", player1.strength); 26 | printf("Stamina: %.2lf", player1.stamina); 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Learning/5. Structs/struct-functionArgument.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct player{ 4 | char name[50]; 5 | int age; 6 | }; 7 | 8 | void display(struct player p1) { 9 | printf("Name: %s\n", p1.name); 10 | printf("Age: %d", p1.age); 11 | } 12 | 13 | int main() { 14 | 15 | struct player player1 = {.name = "Dima", .age = 26}; 16 | 17 | display(player1); 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Learning/5. Structs/structs&-pointers.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef struct Person { 4 | int age; 5 | double salary; 6 | } person; 7 | 8 | int main() { 9 | 10 | person person1 = {.age = 26, .salary = 30000}; 11 | person* pt; 12 | pt = &person1; 13 | 14 | printf("Age: %d\n", pt -> age); 15 | printf("Salary: %.2lf", pt -> salary); 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /Learning/5. Structs/subtract-complex-numbers.c: -------------------------------------------------------------------------------- 1 | // Subtract two complex numbers 2 | 3 | #include 4 | 5 | typedef struct Complex { 6 | double real; 7 | double imag; 8 | } complex; 9 | 10 | int main() { 11 | 12 | complex c1, c2, result; 13 | 14 | scanf("%lf %lf ", &c1.real, &c1.imag); 15 | 16 | scanf("%lf %lf ", &c2.real, &c2.imag); 17 | 18 | result.real = c1.real - c2.real; 19 | result.imag = c1.imag - c2.imag; 20 | 21 | printf("%.2lf + %.2lfi", result.real, result.imag); 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Learning/6. Additional topics/READme: -------------------------------------------------------------------------------- 1 | Hi there! Here you will find basic code as I learn C programming. We all have to start somewhere :) 2 | -------------------------------------------------------------------------------- /Learning/6. Additional topics/aoc-with-macro.c: -------------------------------------------------------------------------------- 1 | // area of circle using macros 2 | 3 | #include 4 | #define PI 3.1415 5 | 6 | int main() { 7 | 8 | double radius; 9 | scanf("%lf", &radius); 10 | 11 | double area = PI * (radius * radius); 12 | printf("%.2lf", area); 13 | 14 | return 0; 15 | } -------------------------------------------------------------------------------- /Learning/6. Additional topics/array-increase-realloc.c: -------------------------------------------------------------------------------- 1 | // Increases size of array dynamically using realloc 2 | 3 | #include 4 | #include 5 | 6 | int main() { 7 | 8 | int* marks; 9 | 10 | marks = (int*)malloc(3 * sizeof(int)); 11 | 12 | for (int i = 0; i < 3; i++) { 13 | scanf("%d", (marks + i)); 14 | } 15 | for (int i = 0; i < 3; i++) { 16 | printf("%d\n", *(marks + i)); 17 | } 18 | 19 | marks = realloc(marks, 5 * sizeof(int)); 20 | 21 | marks[3] = 13; 22 | marks[4] = 15; 23 | 24 | for (int i = 0; i < 5; i++) { 25 | printf("%d\n", *(marks + i)); 26 | } 27 | 28 | return 0; 29 | } 30 | 31 | /* 32 | How program works: 33 | Source code - Preprocess - Compiler - Exe file - Output 34 | */ -------------------------------------------------------------------------------- /Learning/6. Additional topics/enumeration-switch-case.c: -------------------------------------------------------------------------------- 1 | // ENUMERATION EXAMPLE USING SWITCH/CASE STATEMENT 2 | #include 3 | 4 | enum Size { 5 | Small, 6 | Medium, 7 | Large 8 | } size; 9 | 10 | int main() { 11 | 12 | size = Small; 13 | 14 | switch(type){ 15 | case Small: 16 | printf("SMALL"); 17 | break; 18 | case Medium: 19 | printf("MEDIUM"); 20 | break; 21 | case Large: 22 | printf("LARGE"); 23 | break; 24 | 25 | } 26 | 27 | return 0; 28 | } 29 | 30 | // Enum variables can only take one out of a small set of possible values. 31 | // If we know all possible values a variable can take, it is better to use enums. 32 | // This improves the readability of the code. -------------------------------------------------------------------------------- /Learning/6. Additional topics/malloc.c: -------------------------------------------------------------------------------- 1 | // Dynamic memory allocation 2 | #include 3 | #include 4 | 5 | int main() { 6 | 7 | int n; 8 | int* marks; 9 | 10 | scanf("%d", &n); 11 | 12 | marks = (int*) malloc(n * sizeof(int)); 13 | 14 | for (int i = 0; i < n; i++){ 15 | scanf("%d", marks + i); 16 | } 17 | 18 | for (int i = 0; i < n; ++i) { 19 | printf("%d\n", *(marks + i)); 20 | } 21 | 22 | free(marks); 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Learning/6. Additional topics/nested-loop.c: -------------------------------------------------------------------------------- 1 | // nested loops 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | int n; 8 | scanf("%d", &n); 9 | 10 | for (int i = 0; i < n; i++) { 11 | for (int j = 0; j < 2; j++) { 12 | printf("*\n"); 13 | } 14 | } 15 | return 0; 16 | } 17 | 18 | /* 19 | Takes user input to output stars using nested loop. 20 | */ -------------------------------------------------------------------------------- /Learning/6. Additional topics/prime-numbers.c: -------------------------------------------------------------------------------- 1 | //Prime Numbers Between 1 and 100 2 | 3 | #include 4 | 5 | int primeCheck(int number) { 6 | for (int i = 2; i < number; ++i) { //iteration from 2 to (number - 1) because prime number can only divide by 1 or itself 7 | if (number % i == 0) { 8 | return 0; 9 | } 10 | } 11 | return 1; 12 | } 13 | 14 | int main() { 15 | 16 | for (int i = 1; i <= 100; ++i) { 17 | int prime = primeCheck(i); 18 | if (prime) { 19 | printf("%d\n", i); 20 | } 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Learning/6. Additional topics/subtract-2-matrices.c: -------------------------------------------------------------------------------- 1 | // Subtract 2 matrices 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | // first matrix 8 | int A[3][2] = { 9 | {9, 8}, {12, 21}, {32, 36} 10 | }; 11 | 12 | // second matrix 13 | int B[3][2] = { 14 | {4, 5}, {10, 12}, {20, 19} 15 | }; 16 | 17 | // matrix to store the difference 18 | int difference[3][2]; 19 | 20 | // use nested loop to find the difference 21 | for (int i = 0; i < 3; i++) { 22 | for (int j = 0; j < 2; j++) { 23 | 24 | difference[i][j] = A[i][j] - B[i][j]; 25 | } 26 | } 27 | 28 | // print the difference matrix 29 | for (int i = 0; i < 3; i++) { 30 | for (int j = 0; j < 2; j++) { 31 | 32 | printf("%d ", difference[i][j]); 33 | } 34 | printf("\n"); 35 | } 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/1. Intro/READme: -------------------------------------------------------------------------------- 1 | Hello comrade! Here you will find coding challenges which are part of my proffesional certification in C :) 2 | -------------------------------------------------------------------------------- /Learning/7. Challenges/1. Intro/calculate-cost-price.c: -------------------------------------------------------------------------------- 1 | //calculating cost price given selling price and profit percentage. 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | double sellingPrice, profitPercent; 8 | 9 | scanf("%lf %lf", &sellingPrice, &profitPercent); 10 | 11 | double costPrice = (sellingPrice * 100.0) / (100 + profitPercent); //costPrice formula 12 | 13 | printf("%.2lf", costPrice); //result printed to 2 decimal places 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/1. Intro/calculate-profit.c: -------------------------------------------------------------------------------- 1 | //calculating profit given the cost price and selling price 2 | 3 | #include 4 | 5 | int main () { 6 | 7 | double costPrice, sellingPrice; 8 | 9 | scanf("%lf %lf", &costPrice, &sellingPrice); 10 | 11 | double profit = sellingPrice - costPrice; 12 | 13 | double profitPercent = (profit / costPrice) * 100; 14 | 15 | printf("%.2lf\n", profit); 16 | printf("%.2lf", profitPercent); 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/1. Intro/cm-to-ft.c: -------------------------------------------------------------------------------- 1 | // Program to find area of rectangle 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | double centimeter; 8 | 9 | scanf("%lf", ¢imeter); //get user input 10 | 11 | double feet = centimeter * 0.0328; //conversion 12 | 13 | printf("%lf", feet); //print result 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/1. Intro/cube-volume.c: -------------------------------------------------------------------------------- 1 | //find volume of cube 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | int length; 8 | 9 | scanf("%d", &length); 10 | 11 | int volume = length * length * length; 12 | 13 | printf("%d", volume); 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/1. Intro/divide-remainder.c: -------------------------------------------------------------------------------- 1 | //divide n chocolates among m people 2 | 3 | #include 4 | 5 | int main() { 6 | int chocolate; 7 | int children; 8 | 9 | scanf("%d\n%d", &chocolate, &children); 10 | 11 | int each = chocolate / children; //finds how much chocolate per child 12 | printf("%d\n", each); 13 | 14 | int remain = chocolate % children; //finds remainder 15 | printf("%d", remain); 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/1. Intro/fahrenheit-to-celsius.c: -------------------------------------------------------------------------------- 1 | // convert fahrenheit to celsius 2 | 3 | #include 4 | 5 | int main() { 6 | double fahrenheit; 7 | 8 | scanf("%lf", &fahrenheit); 9 | 10 | double celsius = (fahrenheit - 32) * 5 / 9; 11 | 12 | printf("%lf", celsius); 13 | 14 | return 0; 15 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/1. Intro/find-rectangle-area.c: -------------------------------------------------------------------------------- 1 | // Program to find area of rectangle 2 | 3 | #include 4 | 5 | int main() { 6 | int length; 7 | int breadth; 8 | 9 | scanf("%d\n", &length); //Gets user input 10 | scanf("%d", &breadth); 11 | 12 | int area = length * breadth; //calculates area with inputs 13 | 14 | printf("%d", area); //prints result of area 15 | 16 | return 0; 17 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/1. Intro/km-to-mi.c: -------------------------------------------------------------------------------- 1 | // convert kilometers to miles 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | double km; 8 | 9 | scanf("%lf", &km); 10 | 11 | double mi = km / 1.6; 12 | 13 | printf("%lf", mi); 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/1. Intro/max-handshakes.c: -------------------------------------------------------------------------------- 1 | //Maximum possible handshakes from N number of students 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | int students; 8 | 9 | scanf("%d", &students); 10 | 11 | int combo = (students * (students - 1)) / 2; //combo formula for calculation: 12 | //combination = (n * (n - 1)) / 2 13 | //n = total number of students 14 | 15 | printf("%d", combo); 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/1. Intro/pounds-to-kilos.c: -------------------------------------------------------------------------------- 1 | // convert pounds to kilograms 2 | 3 | #include 4 | 5 | int main() { 6 | double pound; 7 | 8 | scanf("%lf", £); 9 | 10 | double kg = pound * 0.453592; 11 | 12 | printf("%lf", kg); 13 | 14 | return 0; 15 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/1. Intro/swap-2-numbers.c: -------------------------------------------------------------------------------- 1 | //swap two numbers 2 | 3 | #include 4 | 5 | int main() { 6 | int x; 7 | int y; 8 | 9 | scanf("%d", &x); 10 | scanf("%d", &y); 11 | 12 | int temp; 13 | 14 | temp = x; 15 | x = y; 16 | y = temp; 17 | 18 | printf("%d\n%d", x, y); 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/2. Control Flow/READme: -------------------------------------------------------------------------------- 1 | Hello comrade! Here you will find coding challenges which are part of my proffesional certification in C :) 2 | -------------------------------------------------------------------------------- /Learning/7. Challenges/2. Control Flow/add-numbers-condition.c: -------------------------------------------------------------------------------- 1 | // Program to find the sum of all the user inputs until the user enters 0. 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | int sum = 0; 8 | int number; 9 | 10 | while (1) { // run a while loop that breaks only if input value is 0 11 | // if input value is other than 0, add it to sum 12 | scanf("%d", &number); 13 | 14 | if (number == 0) { 15 | break; 16 | } 17 | else { 18 | sum = sum + number; 19 | } 20 | } 21 | 22 | printf("%d", sum); // print the sum 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/2. Control Flow/calculate-employee-bonus.c: -------------------------------------------------------------------------------- 1 | // Program to find the bonus of employees based on their year of service. 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | double salary; 8 | double serviceYear; 9 | double bonus; 10 | 11 | scanf("%lf %lf", &salary, &serviceYear); // get input value for salary and serviceYear 12 | 13 | if (serviceYear > 5) { // check if serviceYear is greater than 5 14 | 15 | bonus = (5 * salary) / 100; // compute the bonus and print it 16 | printf("%lf", bonus); 17 | } 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/2. Control Flow/calculator.c: -------------------------------------------------------------------------------- 1 | // Program to create a two-value-input calculator. 2 | #include 3 | 4 | int main() { 5 | 6 | char op; // get input value for operator 7 | scanf("%c", &op); 8 | 9 | double first, second; // get input value of first and second 10 | scanf("%lf %lf", &first, &second); 11 | 12 | switch(op) { 13 | 14 | case '+': // perform operations based on the value of op 15 | printf("%.1lf", first + second); 16 | break; 17 | 18 | case '-': 19 | printf("%.1lf", first - second); 20 | break; 21 | 22 | case '*': 23 | printf("%.1lf", first * second); 24 | break; 25 | 26 | case '/': 27 | printf("%.1lf", first / second); 28 | break; 29 | 30 | default: // if op doesn't match with any of the cases 31 | printf("Error!"); 32 | } 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/2. Control Flow/can-vote.c: -------------------------------------------------------------------------------- 1 | // Program to check if a person can vote or not based on the age. 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | int age; // get input value for the age 8 | scanf("%d", &age); 9 | 10 | if (age >= 18) { // check if the age is greater than or equal to 18 11 | printf("Can Vote"); 12 | } 13 | 14 | else { 15 | printf("Cannot Vote"); 16 | } 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/2. Control Flow/check-abundant-number.c: -------------------------------------------------------------------------------- 1 | // Program to check if a number is an Abundant Number. 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | int number; // get input for number 8 | scanf("%d", &number); 9 | 10 | int sum = 0; // variable to store sum of all divisors 11 | 12 | for (int i = 1; i < number; ++i) { // run loop to find the divisor of number 13 | 14 | if (number % i == 0) { // check if i is divisor of number 15 | 16 | sum = sum + i; // if true, add i to sum 17 | } 18 | } 19 | 20 | if (sum > number) { // check if sum is greater than number 21 | printf("Abundant Number"); 22 | } 23 | else { 24 | printf("Not an Abundant Number"); 25 | } 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/2. Control Flow/check-armstrong-number.c: -------------------------------------------------------------------------------- 1 | // Program to check if a number is armstrong or not. 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | int number; // get input value for number 8 | scanf("%d", &number); 9 | 10 | int originalNumber = number; // assign the number to another variable originalNumber 11 | 12 | int sum = 0; // sum variable to store the sum of cubes 13 | int remainder; // variable to access each digit of the number 14 | 15 | while (originalNumber != 0) { // loop to find the cubes of each digit 16 | 17 | remainder = originalNumber % 10; // use originalNumber % 10 to get the last digit 18 | sum = sum + (remainder * remainder * remainder); // add the cube of the last digit to sum 19 | originalNumber = originalNumber / 10; // use originalNumber / 10 to change the value of originalNumber 20 | } 21 | 22 | if (sum == number) { // check if sum is equal to number 23 | printf("Armstrong Number"); 24 | } 25 | else { 26 | printf("Not an Armstrong Number"); 27 | } 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/2. Control Flow/check-leap-year.c: -------------------------------------------------------------------------------- 1 | // Program to check if a year is a leap year or not. 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | int year; // get input value for year 8 | scanf("%d", &year); 9 | 10 | if (year % 400 == 0) { // check if year is divisible by 400 11 | printf("Leap Year"); 12 | } 13 | else if (year % 4 == 0 && year % 100 != 0) { // check if the year is divisible by 4 and doesn't end with 00 14 | printf("Leap Year"); 15 | } 16 | else { // otherwise print Not a Leap Year 17 | printf("Not a Leap Year"); 18 | } 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/2. Control Flow/check-negative-number.c: -------------------------------------------------------------------------------- 1 | // Program to check if a number is negative or not. 2 | 3 | #include 4 | 5 | 6 | int main() { 7 | 8 | int number; // create a variable and get input value 9 | scanf("%d", &number); 10 | 11 | if (number < 0) { // check if the number is negative or positive or zero 12 | printf("Negative Number"); 13 | } 14 | 15 | else if (number > 0) { 16 | printf("Positive Number"); 17 | } 18 | 19 | else { 20 | printf("Zero Number"); 21 | } 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/2. Control Flow/check-prime-number.c: -------------------------------------------------------------------------------- 1 | // Program to check whether a number is prime or not. 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | int number; // get input value for number 8 | scanf("%d", &number); 9 | 10 | int flag = 0; // create flag variable with value 0 11 | 12 | for (int i = 2; i < number; i++) { // run loop from 2 to number - 1 13 | if (number % i == 0) { // check if number is divisible by number between 2 to number - 1 14 | flag = 1; 15 | break; 16 | } 17 | else { 18 | flag = 0; 19 | } 20 | } 21 | 22 | 23 | if (flag == 1) { // check if flag is 1 and print either Not a Prime Number or Prime Number 24 | printf("Not a Prime Number"); 25 | } 26 | else { 27 | printf("Prime Number"); 28 | } 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/2. Control Flow/compute-student-grades.c: -------------------------------------------------------------------------------- 1 | // Program to find the grade of a student based on the marks. 2 | #include 3 | 4 | int main() { 5 | double marks; 6 | 7 | scanf("%lf", &marks); // get input value for marks 8 | 9 | if (marks >= 90) { // check the value of marks and assign grade based on the value 10 | printf("A"); 11 | } 12 | else if (marks >= 80) { 13 | printf("B"); 14 | } 15 | else if (marks >= 70) { 16 | printf("C"); 17 | } 18 | else { 19 | printf("D"); 20 | } 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/2. Control Flow/count-digits.c: -------------------------------------------------------------------------------- 1 | // Program to count the number of digits in an integer. 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | int number; 8 | int count = 0; 9 | 10 | scanf("%d", &number); // get input value for number 11 | 12 | while (number != 0) { // loop through the integer to calculate the number of digits 13 | count = count + 1; 14 | number = number / 10; 15 | } 16 | 17 | printf("%d", count); // print the number of digits 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/2. Control Flow/factor-of-number.c: -------------------------------------------------------------------------------- 1 | // Program to find the factors of a number. 2 | #include 3 | 4 | int main() { 5 | 6 | int number; // get input value for number 7 | scanf("%d", &number); 8 | 9 | for (int i = 1; i < number; i++) { // run a loop from i = 1 to i < number 10 | 11 | if(number % i == 0) { // check if number is divisible by i 12 | printf("%d\n", i); 13 | } 14 | } 15 | 16 | return 0; 17 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/2. Control Flow/find-factorial.c: -------------------------------------------------------------------------------- 1 | // Program to find the factorial of a number. 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | int number; 8 | 9 | scanf("%d", &number); // get input value for number 10 | 11 | int factorial = 1; 12 | 13 | for (int i = 1; i <= number; i++) { // run a for loop from i = 1 to i <= number 14 | factorial = factorial * i; // inside loop multiply factorial with i 15 | } 16 | 17 | printf("%d", factorial); // print factorial 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/2. Control Flow/find-youngest-brother.c: -------------------------------------------------------------------------------- 1 | // Program to find the youngest brother among 3 based on their ages. 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | int jack; 8 | int roman; 9 | int johnny; 10 | 11 | scanf("%d %d %d", &jack, &roman, &johnny); // get age input for jack, roman and johnny 12 | 13 | if (jack < roman && jack < johnny) { // use if else to find the youngest brother 14 | printf("Jack"); 15 | } 16 | else if (roman < jack && roman < johnny) { 17 | printf("Roman"); 18 | } 19 | else { 20 | printf("Johnny"); 21 | } 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/2. Control Flow/greatest-number-factor.c: -------------------------------------------------------------------------------- 1 | // Program to print the greatest factor of a number besides itself using a loop. 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | int number; // get input value for number 8 | scanf("%d", &number); 9 | 10 | for (int i = number - 1; i > 0; i--) { // run the loop from number - 1 to 1 11 | 12 | if (number % i == 0) { // check if number is divisible by i 13 | printf("%d", i); // if true, print the value of i and break the loop 14 | break; 15 | } 16 | 17 | } 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/2. Control Flow/greatest-number-multiple.c: -------------------------------------------------------------------------------- 1 | // Program to find the greatest multiple of a number below 100. 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | int number; // get input value for number 8 | scanf("%d", &number); 9 | 10 | for (int i = 100; i >= 1; i--) { // run a loop from i = 100 to 1 11 | 12 | if (i % number == 0) { // check if i is divisible by number 13 | printf("%d", i); // if yes, print i and exit loop 14 | break; 15 | } 16 | } 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/2. Control Flow/harshad-number.c: -------------------------------------------------------------------------------- 1 | // Program to check if a number is a Harshad Number. 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | int number; // get input for number 8 | scanf("%d", &number); 9 | 10 | int sum = 0; // variable to store the sum of digits 11 | 12 | int num = number; // use loop to access each digit and find sum of digits 13 | while (num != 0) { 14 | 15 | int digit = num % 10; // access each digit using num % 10 16 | 17 | sum = sum + digit; // add each digit to sum 18 | 19 | num = num / 10; // change value of num 20 | } 21 | if (number % sum == 0) { // check if number is perfectly divided by sum 22 | printf("Harshad Number"); 23 | } 24 | else { 25 | printf("Not a Harshad Number"); 26 | } 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/2. Control Flow/input-print.c: -------------------------------------------------------------------------------- 1 | // Program to print FizzBuzz based on user input. 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | int number; // get input value of number 8 | scanf("%d", &number); 9 | 10 | if (number % 3 == 0 && number % 5 == 0) { // check if number is multiple of both 3 and 5 11 | printf("FizzBuzz"); 12 | } 13 | else if (number % 3 == 0) { // check if number is multiple of 3 14 | printf("Fizz"); 15 | } 16 | else if (number % 5 == 0) { // check if number is multiple of 5 17 | printf("Buzz"); 18 | } 19 | else { // else print the value of number 20 | printf("%d", number); 21 | } 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/2. Control Flow/internal-triangle-angles.c: -------------------------------------------------------------------------------- 1 | // Program to check if 3 values are internal angles of a triangle. 2 | 3 | #include 4 | 5 | int main() { 6 | int a, b, c; 7 | 8 | scanf("%d %d %d", &a, &b, &c); // get 3 input values 9 | 10 | int sum = a + b + c; // find the sum 11 | 12 | if (sum == 180) { // check if sum is equal to 180 and print either true or false 13 | printf("true"); 14 | } 15 | else { 16 | printf("false"); 17 | } 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/2. Control Flow/is-char-alphabet.c: -------------------------------------------------------------------------------- 1 | // Program to check if a character is an alphabet or not. 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | char alphabet; // get character input 8 | scanf("%c", &alphabet); 9 | 10 | // check if character is in the range 'a' to 'z' or 'A' to 'Z' 11 | if ((alphabet >= 'a' && alphabet <= 'z') || (alphabet >= 'A' && alphabet <= 'Z')) { 12 | printf("Alphabet"); 13 | } 14 | else { 15 | printf("Not an Alphabet"); 16 | } 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/2. Control Flow/natural-num-sum.c: -------------------------------------------------------------------------------- 1 | // Program to find the sum of N natural numbers. 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | int n; // create a variable n 8 | 9 | int sum = 0; // variable to store the value of sum 10 | 11 | scanf("%d", &n); // take input value for n 12 | 13 | for (int i = 1; i <= n; ++i) { // loop runs from 1 to n 14 | sum = i + sum; // add i to sum 15 | } 16 | 17 | printf("%d", sum); // print the value of sum 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/2. Control Flow/number-guessing-game.c: -------------------------------------------------------------------------------- 1 | // Program to check if a number guessed by the user is correct or not. 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | int correctNumber = 18; // create a variable named correctNumber 8 | 9 | int guessedNumber; // get input for guessedNumber variable 10 | scanf("%d", &guessedNumber); 11 | 12 | if (guessedNumber < 100 && guessedNumber > 18) { // use if-else statement to check if the guess is correct or not 13 | printf("Wrong, your guess is larger"); 14 | } 15 | else if (guessedNumber > 0 && guessedNumber < 18) { 16 | printf("Wrong, your guess is smaller"); 17 | } 18 | else if (guessedNumber == correctNumber) { 19 | printf("Your guess is correct"); 20 | } 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/2. Control Flow/odd-or-even.c: -------------------------------------------------------------------------------- 1 | // Program to check if a number is even or odd. 2 | 3 | #include 4 | 5 | 6 | int main() { 7 | 8 | int number; // create the number variable 9 | 10 | scanf("%d", &number); // get input value for number 11 | 12 | if (number % 2 == 0) { // check if the number is even or odd 13 | printf("Even"); 14 | } 15 | 16 | else { 17 | printf("Odd"); 18 | } 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/2. Control Flow/power-of-number.c: -------------------------------------------------------------------------------- 1 | // Program to find the power of a number. 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | int number; 8 | int power; 9 | 10 | scanf("%d %d", &number, &power); // get input value for number and power 11 | 12 | int result = 1; // variable to store the power of a number 13 | 14 | for(int i = 1; i <= power; i++) { // run a loop from 1 to power 15 | 16 | result = number * result; // inside loop multiply number with result 17 | } 18 | 19 | printf("%d", result); // print the result 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/2. Control Flow/print-3-sentences.c: -------------------------------------------------------------------------------- 1 | // Program to print a sentence 3 times using a loop. 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | for(int i = 0; i < 3; i++) { // run a for loop from i = 0 to i < 3 8 | printf("C Programming is fun\n"); 9 | } 10 | 11 | return 0; 12 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/2. Control Flow/print-multiplication-table.c: -------------------------------------------------------------------------------- 1 | // Program to print a multiplication table of a number. 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | int number; // create a number variable 8 | 9 | scanf("%d", &number); // take input value for number 10 | 11 | int count = 1; // use while loop to run from 1 to 10 12 | while (count <= 10) { // print multiplication table inside the while loop 13 | int product = number * count; 14 | printf("%d * %d = %d\n", number, count, product); 15 | count = count + 1; 16 | } 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/2. Control Flow/print-with-condition.c: -------------------------------------------------------------------------------- 1 | // Program to print all numbers from 0 to 10 except the multiples of 3. 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | for (int i = 0; i <= 10; i++) { // loop from 1 to 10 8 | if (i % 3 == 0) { // inside loop check whether i is divisible by 3 9 | continue; 10 | } 11 | else { 12 | printf("%d\n", i); 13 | } 14 | } 15 | 16 | return 0; 17 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/2. Control Flow/sum-of-digits.c: -------------------------------------------------------------------------------- 1 | // Program to find the sum of all the digits of a number. 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | int number; // get input value for number 8 | scanf("%d", &number); 9 | 10 | int num = number; 11 | int digit; 12 | int sum = 0; 13 | 14 | while (num != 0) { // run while loop to access each digit of number 15 | 16 | digit = num % 10; // use num % 10 to find digit of each number 17 | 18 | sum = sum + digit; // add each digit to sum 19 | 20 | num = num / 10; 21 | 22 | } 23 | printf("%d", sum); // print the sum 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/3. Functions/GCD-two-numbers.c: -------------------------------------------------------------------------------- 1 | // Program to compute the area of a circle using a function. 2 | #include 3 | 4 | int findGCD(int num1, int num2) { // create function findGCD with parameters: num1 and num2 5 | 6 | int gcd = 1; // compute the gcd of two numbers and return it 7 | 8 | for (int i = num1; i >= 1; i--) { 9 | for (int i = num2; i >= 1; i--) { 10 | if (num1 % i == 0 && num2 % i == 0) { 11 | 12 | gcd = i; 13 | 14 | return gcd; 15 | } 16 | } 17 | } 18 | } 19 | 20 | int main() { 21 | 22 | int x, y; // get input values for x and y 23 | scanf("%d %d", &x, &y); 24 | 25 | int gcd = findGCD(x, y); // call findGCD() with arguments x and y 26 | 27 | printf("%d", gcd); // print the returned value 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/3. Functions/LCM-two-numbers.c: -------------------------------------------------------------------------------- 1 | // Program to find LCM (Lowest Common Multiple) of two numbers using a function. 2 | 3 | #include 4 | 5 | int findLCM(int num1, int num2) { // create function findLCM() with parameters num1 and num2 6 | 7 | int lcm; // compute lcm of two values and return it 8 | 9 | if (num1 >= num2) { 10 | lcm = num1; 11 | } 12 | else { 13 | lcm = num2; 14 | } 15 | 16 | while (1) { 17 | lcm = lcm + 1; 18 | if (lcm % num1 == 0 && lcm % num2 == 0) { 19 | break; 20 | } 21 | } 22 | return lcm; 23 | } 24 | 25 | int main() { 26 | 27 | int x, y; // get input value for x and y 28 | scanf("%d %d", &x, &y); 29 | 30 | int lcm = findLCM(x, y); // call findLCM() with arguments x and y 31 | 32 | printf("%d", lcm); // print the returned value 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/3. Functions/READme: -------------------------------------------------------------------------------- 1 | Hello comrade! Here you will find coding challenges which are part of my proffesional certification in C :) 2 | -------------------------------------------------------------------------------- /Learning/7. Challenges/3. Functions/area-of-circle.c: -------------------------------------------------------------------------------- 1 | // Program to compute the area of a circle using a function. 2 | #include 3 | 4 | double computeArea (double radius, double pi) { // create computeArea() function that accepts radius and pi as parameters 5 | double area = pi * radius * radius; // computes the area and return it 6 | return area; 7 | } 8 | 9 | int main() { 10 | 11 | double pi = 3.14; 12 | 13 | double radius; // get input value for radius 14 | scanf("%lf", &radius); 15 | 16 | double area = computeArea(radius, pi); // call computeArea() with arguments radius and pi 17 | 18 | printf("%.2lf", area); // print returned value up to 2 decimal points 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/3. Functions/armstrong-numbers.c: -------------------------------------------------------------------------------- 1 | // Program to display armstrong numbers between two intervals. 2 | 3 | #include 4 | 5 | int checkArmstrong(int number) { // -------- function that checks if a number is armstrong 6 | 7 | int originalNumber = number; // -------- assign the number to another variable originalNumber 8 | int sum = 0; // -------- sum variable to store the sum of cubes 9 | int remainder; // -------- variable to access each digit of the number 10 | 11 | while (originalNumber != 0) { // loop to find the cubes of each digit 12 | remainder = originalNumber % 10; // use originalNumber % 10 to get the last digit 13 | sum = sum + (remainder * remainder * remainder); // add the cube of the last digit to sum 14 | originalNumber = originalNumber / 10; // use originalNumber / 10 to change the value of originalNumber 15 | } 16 | if (sum == number) { 17 | printf("%d\n", sum); // ------------- prints the number if it is an armstrong number 18 | } 19 | } 20 | 21 | int main() { 22 | 23 | int x, y; // ---------------- get input values for x and y 24 | scanf("%d %d", &x, &y); 25 | 26 | while (x <= y) { // ------------ run loop from x to y 27 | checkArmstrong(x); // -------- call checkArmstrong() for each value from x 28 | x = x + 1; 29 | } 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/3. Functions/calculate-simple-interest.c: -------------------------------------------------------------------------------- 1 | // Program to calculate simple interest using a function 2 | 3 | #include 4 | 5 | double simpleInterest (double principal, double time, double rate) { // double data type function with 3 arguments 6 | double simpleInterest = (principal * time * rate) / 100; // formula to compute simple interest 7 | return simpleInterest; // returns value 8 | } 9 | 10 | int main() { 11 | 12 | double principal, time, rate; // ------- take input for principal, time, and rate 13 | scanf("%lf %lf %lf", &principal, &time, &rate); 14 | 15 | double interest = simpleInterest(principal, time, rate); // call simpleInterest() with arguments: principal, time and rate 16 | 17 | printf("%.2lf", interest); // --- print simple interest 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/3. Functions/check-palindrome-number.c: -------------------------------------------------------------------------------- 1 | // Program to check if a number is palindrome or not. 2 | 3 | #include 4 | 5 | int findReverse(int number) { // function to find the reverse number 6 | int reversed = 0; 7 | int remainder; 8 | 9 | while (number != 0) { // ----------- run a while loop until number is equal to 0 10 | remainder = number % 10; // ------- use originalNumber % 10 to get the last digit 11 | number = number / 10; // -------- use originalNumber / 10 to change the value of originalNumber 12 | reversed = reversed * 10 + remainder; // re-arranges number back to front 13 | } 14 | 15 | return reversed; 16 | } 17 | 18 | int main() { 19 | 20 | int number; 21 | 22 | scanf("%d", &number); // get input value for number 23 | 24 | int result = findReverse(number); // call findReverse() with argument number 25 | 26 | if (number == result) { // --- check if number is equal to the returned value 27 | printf("Palindrome Number"); 28 | } 29 | else { 30 | printf("Not a Palindrome Number"); 31 | } 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/3. Functions/check-perfect-number.c: -------------------------------------------------------------------------------- 1 | // Program to check if a number is a Perfect Number. 2 | 3 | #include 4 | 5 | int findSum(int number) { 6 | int sum = 0; 7 | 8 | for (int i = 1; i < number; i++) { // run a for loop to find the sum of all divisors 9 | if (number % i == 0) { // if number perfectly divides 10 | sum = sum + i; // add said number to sum 11 | } 12 | } 13 | 14 | return sum; 15 | } 16 | 17 | int main() { 18 | 19 | int number; // ------------- get input value for number 20 | scanf("%d", &number); 21 | 22 | int result = findSum(number); // call the findSum() function with argument number 23 | 24 | if (result == number) { // if result is equal to number, print Perfect Number 25 | printf("Perfect Number"); 26 | } 27 | else { 28 | printf("Not a Perfect Number"); // otherwise, print Not a Perfect Number. 29 | } 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/3. Functions/prime-numbers.c: -------------------------------------------------------------------------------- 1 | // Program to find all the prime numbers between two intervals using a function. 2 | 3 | #include 4 | 5 | int isPrime (int number) { // ----------- create a function that checks if a number is prime or not 6 | 7 | int flag = 0; // ------------------- create flag variable with value 0 8 | 9 | for (int i = 2; i < number; i++) { 10 | if (number % i == 0) { // --- run loop from 2 to number - 1 11 | flag = 1; // ------- check if number is divisible by number between 2 to number - 1 12 | break; 13 | } 14 | else { 15 | flag = 0; 16 | } 17 | } 18 | if (flag == 0) { // ------- check if flag is 0 which is condition isolated for prime number 19 | printf("%d\n", number); // --- print prime number and add new line 20 | } 21 | } 22 | 23 | int main() { 24 | 25 | int x, y; 26 | scanf("%d %d", &x, &y); 27 | 28 | for (int i = x; i <= y; ++i) { // run the loop from x to y 29 | isPrime(i); // for each iteration of loop call isPrime() 30 | } 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/3. Functions/self-dividing-number.c: -------------------------------------------------------------------------------- 1 | // Program to check if a number is a self-dividing number. 2 | 3 | #include 4 | 5 | int checkSelfDivide(int number) { 6 | int num = number; 7 | int digit; 8 | 9 | while (num !=0) { 10 | 11 | digit = num % 10; // --- access each digit of num 12 | 13 | if (num % digit != 0) { // check if any digit doesn't divide the number 14 | return 0; // -- return 0, if true 15 | } 16 | 17 | num = num / 10; // --- change the value of num by using num / 10 18 | 19 | } 20 | 21 | return 1; // ----------- if all digit divides the number 22 | 23 | } 24 | 25 | int main() { 26 | 27 | int number; 28 | 29 | scanf("%d", &number); // get input value for number 30 | 31 | int result = checkSelfDivide(number); // call checkSelfDivide() function 32 | 33 | if (result == 1) { // check the returned value is 1 34 | printf("Self Dividing Number"); 35 | } 36 | else { 37 | printf("Not a Self Dividing Number"); 38 | } 39 | 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/4. Arrays/READme: -------------------------------------------------------------------------------- 1 | Hello comrade! Here you will find coding challenges which are part of my proffesional certification in C :) 2 | -------------------------------------------------------------------------------- /Learning/7. Challenges/4. Arrays/compute-standard-deviation.c: -------------------------------------------------------------------------------- 1 | // Program to compute Standard Deviation 2 | 3 | #include 4 | #include 5 | 6 | // function to compute standard deviation 7 | double calculateSD(double data[], int size) { 8 | 9 | if (size <= 0 || data == NULL) { // checks if input array valid or has at least one element 10 | return NAN; 11 | } 12 | 13 | double sum = 0.0; 14 | 15 | // find the sum of all the array elements 16 | for (int i = 0; i < size; i++) { 17 | sum += data[i]; 18 | } 19 | 20 | // compute the mean 21 | double mean = sum / size; // (sizeof(data) / sizeof(data[0]) finds array size too 22 | 23 | // find difference of each value and mean 24 | // compute the square of each difference 25 | // add all the squared difference 26 | double result = 0.0; 27 | double difference; 28 | double difference_squared; 29 | 30 | for (int i = 0; i < size; i++) { 31 | difference = mean - data[i]; 32 | difference_squared = pow(difference, 2); 33 | result += difference_squared; 34 | } 35 | 36 | // compute variance by dividing the above result by 5.0 37 | double variance = result / size; 38 | 39 | // compute the standard deviation by 40 | // calculation the square root of variance 41 | double sd = sqrt(variance); 42 | 43 | // check if the square root function returned an error 44 | if (isnan(sd)) { 45 | return NAN; 46 | } 47 | 48 | // return standard deviation 49 | return sd; 50 | } 51 | 52 | int main() { 53 | 54 | double numArray[5]; 55 | 56 | // get input array elements 57 | for (int i = 0; i < 5; i++) { 58 | scanf("%lf", &numArray[i]); 59 | } 60 | 61 | // call calculateSD with arguments numArray 62 | double sd = calculateSD(numArray, sizeof(numArray) / sizeof(numArray[0])); 63 | 64 | // print the returned value 65 | printf("%.3lf", sd); 66 | 67 | return 0; 68 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/4. Arrays/even-array-numbers.c: -------------------------------------------------------------------------------- 1 | // Program to print only the even numbers from an array. 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | int numbers[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 8 | 9 | int evenNumbers[10]; 10 | 11 | // variable to track position of evenNumbers 12 | int j = 0; 13 | 14 | // run a for loop to access each element of numbers 15 | for (int i = 0; i < 10; ++i) { 16 | 17 | // check if ith element of number is even 18 | // if true, add the element to the jth position of evenNumbers 19 | // increase value of j by 1 20 | if (numbers[i] % 2 == 0) { 21 | evenNumbers[j] = numbers[i]; 22 | j++; 23 | } 24 | } 25 | 26 | // run a for loop from 0 to j to print even numbers 27 | for (int i = 0; i < j; i++) { 28 | printf("%d\n", evenNumbers[i]); 29 | } 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/4. Arrays/multiply-array.c: -------------------------------------------------------------------------------- 1 | // Program to multiply each element of an array by 10. 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | // create an array 8 | int arr[5] = {1, 2, 3, 4, 5}; 9 | 10 | // create another array with the same size 11 | int arrCopy[5]; 12 | 13 | // multiply elements of first array by 10 and store them in second array 14 | for (int i = 0; i < 5; i++) { 15 | arrCopy[i] = 10 * arr[i]; 16 | } 17 | 18 | // print second array 19 | for (int i = 0; i < 5; i++) { 20 | printf("%d\n", arrCopy[i]); 21 | } 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/4. Arrays/player-mean-height.c: -------------------------------------------------------------------------------- 1 | // Program to find the mean height of players present in a football team. 2 | 3 | #include 4 | 5 | int main () { 6 | 7 | double heights[11]; 8 | 9 | // get input height 10 | for (int i = 0; i < 11; i++) { 11 | scanf("%lf\n", &heights[i]); 12 | } 13 | 14 | double sum = 0.0; 15 | 16 | // find the sum of all heights 17 | for (int i = 0; i < 11; i++) { 18 | sum += heights[i]; 19 | } 20 | 21 | // find the mean and print it 22 | double mean = sum / 11.0; 23 | printf("%.2lf", mean); 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/4. Arrays/smallest-array-element.c: -------------------------------------------------------------------------------- 1 | // Program to find the smallest element from an array. 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | // create an integer array of size 5 8 | int numbers[5]; 9 | 10 | // get input value for the array 11 | for (int i = 0; i < 5; i++) { 12 | scanf("%d", &numbers[i]); 13 | } 14 | 15 | // create a variable smallest and assign first element of numbers to it 16 | int smallest = numbers[0]; 17 | 18 | // run a for loop from i = 1 to i < 5 19 | // check if smallest is less than element at i 20 | for (int i = 1; i < 5; i++) { 21 | if (numbers[i] < smallest) { 22 | smallest = numbers[i]; 23 | } 24 | } 25 | 26 | // print smallest 27 | printf("%d", smallest); 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/4. Arrays/standard-decviation-improved.c: -------------------------------------------------------------------------------- 1 | // Program to compute Standard Deviation 2 | 3 | #include 4 | #include 5 | 6 | // function to compute standard deviation 7 | double calculateSD(double data[], int size) { 8 | 9 | if (size <= 0 || data == NULL) { // checks if input array valid or has at least one element 10 | return NAN; 11 | } 12 | 13 | double sum = 0.0; 14 | 15 | // find the sum of all the array elements 16 | for (int i = 0; i < size; i++) { 17 | sum += data[i]; 18 | } 19 | 20 | // compute the mean 21 | double mean = sum / size; // (sizeof(data) / sizeof(data[0]) finds array size too 22 | 23 | // find difference of each value and mean 24 | // compute the square of each difference 25 | // add all the squared difference 26 | double result = 0.0; 27 | double difference; 28 | double difference_squared; 29 | 30 | for (int i = 0; i < size; i++) { 31 | difference = mean - data[i]; 32 | difference_squared = pow(difference, 2); 33 | result += difference_squared; 34 | } 35 | 36 | // compute variance by dividing the above result by 5.0 37 | double variance = result / size; 38 | 39 | // compute the standard deviation by 40 | // calculation the square root of variance 41 | double sd = sqrt(variance); 42 | 43 | // check if the square root function returned an error 44 | if (isnan(sd)) { 45 | return NAN; 46 | } 47 | 48 | // return standard deviation 49 | return sd; 50 | } 51 | 52 | int main() { 53 | 54 | int size; 55 | printf("Enter the size of the array: \n"); 56 | scanf("%d", &size); 57 | 58 | double numArray[size]; 59 | 60 | // get input array elements 61 | for (int i = 0; i < size; i++) { 62 | printf("\nEnter element %d: ", i + 1); 63 | scanf("%lf", &numArray[i]); 64 | } 65 | 66 | // call calculateSD with arguments numArray 67 | double sd = calculateSD(numArray, size); 68 | 69 | // print the returned value 70 | printf("\n\nStandard deviation: %.3lf", sd); 71 | 72 | return 0; 73 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/4. Arrays/student-percentage-marks.c: -------------------------------------------------------------------------------- 1 | // Program to find the percentage obtained by a student. 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | double marks[8]; 8 | 9 | // get input value for obtained marks 10 | for (int i = 0; i < 8; i++) { 11 | scanf("%lf", &marks[i]); 12 | } 13 | 14 | int totalMarks = 800; 15 | double obtainedMarks = 0.0; 16 | 17 | // find the sum of obtained marks 18 | for (int i = 0; i < 8; i++) { 19 | obtainedMarks = obtainedMarks + marks[i]; 20 | } 21 | 22 | // find the percentage and print it 23 | double percentage = obtainedMarks / 8; 24 | printf("%.2lf", percentage); 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/5. Strings/READme: -------------------------------------------------------------------------------- 1 | Hello comrade! Here you will find coding challenges which are part of my proffesional certification in C :) 2 | -------------------------------------------------------------------------------- /Learning/7. Challenges/5. Strings/char-frequency.c: -------------------------------------------------------------------------------- 1 | // Program to find the total occurrence of a character in a string. 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | // take the string input 8 | char str[100]; 9 | fgets(str, sizeof(str), stdin); 10 | 11 | // take character input 12 | char ch; 13 | scanf("%c", &ch); 14 | 15 | int count = 0; 16 | 17 | // find the length of string 18 | int length = strlen(str); 19 | 20 | // run a for loop from i = 0 to i < length 21 | // to access each character of string 22 | for (int i = 0; i < length; i++) { 23 | // compare each character with the ch character 24 | // if both are equal, increase the value of count 25 | if (ch == str[i]) { 26 | count++; 27 | } 28 | } 29 | 30 | printf("%d", count); 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/5. Strings/chars-in-string.c: -------------------------------------------------------------------------------- 1 | // Program to count the total number of characters in a string. 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | char text[100]; 8 | fgets(text, sizeof(text), stdin); // get input string 9 | int count = 0; // variable to count number of characters 10 | 11 | // use while loop with condition 12 | // if first character of text is not equal to '\0' 13 | while (text[count] != '\0') { 14 | count++; 15 | } 16 | 17 | // print count 18 | printf("%d", count); 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/5. Strings/count-vowels.c: -------------------------------------------------------------------------------- 1 | // Program to count the number of vowels present in a string. 2 | 3 | #include 4 | 5 | int main() { 6 | 7 | char text[150]; 8 | 9 | // get input value for text using fgets() 10 | fgets(text, sizeof(text), stdin); 11 | 12 | // variable to count number of vowels 13 | int vowels = 0; 14 | 15 | // loop to access each character of text 16 | for (int i = 0; text[i] != '\0'; ++i) { 17 | 18 | // check if character at ith position is a vowel (lowercase or uppercase) 19 | if (text[i] == 'a' || text[i] == 'A' || text[i] == 'e' || text[i] == 'E' || text[i] == 'i' || text[i] == 'I' || text[i] == 'o' || text[i] == 'O' || text[i] == 'u' || text[i] == 'U') { 20 | vowels++; 21 | } 22 | } 23 | 24 | // print the value of vowel 25 | printf("%d", vowels); 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/5. Strings/find-longer-string.c: -------------------------------------------------------------------------------- 1 | // Program to find the longer string among two strings. 2 | 3 | #include 4 | 5 | // function to find the length 6 | int findLength(char text[100]) { 7 | int count = 0; 8 | 9 | // run a while loop to access each characters 10 | // inside loop, increase value of count 11 | while (text[count] != '\0') { 12 | count++; 13 | } 14 | 15 | return count; 16 | } 17 | 18 | int main() { 19 | 20 | char text1[100]; 21 | char text2[100];c 22 | 23 | scanf("%s %s", &text1, &text2); // get 2 string inputs 24 | 25 | findLength(text1); // find the length of two strings by calling findLength() 26 | findLength(text2); 27 | 28 | // compare the length of two strings 29 | // and print the string with the larger length 30 | if (findLength(text1) > findLength(text2)) { 31 | printf("%s", text1); 32 | } 33 | else if (findLength(text1) == findLength(text2)) { 34 | printf("Equal length"); 35 | } 36 | else { 37 | printf("%s", text2); 38 | } 39 | 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Learning/7. Challenges/5. Strings/join-two-strings.c: -------------------------------------------------------------------------------- 1 | // Program to find the total occurrence of a character in a string. 2 | 3 | #include 4 | #include 5 | 6 | int main() { 7 | char text1[100]; 8 | char text2[100]; 9 | char joined[200] = ""; // Initialize the joined string with an empty string 10 | 11 | // get string input using scanf 12 | scanf("%s", text1); 13 | scanf("%s", text2); 14 | 15 | // join two string using strcat and add space between them 16 | strcat(joined, text1); 17 | strcat(joined, " "); 18 | strcat(joined, text2); 19 | 20 | 21 | // print joined string 22 | printf("%s", joined); 23 | 24 | return 0; 25 | } 26 | 27 | /* OR 28 | 29 | #include 30 | #include 31 | 32 | int main() { 33 | char text1[100]; 34 | char text2[100]; 35 | 36 | // get string input using scanf 37 | scanf("%s", text1); 38 | scanf("%s", text2); 39 | 40 | // join two string using strcat and add space between them 41 | strcat(text1, " "); 42 | strcat(text1, text2); 43 | 44 | // print joined string 45 | printf("%s", text1); 46 | 47 | return 0; 48 | } 49 | 50 | */ -------------------------------------------------------------------------------- /Learning/7. Challenges/READme: -------------------------------------------------------------------------------- 1 | Hi there! Here you will find basic code as I learn C programming. We all have to start somewhere :) 2 | -------------------------------------------------------------------------------- /Projects/READme: -------------------------------------------------------------------------------- 1 | Projects here 2 | -------------------------------------------------------------------------------- /Projects/rock-paper-scissors.c: -------------------------------------------------------------------------------- 1 | // rock paper scissors 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | // returns either 'r', 'p', or 's' for rock, paper or scissors 9 | char getComputersChoice() { 10 | 11 | // generate random number based on time 12 | srand(time(NULL)); 13 | 14 | int randomNumber = (rand() % 3) + 1; 15 | char computersChoice; 16 | 17 | switch(randomNumber) { 18 | case 1: 19 | computersChoice = 'r'; 20 | break; 21 | case 2: 22 | computersChoice = 'p'; 23 | break; 24 | case 3: 25 | computersChoice = 's'; 26 | break; 27 | } 28 | 29 | return computersChoice; 30 | } 31 | 32 | // function to get user input 33 | // take user input and return it 34 | char getUserInput() { 35 | char userInput; 36 | 37 | printf("Enter Your Choice\n(r for rock, p for paper, s for scissors): "); 38 | scanf(" %c", &userInput); 39 | 40 | userInput = tolower(userInput); 41 | 42 | return userInput; 43 | } 44 | 45 | // return either 'w', 'l' or 'd' for win, loss or draw 46 | char getResult(char userPick, char computerPick) { 47 | 48 | // condition for user to draw 49 | if (computerPick == userPick) { 50 | return 'd'; 51 | } 52 | 53 | // conditions for user to win 54 | else if (userPick == 'p' && computerPick == 'r') { 55 | return 'w'; 56 | } 57 | else if (userPick == 'r' && computerPick == 's') { 58 | return 'w'; 59 | } 60 | else if (userPick == 's' && computerPick == 'p') { 61 | return 'w'; 62 | } 63 | 64 | // all other conditions result in user losing 65 | else { 66 | return 'l'; 67 | } 68 | } 69 | 70 | int main() { 71 | 72 | // get computer choice 73 | char computerPick = getComputersChoice(); 74 | char userPick = getUserInput(); 75 | 76 | 77 | while (1) { 78 | userPick = getUserInput(); 79 | if (userPick == 'r' || userPick == 'p' || userPick == 's') { 80 | break; 81 | } 82 | } 83 | 84 | char result = getResult(userPick, computerPick); 85 | 86 | // print result 87 | switch(result) { 88 | case 'w': 89 | printf("Computer's pick: %c\n", computerPick); 90 | printf("Your pick: %c\n", userPick); 91 | printf("You won"); 92 | break; 93 | case 'l': 94 | printf("Computer's pick: %c\n", computerPick); 95 | printf("Your pick: %c\n", userPick); 96 | printf("You lose"); 97 | break; 98 | case 'd': 99 | printf("Computer's pick: %c\n", computerPick); 100 | printf("Your pick: %c\n", userPick); 101 | printf("Draw"); 102 | break; 103 | } 104 | 105 | return 0; 106 | } --------------------------------------------------------------------------------