├── Some Important_codes ├── even-odd.c ├── sumOfArray.c ├── swapping.c ├── printNto1usingRecursion.c ├── first N fibonacci numbers.c ├── palindromeorNot.c └── vowelConsonantWithTcase.c ├── Function in C ├── basicFunction_helloWorld.c ├── function_add_twoNumbers.c ├── function_even-odd.c ├── functionAverage.c ├── basicOperation.cpp └── primeNumber.c ├── Array in C ├── sumOfanArray.c ├── sumOfEvenNumbers.c ├── userinput_.c ├── LinearSearch.c ├── deleteElementFromArray.c └── arrayOperations.cpp ├── Pointer in C ├── pointer.c ├── arrayPointer.c ├── stringPointer.c └── pointerStringInput.c ├── Recursion ├── 1TO5.c └── oneToFive.c └── Conditional Statement in C ├── vowelConsonant.c └── grade.c /Some Important_codes/even-odd.c: -------------------------------------------------------------------------------- 1 | ///Check a number is odd or even 2 | #include 3 | int main() 4 | { 5 | int n; 6 | scanf("%d", &n); 7 | if(n % 2 == 0) 8 | printf("EVEN\n"); 9 | else 10 | printf("ODD\n"); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /Function in C/basicFunction_helloWorld.c: -------------------------------------------------------------------------------- 1 | ///A basic function for printing 'Hello, world!' 2 | #include 3 | 4 | void printMessage() //Function definition 5 | { 6 | printf("Hello, world!\n"); 7 | } 8 | 9 | int main() { 10 | printMessage(); //Function call 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /Array in C/sumOfanArray.c: -------------------------------------------------------------------------------- 1 | ///Taking array as input 2 | #include 3 | int main() 4 | { 5 | int size; 6 | scanf("%d", &size); 7 | int arr[size], sum = 0; 8 | //Taking the array as input 9 | for(int i = 0; i < size; i++){ 10 | scanf("%d", &arr[i]); 11 | sum = sum + arr[i]; 12 | } 13 | printf("Sum = %d\n", sum); 14 | } 15 | -------------------------------------------------------------------------------- /Some Important_codes/sumOfArray.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int size; 5 | scanf("%d", &size); 6 | int ar1[size], sum = 0; 7 | 8 | printf("input ar1: "); 9 | for(int i = 0; i < size; i++){ 10 | scanf("%d", &ar1[i]); 11 | sum += ar1[i]; 12 | } 13 | printf("The sum is: %d\n", sum); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /Some Important_codes/swapping.c: -------------------------------------------------------------------------------- 1 | //Swap two number using pointer 2 | #include 3 | void swap(int *x, int *y) 4 | { 5 | int temp = *x; 6 | *x = *y; 7 | *y = temp; 8 | } 9 | int main() 10 | { 11 | int a, b; 12 | scanf("%d%d", &a, &b); 13 | printf("Before swapping: a = %d, b = %d\n", a, b); 14 | swap(&a, &b); 15 | printf("After swapping: a = %d, b = %d\n", a, b); 16 | } 17 | -------------------------------------------------------------------------------- /Array in C/sumOfEvenNumbers.c: -------------------------------------------------------------------------------- 1 | ///Sum of all even numbers 2 | #include 3 | int main() 4 | { 5 | int size; 6 | scanf("%d", &size); 7 | int arr[size], sum = 0; 8 | //Array input and sum 9 | for(int i = 0; i < size; i++){ 10 | scanf("%d", &arr[i]); 11 | if(arr[i] % 2 == 0) 12 | sum = sum + arr[i]; 13 | } 14 | printf("Sum = %d\n", sum); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /Function in C/function_add_twoNumbers.c: -------------------------------------------------------------------------------- 1 | ///Program to add two numbers using function 2 | #include 3 | 4 | int add(int a, int b) //Function definition 5 | { 6 | return a + b; 7 | } 8 | 9 | int main() { 10 | int x, y, sum; 11 | scanf("%d%d", &x, &y); //Scan x, y 12 | sum = add(x, y); //Function call with arguments x & y 13 | printf("Sum is: %d\n", sum); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /Some Important_codes/printNto1usingRecursion.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void printNto1(int n) { 4 | if (n < 1) return; //Base case: if n is less than 1, return previous state 5 | 6 | printf("%d ", n); 7 | printNto1(n - 1); //Recursive call with the next smaller number 8 | } 9 | 10 | int main() 11 | { 12 | int n; 13 | scanf("%d", &n); 14 | printNto1(n); 15 | return 0; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /Array in C/userinput_.c: -------------------------------------------------------------------------------- 1 | ///Taking array as input 2 | #include 3 | int main() 4 | { 5 | int size; 6 | scanf("%d", &size); 7 | int arr[size]; 8 | //Taking the array as input 9 | for(int i = 0; i < size; i++) 10 | scanf("%d", &arr[i]); 11 | //Printing the array 12 | printf("Printing the array: \n"); 13 | for(int i = 0; i < size; i++) 14 | printf("%d ", arr[i]); 15 | } 16 | -------------------------------------------------------------------------------- /Function in C/function_even-odd.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Function declaration 4 | int isEven(int num); 5 | 6 | int main() { 7 | int number = 5; 8 | if (isEven(number)) { 9 | printf("%d is even.\n", number); 10 | } else { 11 | printf("%d is odd.\n", number); 12 | } 13 | return 0; 14 | } 15 | 16 | // Function definition 17 | int isEven(int num) { 18 | return num % 2 == 0; 19 | } 20 | -------------------------------------------------------------------------------- /Pointer in C/pointer.c: -------------------------------------------------------------------------------- 1 | #include 2 | void swap(int *x, int *y) 3 | { 4 | *x = *x + *y; 5 | *y = *x - *y; 6 | *x = *x - *y; 7 | 8 | //++*x, *y += 50; 9 | 10 | } 11 | int main() 12 | { 13 | int a = 10, b = 20; 14 | //scanf("%d%d", &a, &b); 15 | printf("Before swapping: a = %d, b = %d\n", a, b); 16 | swap(&a, &b); 17 | printf("After swapping: a = %b, b = %d\n", a, b); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Recursion/1TO5.c: -------------------------------------------------------------------------------- 1 | ///Recursion to print 1 to 10 2 | #include 3 | 4 | // Function declaration 5 | void printNumbers(int n); 6 | 7 | int main() { 8 | printNumbers(1); // Start from number 1 9 | return 0; 10 | } 11 | 12 | // Function to print numbers using recursion 13 | void printNumbers(int n) { 14 | if (n <= 5) { 15 | printf("%d\n", n); 16 | printNumbers(n + 1); // Recursive call with incremented value 17 | // printf("%d\n", n); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Pointer in C/arrayPointer.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a[5] = {7, 5, 2, 3, 10}; 6 | int *ptr = &a; 7 | int i; 8 | //printf("%d %d", ptr[0], ptr[1]); 9 | 10 | for(i = 0; i < 5; i++) 11 | { 12 | // printf("%d[%d]: a[%d] = %d\n", ptr+i, i, i, *(ptr+i)); 13 | printf("%d ", &a[i]);//0+0, 0+1, 0+2, 0+3, 0+4 14 | //printf("%d ", ptr[i]); //ptr[0], ptr[1] 15 | //printf("%d ", *ptr++); //0, 1, 2, 3, 4 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /Some Important_codes/first N fibonacci numbers.c: -------------------------------------------------------------------------------- 1 | //Print First N Fibonacci numbers 2 | #include 3 | int fib(int n) 4 | { 5 | if(n == 0) return 0; 6 | if(n == 1) return 1; 7 | return fib(n-1) + fib(n-2); 8 | } 9 | 10 | int main() 11 | { 12 | int n; 13 | scanf("%d", &n); 14 | //printf("%dth Fibonacci number is %d\n",n, fib(n)); 15 | printf("First %d Fibonacci series:\n", n); 16 | for(int i =0; i < n; i++) 17 | { 18 | printf("%d ", fib(i)); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Pointer in C/stringPointer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | char *fruits[] = {"apple", "banana", "mango", "cherry"}; 7 | int i; 8 | // for(i = 0; i < 10; i++) 9 | // scanf("%s", &fruits[i]); 10 | 11 | //int size_fruits = sizeof(fruits)/sizeof(fruits[0]); 12 | int size_fruits = strlen(fruits[1]); 13 | 14 | printf("Size: %d\n", size_fruits); 15 | for(i = 0; i < size_fruits; i++) 16 | printf("%s ", fruits[i]); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /Recursion/oneToFive.c: -------------------------------------------------------------------------------- 1 | ///Recursion to print 1 to 10 2 | #include 3 | 4 | // Function declaration 5 | void printNumbers(int n); 6 | 7 | int main() { 8 | printNumbers(10); // Start from number 1 9 | printf("Hello\n"); 10 | printf("Done\n"); 11 | return 0; 12 | } 13 | 14 | // Function to print numbers using recursion 15 | void printNumbers(int n) { 16 | if (n >= 1) { 17 | printNumbers(n - 1); // Recursive call with incremented value 18 | printf("%d\n", n); 19 | } 20 | } 21 | 22 | -------------------------------------------------------------------------------- /Some Important_codes/palindromeorNot.c: -------------------------------------------------------------------------------- 1 | ///check an integer is palindrome or not 2 | #include 3 | int main() 4 | { 5 | int num, rem=0, rev=0; 6 | scanf("%d", &num); //121 7 | int original_val = num; 8 | while(num > 0) 9 | { 10 | rem = num % 10; ///remainder 11 | rev = rev * 10 + rem;//10+2=12 120+1=121 12 | num /= 10; //0 13 | } 14 | if(original_val == rev) 15 | { 16 | printf("Palindrome\n"); 17 | } 18 | else printf("Not Palindrome\n"); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Conditional Statement in C/vowelConsonant.c: -------------------------------------------------------------------------------- 1 | //Check an alphabet is vowel or consonant 2 | #include 3 | int main() 4 | { 5 | char ch; 6 | scanf("%c", &ch); 7 | if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) 8 | { 9 | if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') 10 | printf("Vowel\n"); 11 | else 12 | printf("Consonant\n"); 13 | } 14 | else 15 | printf("Not an Alphabet\n"); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /Function in C/functionAverage.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | float average(int x, int y) //Function to find average 4 | { 5 | int s = sum(x, y); 6 | float av = (float)s/2; 7 | return av; 8 | 9 | } 10 | 11 | sum(int x, int y) //Function to find summation 12 | { 13 | return x + y; 14 | } 15 | 16 | void result() 17 | { 18 | int a, b; 19 | scanf("%d%d", &a, &b); 20 | float avg = average(a, b); // Call average 21 | printf("The average is %.2f\n", avg); 22 | } 23 | int main(){ 24 | 25 | result(); //Call result 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /Array in C/LinearSearch.c: -------------------------------------------------------------------------------- 1 | ///Linear search 2 | #include 3 | int main() 4 | { 5 | int size, target, flag = 0; 6 | scanf("%d", &size); 7 | int arr[size]; 8 | //Input array 9 | for(int i = 0; i < size; i++){ 10 | scanf("%d", &arr[i]); 11 | } 12 | //input target element 13 | scanf("%d", &target); 14 | for(int i = 0; i < size; i++){ 15 | if(arr[i] == target) 16 | { 17 | printf("%d is found at index %d\n", target, i); 18 | flag = 1; 19 | break; 20 | } 21 | } 22 | if(flag != 1) 23 | printf("Not found!\n"); 24 | return 0; 25 | 26 | } 27 | 28 | 29 | -------------------------------------------------------------------------------- /Array in C/deleteElementFromArray.c: -------------------------------------------------------------------------------- 1 | ///Delete an element from array 2 | #include 3 | int main() 4 | { 5 | int size, target_pos; 6 | scanf("%d", &size); 7 | int arr[size]; 8 | //Input array 9 | for(int i = 0; i < size; i++) 10 | scanf("%d", &arr[i]); 11 | 12 | //input target position 13 | scanf("%d", &target_pos); 14 | if(target_pos < 0 || target_pos >= size) 15 | printf("Invalid Position\n"); 16 | else 17 | { 18 | for(int i = target_pos; i < size; i++) 19 | { 20 | arr[i] = arr[i+1]; 21 | } 22 | } 23 | //Print Updated Array 24 | for(int i = 0; i < size-1; i++) 25 | { 26 | printf("%d ", arr[i]); 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Some Important_codes/vowelConsonantWithTcase.c: -------------------------------------------------------------------------------- 1 | //Check an alphabet is vowel or consonant 2 | #include 3 | int main() 4 | { 5 | int no_of_tCase; 6 | char ch; 7 | scanf("%d", &no_of_tCase); 8 | for(int i = 1; i <= no_of_tCase; i++) 9 | { 10 | scanf(" %c", &ch); 11 | if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) 12 | { 13 | if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || 14 | ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || 15 | ch == 'O' || ch == 'U') 16 | printf("Case %d: Vowel\n", i); 17 | else 18 | printf("Case %d: Consonant\n", i); 19 | } 20 | else 21 | printf("Case %d: Not an Alphabet\n", i); 22 | } 23 | return 0; 24 | 25 | } 26 | 27 | -------------------------------------------------------------------------------- /Function in C/basicOperation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | //function without return type and has no parameters 3 | void print1to5() 4 | { 5 | for (int i = 1; i <= 5; ++i) 6 | { 7 | printf("%d ", i); 8 | } 9 | 10 | return; 11 | } 12 | void print1toN(int n) 13 | { 14 | for (int i = 1; i <= n; ++i) 15 | { 16 | printf("%d ", i); 17 | } 18 | return; 19 | } 20 | int max(int a, int b) //here a and b are parameters 21 | { 22 | if(a > b) return a; 23 | else return b; 24 | } 25 | int main() 26 | { 27 | print1to5(); //user defined //scenario 1 28 | printf("\n"); 29 | int N; 30 | scanf("%d", &N); 31 | print1toN(N); //scenario 2 32 | printf("\n"); 33 | 34 | int m, n; 35 | scanf("%d%d", &m, &n); //here m and n are arguments 36 | int mx = max(m, n); ////scenario 3 37 | printf("Maximum = %d\n", mx); 38 | return 0; //to terminate the main function 39 | } 40 | -------------------------------------------------------------------------------- /Conditional Statement in C/grade.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | float marks; 5 | printf("Enter your marks: \n"); 6 | 7 | scanf("%f", &marks); 8 | if(marks >= 80 && marks <= 100) 9 | printf("A+\n"); 10 | else if(marks >= 75 && marks < 80) 11 | printf("A\n"); 12 | else if(marks >= 70 && marks < 75) 13 | printf("A-\n"); 14 | else if(marks >= 65 && marks < 70) 15 | printf("B+\n"); 16 | else if(marks >= 60 && marks < 65) 17 | printf("B\n"); 18 | else if(marks >= 55 && marks < 60) 19 | printf("B-\n"); 20 | else if(marks >= 50 && marks < 55) 21 | printf("C+\n"); 22 | else if(marks >= 45 && marks < 50) 23 | printf("C\n"); 24 | else if(marks >= 40 && marks < 45) 25 | printf("D\n"); 26 | else if(marks >= 0 && marks < 40) 27 | printf("Failed!\n"); 28 | else 29 | printf("Enter a valid Marks.\n"); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Function in C/primeNumber.c: -------------------------------------------------------------------------------- 1 | ///Program to check a number is prime or not 2 | #include 3 | #include 4 | #include 5 | 6 | bool isPrime(int n) //Function to check prime 7 | { 8 | if(n < 2) return 0; 9 | int i; 10 | for(i = 2; i <= sqrt(n); i++) 11 | { 12 | if(n % i == 0) 13 | return 0; 14 | } 15 | return 1; 16 | } 17 | 18 | int main() 19 | { 20 | int n, t_case, i; 21 | scanf("%d", &t_case); 22 | for(i = 1; i <= t_case; i++){ 23 | scanf("%d", &n); 24 | printf(isPrime(n) ? "Case %d: %d is a prime.\n" : "Case %d: %d is not a prime.\n", i, n); 25 | } 26 | return 0; 27 | } 28 | 29 | ///SAMPLE I/0 30 | /* 31 | 5 32 | 10 2 5 6 4 33 | */ 34 | 35 | /* Way 1: 36 | if(!isPrime(n)) //1 37 | printf("%d is not a prime.\n", n); 38 | else 39 | printf("%d is a prime.\n", n);*/ 40 | //Way 2 41 | //(isPrime(n)) ? printf("%d is a prime.\n", n) : printf("%d is not a prime.\n", n); 42 | 43 | //Way 2 44 | -------------------------------------------------------------------------------- /Array in C/arrayOperations.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void linearSearch(int arr[]) 4 | { 5 | 6 | int key; 7 | scanf("%d", &key); 8 | //search operation using linear search 9 | for (int i = 0; i < 5; ++i) 10 | { 11 | if (key == arr[i]) 12 | { 13 | printf("%d is found at index: %d\n", key, i); 14 | break; 15 | } 16 | } 17 | } 18 | 19 | void even_odd_count(int arr[], int n) 20 | { 21 | int cnt_even = 0, cnt_odd = 0; 22 | for (int i = 0; i < n; ++i) 23 | { 24 | if(arr[i] % 2 == 0) cnt_even++; 25 | else cnt_odd++; 26 | } 27 | printf("Even = %d\nOdd = %d\n", cnt_even, cnt_odd); 28 | } 29 | int largestNumber(int arr[], int n) 30 | { 31 | int large = arr[0]; 32 | for (int i = 0; i < n; ++i) 33 | { 34 | if(large < arr[i]) 35 | large = arr[i]; 36 | } 37 | //printf("largest number is: %d\n", large); 38 | return large; 39 | } 40 | 41 | void secondLargest(int arr[], int n) 42 | { 43 | int large = largestNumber(arr, n); 44 | int second_large = INT_MIN; //initialize second largest to the smallest possible number 45 | for (int i = 0; i < n; ++i) 46 | { 47 | if(arr[i] == large) continue;//to skip the largest one 48 | if(arr[i] > second_large){ 49 | second_large = arr[i]; //update the second_large 50 | } 51 | } 52 | printf("Second Large is: %d\n", second_large); 53 | } 54 | int main() 55 | { 56 | int n; scanf("%d", &n); 57 | int arr[n]; 58 | for (int i = 0; i < n; ++i) 59 | scanf("%d", &arr[i]); 60 | 61 | //even_odd_count(arr, n); 62 | //linearSearch(); 63 | //largestNumber(arr, n); 64 | secondLargest(arr, n); 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /Pointer in C/pointerStringInput.c: -------------------------------------------------------------------------------- 1 | //#include 2 | #include 3 | 4 | #define MAX_STRINGS 5 // Maximum number of strings 5 | #define STRING_SIZE 256 // Maximum size of each string 6 | 7 | int main() { 8 | char strings[MAX_STRINGS][STRING_SIZE]; // Array of strings 9 | int i; 10 | 11 | // Prompt the user to input strings 12 | printf("Enter %d strings, each not exceeding %d characters:\n", MAX_STRINGS, STRING_SIZE - 1); 13 | 14 | for (i = 0; i < MAX_STRINGS; i++) { 15 | printf("String %d: ", i + 1); 16 | fgets(strings[i], STRING_SIZE, stdin); 17 | 18 | // Remove the newline character if it's at the end of the string 19 | strings[i][strcspn(strings[i], "\n")] = 0; 20 | } 21 | 22 | // Display all the strings entered by the user 23 | printf("\nYou entered these strings:\n"); 24 | for (i = 0; i < MAX_STRINGS; i++) { 25 | printf("String %d: %s\n", i + 1, strings[i]); 26 | } 27 | 28 | return 0; 29 | } 30 | 31 | 32 | 33 | /* 34 | int main() { 35 | const int SIZE = 256; // Define the maximum length of the string 36 | char str[SIZE]; // Declare a character array to hold the input 37 | char *ptr = str; // Declare a pointer to the character array 38 | 39 | printf("Enter a string: "); 40 | // Use fgets to read a string from the user 41 | // It reads up to SIZE-1 characters to leave space for the null terminator 42 | fgets(ptr, SIZE, stdin); 43 | 44 | // Output the string entered by the user 45 | printf("You entered: %s", ptr); 46 | 47 | return 0; 48 | } 49 | */ 50 | --------------------------------------------------------------------------------