├── 1.1Hello.c ├── 1.2Array_of_variable.c ├── 1.3Struct_Variable.c ├── 10.1Array_SumFunction.c ├── 11.1Array_Pointer.c ├── 11.2Pointer_Arithmetic.c ├── 11.3Dynamic_memory.c ├── 12.1Swapping.c ├── 12.3_Bubble_Sort.c ├── 13.1Accessing_Structure_pointer.c ├── 13.2Function_pointer.c ├── 14.1Command_line_argument.c ├── 2.1Data_Types.c ├── 2.3Struct_CustomeData.c ├── 3.1Arithmetic.c ├── 3.3Bitwise.c ├── 4.1Switch.c ├── 4.3Goto.c ├── 5.2Even.c ├── 5.3Table.c ├── 6.1Naturals.c ├── 6.2Factorial.c ├── 6.3Reverse.c ├── 7.2 Maximum_Array_Element.c ├── 7.3 Reverse_an_Array.c ├── 8.12D_Array_Sum.c ├── 8.2Matrix_Addition.c ├── 9.2Matrix3D_multiplication.c └── README.md /1.1Hello.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | printf("Hello, World!"); 4 | return 0; 5 | } -------------------------------------------------------------------------------- /1.2Array_of_variable.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | 6 | int arr[30], size, i, sum = 0; 7 | 8 | //Get size input from user 9 | printf("Enter the number of elements in the array: "); 10 | scanf("%d",&size); 11 | 12 | //Get all elements using for loop and store it in array 13 | printf("Enter %d elements: ",size); 14 | for(i = 0; i < size; i++) 15 | { 16 | scanf("%d",&arr[i]); 17 | } 18 | //add all elements to the variable sum. 19 | for(i = 0; i < size; i++) 20 | { 21 | sum = sum + arr[i]; // same as sum += arr[i]; 22 | } 23 | //print the result 24 | printf("Sum of the elements: %d",sum); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /1.3Struct_Variable.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // Define the 'Student' struct 5 | struct Student { 6 | char name[100]; 7 | int id; 8 | float grade; 9 | }; 10 | 11 | // Function to print student details 12 | void printStudent(struct Student s) { 13 | printf("Name: %s\n", s.name); 14 | printf("ID: %d\n", s.id); 15 | printf("Grade: %.2f\n", s.grade); 16 | } 17 | 18 | // Function to update the student's grade 19 | void updateGrade(struct Student* s, float newGrade) { 20 | s->grade = newGrade; 21 | } 22 | 23 | int main() { 24 | struct Student student; 25 | 26 | // Read the student's name using fgets (safe for spaces) 27 | printf("Enter student's name: "); 28 | fgets(student.name, sizeof(student.name), stdin); 29 | 30 | // Remove the trailing newline character that fgets may include 31 | student.name[strcspn(student.name, "\n")] = '\0'; 32 | 33 | // Read the student's ID and grade 34 | printf("Enter student's ID: "); 35 | scanf("%d", &student.id); 36 | 37 | printf("Enter student's grade: "); 38 | scanf("%f", &student.grade); 39 | 40 | // Print initial student details 41 | printf("\nStudent Details:\n"); 42 | printStudent(student); 43 | 44 | // Update student's grade 45 | printf("\nEnter new grade for the student: "); 46 | float newGrade; 47 | scanf("%f", &newGrade); 48 | 49 | // Update and print the updated details 50 | updateGrade(&student, newGrade); 51 | printf("\nUpdated Student Details:\n"); 52 | printStudent(student); 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /10.1Array_SumFunction.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | // Function to calculate the sum of elements in an array 5 | int sumOfArray(int arr[], int size) { 6 | int sum = 0; 7 | for (int i = 0; i < size; i++) { 8 | sum = sum + arr[i]; // Add each element to sum 9 | } 10 | return sum; 11 | } 12 | 13 | int main() { 14 | int n; 15 | 16 | // Prompt the user to enter the number of elements 17 | printf("Enter the number of elements in the array:\n"); 18 | scanf("%d", &n); 19 | 20 | // Declare the array with the size entered by the user 21 | int arr[n]; 22 | 23 | // Prompt the user to enter the elements of the array 24 | printf("Enter the elements of the array:\n"); 25 | for (int i = 0; i < n; i++) { 26 | scanf("%d", &arr[i]); 27 | } 28 | 29 | // Call the sumOfArray function and display the result 30 | int result = sumOfArray(arr, n); 31 | printf("The sum of the elements in the array is: %d\n", result); 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /11.1Array_Pointer.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int arr[5]; // Declare an integer array of size 5 5 | int *ptr = arr; // Declare a pointer to the first element of the array 6 | 7 | // Input values into the array using the pointer 8 | printf("Enter 5 elements:\n"); 9 | for (int i = 0; i < 5; i++) { 10 | scanf("%d", ptr + i); // Store the value in the array using the pointer 11 | } 12 | 13 | // Print the array elements using the pointer 14 | printf("Array elements are:\n"); 15 | for (int i = 0; i < 5; i++) { 16 | printf("%d ", *(ptr + i)); // Dereference pointer to print values 17 | } 18 | 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /11.2Pointer_Arithmetic.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int n, sum = 0; 5 | float average; 6 | 7 | // Input number of elements in the array 8 | printf("Enter the number of elements:\n"); 9 | scanf("%d", &n); 10 | 11 | // Declare an array of size n 12 | int arr[n]; 13 | 14 | // Input array elements 15 | printf("Enter %d elements:\n",n); 16 | for (int i = 0; i < n; i++) { 17 | scanf("%d", &arr[i]); 18 | } 19 | 20 | // Using a pointer to traverse the array and calculate sum 21 | int *ptr = arr; // Pointer to the first element of the array 22 | 23 | for (int i = 0; i < n; i++) { 24 | sum += *(ptr + i); // Dereference pointer to get the value and add to sum 25 | } 26 | 27 | // Calculate the average 28 | average = (float)sum / n; 29 | 30 | // Output the sum and average 31 | printf("Sum: %d\n", sum); 32 | printf("Average: %.2f\n", average); 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /11.3Dynamic_memory.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include // For malloc and free 3 | 4 | int main() { 5 | int *arr; 6 | int n, i; 7 | 8 | // Ask the user for the size of the array 9 | printf("Enter the number of elements:\n"); 10 | scanf("%d", &n); 11 | 12 | // Dynamically allocate memory for the array 13 | arr = (int *)malloc(n * sizeof(int)); 14 | 15 | // Check if memory allocation was successful 16 | if (arr == NULL) { 17 | printf("Memory allocation failed!\n"); 18 | return 1; // Exit the program if allocation failed 19 | } 20 | 21 | // Populate the array with values 22 | printf("Enter %d elements:\n", n); 23 | for (i = 0; i < n; i++) { 24 | printf("Element %d: ", i + 1); 25 | scanf("%d", &arr[i]); 26 | } 27 | 28 | // Print the array 29 | printf("The array elements are:\n"); 30 | for (i = 0; i < n; i++) { 31 | printf("%d ", arr[i]); 32 | } 33 | printf("\n"); 34 | 35 | // Free the dynamically allocated memory 36 | free(arr); 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /12.1Swapping.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Function to swap two numbers using pointers 4 | void swap(int *a, int *b) { 5 | int temp = *a; // Store the value at the address a in a temporary variable 6 | *a = *b; // Assign the value at address b to address a 7 | *b = temp; // Assign the temporary value to address b 8 | } 9 | 10 | int main() { 11 | int x, y; 12 | 13 | // Ask the user to input two numbers 14 | printf("Enter the first number:\n"); 15 | scanf("%d", &x); 16 | 17 | printf("Enter the second number:\n"); 18 | scanf("%d", &y); 19 | 20 | // Display values before swapping 21 | printf("Before swapping: num1 = %d, num2 = %d\n", x, y); 22 | 23 | // Call the swap function, passing the addresses of x and y 24 | swap(&x, &y); 25 | 26 | // Display values after swapping 27 | printf("After swapping: num1 = %d, num2 = %d\n", x, y); 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /12.3_Bubble_Sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Function to swap two integers using pointers 4 | void swap(int *a, int *b) { 5 | int temp = *a; 6 | *a = *b; 7 | *b = temp; 8 | } 9 | 10 | // Bubble sort function to sort the array 11 | void bubbleSort(int arr[], int n) { 12 | // Outer loop for each pass through the array 13 | for (int i = 0; i < n-1; i++) { 14 | // Inner loop to perform the comparisons and swaps 15 | for (int j = 0; j < n-i-1; j++) { 16 | // If the element at index j is greater than the next element 17 | if (arr[j] > arr[j+1]) { 18 | // Swap the elements using the swap function 19 | swap(&arr[j], &arr[j+1]); 20 | } 21 | } 22 | } 23 | } 24 | 25 | // Main function 26 | int main() { 27 | int n; 28 | 29 | // Read the number of elements in the array 30 | printf("Enter the number of elements:\n"); 31 | scanf("%d", &n); 32 | 33 | int arr[n]; 34 | 35 | // Read the array elements from the user 36 | printf("Enter %d elements:\n",n); 37 | for (int i = 0; i < n; i++) { 38 | scanf("%d", &arr[i]); 39 | } 40 | 41 | // Call bubbleSort to sort the array 42 | bubbleSort(arr, n); 43 | 44 | // Print the sorted array 45 | printf("Sorted array: "); 46 | for (int i = 0; i < n; i++) { 47 | printf("%d ", arr[i]); 48 | } 49 | printf("\n"); 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /13.1Accessing_Structure_pointer.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Define the structure 4 | struct Student { 5 | char name[50]; 6 | int rollNo; 7 | float marks; 8 | }; 9 | 10 | int main() { 11 | // Declare a pointer to the structure 12 | struct Student *ptr; 13 | 14 | // Create a structure variable 15 | struct Student student; 16 | 17 | // Point the pointer to the structure variable 18 | ptr = &student; 19 | 20 | // Take user input via the pointer 21 | printf("Enter name:\n"); 22 | scanf("%49s", ptr->name); // Using %49s to prevent buffer overflow 23 | 24 | printf("Enter roll number:\n"); 25 | scanf("%d", &ptr->rollNo); 26 | 27 | printf("Enter marks:\n"); 28 | scanf("%f", &ptr->marks); 29 | 30 | // Display the structure members using the pointer 31 | printf("\nStudent Details:\n"); 32 | printf("Name: %s\n", ptr->name); 33 | printf("Roll No: %d\n", ptr->rollNo); 34 | printf("Marks: %.2f\n", ptr->marks); 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /13.2Function_pointer.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Define the structure Student 4 | struct Student { 5 | char name[50]; 6 | int age; 7 | float marks; 8 | }; 9 | 10 | // Function to print student details using a pointer 11 | void printStudentDetails(struct Student *ptr) { 12 | printf("\nStudent Details:\n"); 13 | printf("Name: %s\n\n", ptr->name); 14 | printf("Age: %d\n", ptr->age); 15 | printf("Marks: %.2f\n", ptr->marks); 16 | } 17 | 18 | int main() { 19 | // Declare a Student structure 20 | struct Student student; 21 | 22 | // Take user input for the structure members 23 | printf("Enter name:\n"); 24 | scanf("%49s", student.name); // Using %49s to prevent buffer overflow 25 | 26 | printf("Enter age:\n"); 27 | scanf("%d", &student.age); 28 | 29 | printf("Enter marks:\n"); 30 | scanf("%f", &student.marks); 31 | 32 | // Call the function to print the details, passing the address of the structure 33 | printStudentDetails(&student); 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /14.1Command_line_argument.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(int argc, char *argv[]) { 4 | // Loop through each argument 5 | for (int i = 0; i < argc; i++) { 6 | printf("Total number of arguments: %d\n",i+1); 7 | printf("Argument %d: %s\n", i, argv[i]); 8 | } 9 | 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /2.1Data_Types.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | //Declare variable 5 | int myInt ; 6 | float myFloat; 7 | char myChar ; 8 | double myDouble ; 9 | 10 | // Take input from the user 11 | printf("Enter an integer: "); 12 | scanf("%d", &myInt); 13 | 14 | printf("Enter a float: "); 15 | scanf("%f", &myFloat); 16 | 17 | printf("Enter a character: "); 18 | scanf(" %c", &myChar); // Notice the space before %c to consume any newline character 19 | 20 | printf("Enter a double: "); 21 | scanf("%lf", &myDouble); 22 | 23 | // Print their values 24 | printf("Integer: %d", myInt); 25 | printf("\nFloat: %.2f", myFloat); 26 | printf("\nCharacter: %c", myChar); 27 | printf("\nDouble: %.2lf", myDouble); 28 | 29 | return 0; 30 | 31 | } -------------------------------------------------------------------------------- /2.3Struct_CustomeData.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int num1, num2; 5 | 6 | // Input two integers 7 | printf("Enter an integer: "); 8 | scanf("%d", &num1); 9 | 10 | printf("Enter a number to AND with:"); 11 | scanf("%d", &num2); 12 | 13 | // Performing bitwise AND (&) 14 | printf(" Result of %d AND %d = %d\n",num1,num2, num1 & num2); 15 | 16 | // Performing bitwise OR (|) 17 | printf("Enter a number to OR with: "); 18 | scanf("%d", &num2); 19 | printf("Result of %d OR %d = %d\n",num1,num2, num1 | num2); 20 | 21 | // Performing bitwise XOR (^) 22 | printf("Enter a number to XOR with: "); 23 | scanf("%d", &num2); 24 | printf("Result of %d XOR %d = %d\n",num1,num2, num1 ^ num2); 25 | 26 | // Performing bitwise NOT (~) 27 | printf("Result of NOT %d = %d\n",num1,~num1); 28 | 29 | 30 | // Performing left shift (<<) by 1 bit (example) 31 | int shift; 32 | printf("Enter the number of positions to left shift: "); 33 | scanf("%d",&shift); 34 | printf("Result of %d << %d = %d\n",num1,shift,num1<>) by 1 bit (example) 37 | printf("Enter the number of positions to right shift: "); 38 | scanf("%d",&shift); 39 | printf("Result of %d >> %d = %d\n",num1,shift,num1>>shift); 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /3.1Arithmetic.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | int a, b; 6 | printf("Enter two integers: "); 7 | scanf("%d%d",&a,&b); 8 | 9 | int sum = a+b; 10 | printf("Addition: %d + %d = %d\n",a,b,sum); 11 | 12 | int sub = a-b; 13 | printf("Subtraction: %d - %d = %d\n",a,b,sub); 14 | 15 | int mul = a*b; 16 | printf("Multiplication: %d * %d = %d\n",a,b,mul); 17 | 18 | int div = a/b; 19 | printf("Division: %d / %d = %d\n",a,b,div); 20 | 21 | int rem = a%b; 22 | printf("Modulus: %d %% %d = %d\n",a,b,rem); 23 | 24 | // Write Your Code Here 25 | 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /3.3Bitwise.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int num1, num2; 5 | 6 | // Input two integers 7 | printf("Enter an integer: "); 8 | scanf("%d", &num1); 9 | 10 | printf("Enter a number to AND with:"); 11 | scanf("%d", &num2); 12 | 13 | // Performing bitwise AND (&) 14 | printf(" Result of %d AND %d = %d\n",num1,num2, num1 & num2); 15 | 16 | // Performing bitwise OR (|) 17 | printf("Enter a number to OR with: "); 18 | scanf("%d", &num2); 19 | printf("Result of %d OR %d = %d\n",num1,num2, num1 | num2); 20 | 21 | // Performing bitwise XOR (^) 22 | printf("Enter a number to XOR with: "); 23 | scanf("%d", &num2); 24 | printf("Result of %d XOR %d = %d\n",num1,num2, num1 ^ num2); 25 | 26 | // Performing bitwise NOT (~) 27 | printf("Result of NOT %d = %d\n",num1,~num1); 28 | 29 | 30 | // Performing left shift (<<) by 1 bit (example) 31 | int shift; 32 | printf("Enter the number of positions to left shift: "); 33 | scanf("%d",&shift); 34 | printf("Result of %d << %d = %d\n",num1,shift,num1<>) by 1 bit (example) 37 | printf("Enter the number of positions to right shift: "); 38 | scanf("%d",&shift); 39 | printf("Result of %d >> %d = %d\n",num1,shift,num1>>shift); 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /4.1Switch.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int day; 5 | 6 | // Prompt the user to enter a day number 7 | printf("Enter the day number (1-7): "); 8 | scanf("%d", &day); 9 | 10 | // Use switch statement to determine the day name 11 | switch (day) { 12 | case 1: 13 | printf("Sunday\n"); 14 | break; 15 | case 2: 16 | printf("Monday\n"); 17 | break; 18 | case 3: 19 | printf("Tuesday\n"); 20 | break; 21 | case 4: 22 | printf("Wednesday\n"); 23 | break; 24 | case 5: 25 | printf("Thursday\n"); 26 | break; 27 | case 6: 28 | printf("Friday\n"); 29 | break; 30 | case 7: 31 | printf("Saturday\n"); 32 | break; 33 | default: 34 | printf("Invalid day number! Please enter a number between 1 and 7.\n"); 35 | break; 36 | } 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /4.3Goto.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int num; 5 | int factorial = 1; 6 | 7 | // Input a number from the user 8 | printf("Enter a positive integer: "); 9 | //scanf("%d", &num); 10 | if (scanf("%d", &num) != 1) { 11 | goto error; // Jump to error handling if input is invalid 12 | } 13 | 14 | 15 | // Check for negative input 16 | if (num < 0 ) { 17 | goto error; // Jump to error handling if negative 18 | } 19 | 20 | // Calculate factorial 21 | for (int i = 1; i <= num; i++) { 22 | factorial = factorial* i; 23 | } 24 | 25 | // Print the result 26 | printf("Factorial of %d = %d", num, factorial); 27 | return 0; 28 | 29 | error: 30 | // Error handling 31 | printf("Invalid input. Please enter a positive integer."); 32 | return 1; // Return a non-zero value to indicate an error 33 | } 34 | -------------------------------------------------------------------------------- /5.2Even.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int n, sum = 0, curr = 2; 5 | printf("Enter the number of even numbers: "); 6 | scanf("%d", &n); 7 | 8 | for(int i=1; i<=n; i++){ 9 | sum+=curr;// sum = sum+curr 10 | 11 | curr+=2;//curr =curr+2 12 | 13 | } 14 | 15 | printf("Sum of the first %d even numbers is: %d\n", n, sum); 16 | return 0; 17 | } -------------------------------------------------------------------------------- /5.3Table.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int n; 5 | 6 | // Prompt user for input 7 | printf("Enter the value of n: "); 8 | scanf("%d", &n); 9 | 10 | 11 | for (int i = 1; i <= n; i++) { 12 | for (int j = 1; j <= n; j++) { 13 | printf("%4d", i * j); // Print the product with formatting 14 | } 15 | printf("\n"); // Move to the next line after each row 16 | } 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /6.1Naturals.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int n, sum = 0, i = 1; 5 | 6 | // Get the value of n from the user 7 | printf("Enter a positive integer: "); 8 | scanf("%d",&n); 9 | 10 | // Calculate the sum of the first n natural numbers using a while loop 11 | while(i<=n){ 12 | 13 | sum = sum+i; 14 | i++; 15 | 16 | } 17 | printf("The sum of the first %d natural numbers is: %d",n, sum); 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /6.2Factorial.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int number, fact = 1, i = 1; 5 | 6 | printf("Enter a positive integer: "); 7 | scanf("%d",&number); 8 | if(number>=0){ 9 | 10 | do{ 11 | fact =fact*i; 12 | i++; 13 | } 14 | 15 | while(i<=number); 16 | 17 | 18 | printf("Factorial of %d is %d",number,fact); 19 | }else { 20 | printf("Factorial of a negative number doesn't exist."); 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /6.3Reverse.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int n, reversedNum = 0, lastDigit; 5 | 6 | // Input a number from the user 7 | printf("Enter an integer: "); 8 | scanf("%d", &n); 9 | 10 | // Use a while loop to reverse the number 11 | while (n != 0) { 12 | lastDigit = n % 10; // Extract the last digit 13 | reversedNum = (reversedNum * 10) + lastDigit; // Build the reversed number 14 | n /= 10; // Remove the last digit 15 | } 16 | 17 | // Output the reversed number 18 | printf("Reversed number: %d\n", reversedNum); 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /7.2 Maximum_Array_Element.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | 6 | int arr[20],size, i, max; 7 | 8 | //Get size input from user 9 | printf("Enter the number of elements:\n"); 10 | scanf("%d",&size); 11 | 12 | //Get all elements using for loop and store it in array 13 | printf("Enter %d elements:\n",size); 14 | for(i = 0; i < size; i++) 15 | { 16 | scanf("%d",&arr[i]); 17 | } 18 | // Initialize max with the first element 19 | max = arr[0]; 20 | 21 | // Traverse the array to find the maximum 22 | for(i = 1; i < size; i++) 23 | { 24 | if(arr[i]>max) 25 | { 26 | max = arr[i]; 27 | } 28 | } 29 | 30 | 31 | //print the result 32 | printf("The maximum element is %d",max); 33 | 34 | return 0; 35 | } 36 | 37 | -------------------------------------------------------------------------------- /7.3 Reverse_an_Array.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | 6 | int arr[20],n, i; 7 | 8 | //Get size input from user 9 | printf("Enter the number of elements:\n"); 10 | scanf("%d",&n); 11 | 12 | //Get all elements using for loop and store it in array 13 | printf("Enter %d elements:\n",n); 14 | for(i = 0; i < n; i++) 15 | { 16 | scanf("%d",&arr[i]); 17 | } 18 | 19 | 20 | // Reverse the array 21 | int rev[n], j = 0; 22 | for(i = n-1; i >= 0; i--) 23 | { 24 | rev[j] = arr[i]; 25 | j++; 26 | } 27 | // Output the reversed array 28 | printf("Reversed array:\n"); 29 | for(i = 0; i < n; i++) 30 | { 31 | printf("%d ", rev[i]); 32 | } 33 | 34 | 35 | return 0; 36 | } 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /8.12D_Array_Sum.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int arr[3][3]; // 2D array with 3 rows and 3 columns 5 | int sum = 0; 6 | 7 | // Take input values from user for the 2D array 8 | printf("Enter elements of the array (3 x 3):\n"); 9 | 10 | // Loop to get user input for each element in the 2D array 11 | for (int i = 0; i < 3; i++) { 12 | for (int j = 0; j < 3; j++) { 13 | printf("Element [%d][%d]: ", i, j); // Prompt for each element 14 | scanf("%d", &arr[i][j]); // Store the input in the array 15 | } 16 | } 17 | 18 | // Calculate the sum of all the elements in the array 19 | for (int i = 0; i < 3; i++) { 20 | for (int j = 0; j < 3; j++) { 21 | sum = sum + arr[i][j]; // Add the element to the sum 22 | } 23 | } 24 | 25 | // Print the sum 26 | printf("Sum of all elements: %d\n", sum); 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /8.2Matrix_Addition.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int m, n; 5 | 6 | // Get the dimensions of the matrices 7 | printf("Enter the number of rows and columns:\n"); 8 | scanf("%d %d", &m, &n); 9 | 10 | // Declare matrices 11 | int mat1[m][n], mat2[m][n], res[m][n]; 12 | 13 | // Input elements for the first matrix 14 | printf("Enter elements of matrix 1:\n"); 15 | for (int i = 0; i < m; i++) { 16 | for (int j = 0; j < n; j++) { 17 | printf("mat1[%d][%d] = ", i, j); 18 | scanf("%d", &mat1[i][j]); 19 | } 20 | } 21 | 22 | // Input elements for the second matrix 23 | printf("Enter elements of matrix 2:\n"); 24 | for (int i = 0; i < m; i++) { 25 | for (int j = 0; j < n; j++) { 26 | printf("mat2[%d][%d] = ", i, j); 27 | scanf("%d", &mat2[i][j]); 28 | } 29 | } 30 | 31 | // Add matrices and store the result in the third matrix 32 | for (int i = 0; i < m; i++) { 33 | for (int j = 0; j < n; j++) { 34 | res[i][j] = mat1[i][j] + mat2[i][j]; 35 | } 36 | } 37 | 38 | // Print the result matrix 39 | printf("Resultant Matrix:\n"); 40 | for (int i = 0; i < m; i++) { 41 | for (int j = 0; j < n; j++) { 42 | printf("%d ", res[i][j]); 43 | } 44 | printf("\n"); 45 | } 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /9.2Matrix3D_multiplication.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define X 2 // Dimension 1 of the 3D matrix 4 | #define Y 2 // Dimension 2 of the 3D matrix 5 | #define Z 2 // Dimension 3 of the 3D matrix 6 | 7 | int main() { 8 | // Define the 3D matrices A, B, and C 9 | int A[X][Y][Z], B[X][Y][Z], C[X][Y][Z]; 10 | 11 | // Input the elements for matrix A 12 | printf("Enter elements of matrix A (2 x 2 x 2):\n"); 13 | for (int i = 0; i < X; i++) { 14 | for (int j = 0; j < Y; j++) { 15 | for (int k = 0; k < Z; k++) { 16 | //printf("A[%d][%d][%d]: ", i, j, k); 17 | scanf("%d", &A[i][j][k]); 18 | } 19 | } 20 | } 21 | 22 | // Input the elements for matrix B 23 | printf("Enter elements of matrix B (2 x 2 x 2):\n"); 24 | for (int i = 0; i < X; i++) { 25 | for (int j = 0; j < Y; j++) { 26 | for (int k = 0; k < Z; k++) { 27 | // printf("B[%d][%d][%d]: ", i, j, k); 28 | scanf("%d", &B[i][j][k]); 29 | } 30 | } 31 | } 32 | 33 | // Multiply corresponding elements of A and B, and store the result in C 34 | for (int i = 0; i < X; i++) { 35 | for (int j = 0; j < Y; j++) { 36 | for (int k = 0; k < Z; k++) { 37 | C[i][j][k] = A[i][j][k] * B[i][j][k]; 38 | } 39 | } 40 | } 41 | 42 | // Display the resulting matrix C 43 | printf("Result matrix C (2 x 2 x 2):\n"); 44 | for (int i = 0; i < X; i++) { 45 | for (int j = 0; j < Y; j++) { 46 | for (int k = 0; k < Z; k++) { 47 | printf("%d ",C[i][j][k]); 48 | } 49 | printf("\n"); 50 | } 51 | printf("\n"); 52 | } 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | BCA I sem Programming in C Language Assignment. 2 | --------------------------------------------------------------------------------