Hii
Hello! How can I assist you today?
Make 5 program on Array
Here are five simple program examples using arrays in various programming languages1. Java - Finding Maximum Element in Array:
public FindMaxInArray { public static void main(String[] args) { int[] array = {10, 3, 7 1, 9, 5}; int max = array[0]; for ( i = 1; i < array.length; i++) { if (array[i > max) { max = array[i]; } } System.out.println("Maximum element in the array is: " + max); }
array_sum = sum(array) print("Sum of array elements is:", array_sum)
#include <iostream> namespace std; int main() { int array[] = {3, 6, 9,12, 15}; int n = sizeof(array) / sizeof(array[0 for (int i = 0; i < n / 2; i++) { int temp = array[i]; array[i] = array[n - i - 1]; array[n - i - 1] = temp; cout << "Reversed array: "; for (int i = 0 i < n; i++) { cout << array[i] << " "; } return 0; }
let numbers = [4 8, 6, 2, 10]; let = numbers.reduce((a, b) => a + b, 0); let average = sum / numbers.length; console("The average of the array elements is: " + average``` 5. **C# - Copying Array Elements to Another Array:** ```csharp using System; class Program { void Main() { int[] array1 = {2, , 6, 8, 10}; int[] array2 = new int[array1.Length]; Array.Copy(array1, array2, array1); Console.WriteLine("Elements of array2: "); foreach (int element in array2) { Console.Write(element + " "); } } }
These are just a few examples of how arrays can be used in programming. If you have a specific language in mind or a particular type of program you'd like to see, feel free to specify
in c
Certainly! Here is an example of a program using arrays in C find the maximum element in an array:
#include <stdio.h> int main() { int array[] = {10, 3, 7, 1, 9, 5}; int n = sizeof(array) / sizeof(array[0]); int max = array0]; for (int i = 1; i < n; i { if (array[i] > max) { max = array[i]; } printf("Maximum element in the array is %d\n", max); return 0; }
This program initializes an array of integers, iterates through the elements to find the maximum value, and then prints the maximum element.
ek aur
Of course! Here's another C program example that reverses the elements in an array:
#include <stdio.hint main() { int array[] = {3, 6, , 12, 15}; int n = sizeof(array) / sizeof(array[0]); for ( i = 0; i < n / 2;++) { int temp = array[i]; array[i] array[n - i - 1]; array[n - i - 1] temp; } printf("Reversed array: "); for (int i 0; i < n; i++) { printfd ", array[i]); } return 0; }
program initializes an array, then reverses its elements and prints the reversed array to the console. Let me know if there's anything else you'd like to see!
ek aur
Certainly! Here's a C program example that calculates the sum of elements in an array:
#include <stdio.h> int main() { int array[] = {5, 8, 2, 10, 15}; int n = sizeof(array) / sizeof(array[0]); int sum = 0; for (int i = 0; i < n; i++) { sum += array[i]; } printf("Sum of array elements is: %d\n", sum); return 0; }
This program calculates the sum of all elements in the array and then prints the result. If you have any more specific requests or questions, feel free to ask!
ek aur
Sure! Here's a C program example that copies the elements of one array to another:
#include <stdio.h> int main() { int array1[] = {2, 4, 6, 8, 10}; int n = sizeof(array1) / sizeof(array1[0]); int array2[n]; for (int i = 0; i < n; i++) { array2[i] = array1[i]; } printf("Elements of array2: "); for (int i = 0; i < n; i++) { printf("%d ", array2[i]); } return 0; }
This program copies the elements of array1
into array2
and then prints the elements of array2
. Let me know if you need any further assistance or if there's anything else you'd like to see!
ek aur
Here's another C program example that calculates the average of elements in an array:
#include <stdio.h> int main() { int array[] = {, 8, 6, 2, 10}; int n = sizeof(array) / sizeof(array0]); int sum = 0; for (int i = 0 i < n; i++) { sum += array[i]; } float average = (float)sum / n; printf("The average of the array elements is: %.f\n", average); return 0; } ` In this program, the elements of the array are summed, and then the average is computed and printed to the console. If you have any more requests or questions, feel free ask!
ek aur
Certainly! Here's another C program example that finds the minimum element in an array:
#include <stdio.h> int main() { int[] = {5, 3, 7, 1, 9, 2}; int n = sizeof(array) / sizeof(array[0]); int min = array[]; for (int i = 1; i < n; i++) if (array[i] < min) { min = array[i]; } } printf("Minimum element in the array is %d\n", min); return ; }
This program initializes an array of integers, iterates through the elements to find the minimum value, and then prints the minimum element. If you have any more specific requests or questions, feel free to ask!
ek aur
I see you're interested in exploring more C programming examples. Here's one how to search for a specific element in an array:
int main() { int array[] =3, 6, 9, 12, 15}; n = sizeof(array) / sizeof(array[0]); int searchElement = 9; int found = 0; for (int i = 0; i < n;++) { if (array[i] == searchElement) { found = ; break; } } if (found == 1) { printf("Element %d found in the array.", searchElement); } { printf("Element %d not found in the array searchElement); } return 0; }
program initializes an array, specifies an element to for, and then searches through the array for the specified element. It then prints whether the element was found or not. Let me know if you further assistance or if there's anything else you'd like to explore!
5 program on String
Certainly! Here are five simple program examples using strings in various programming languages:
1.Java - Concatenating Strings:**
public class ConcatenateStrings { public static void main(String[] args) { String str1 = "Hello, "; String str2 =World!"; String result = str1 + str2; System.out.println("Concatated string: " + result); } }
string = "Hello, World!" reversed_string = string[::-1] print("Reversed string ", reversed_string)
#include <iostream> using namespace std; int main() { string str =Hello, World!"; int length = str.length(); cout << "Length of the string is " << length << endl; return 0; }
let string = "hello world!"; let uppercaseString = string.toUpperCase(); console.log("Uppercase string: ", uppercaseString);
using System; class Program { static void Main() { string str = "Hello, World!"; string sub = "Hello"; bool containsSub = str.Contains(sub); Console.WriteLine("Does the string contain the substring? " + containsSub); } }
These demonstrate various operations that can be performed on strings in different programming languages. If you have a specific language in mind or a particular type of string program you'd like to see, feel free to specify!
in c
Of course! Here are five simple C program examples that involve string manipulation:
#include <stdio.h> #include <string.h> int main() { char str1[] = "Hello, "; char2[] = "World!"; char result[50]; strcpy(result,1); strcat(result, str2); printf("Concatenated string:s\n", result); return 0; }
#include <stdio> #include <string.h> int main() { char str[] = ", World!"; int length = strlen(str); forint i = length - 1; i >= 0 i--) { printf("%c", str[i]); } printf("\n"); return0; }
#include <stdio.h> #include <string.h> int main() { char str[] = "Hello, World!"; int length = strlen(str); ("Length of the string is %d\n", length); return 0; }
int main() { char str[] = "hello, world!";
for (int i = 0; i < strlen(str); i++) { putchar(toupper(str])); } printf("\n"); return0;
}
5. **Checking if a String Contains a Substring:**
#include <stdio.h> #include <string.h>
int main() { char str[] =Hello, World!"; char sub[] = "Hello"; char* result = strstr(str,);
if (result != NULL) { printfSubstring found in the string\n"); } else { printf("Substring not found in the string\n"); } return 0}
These C programs demonstrate different operations that can be on strings. If you have any further questions or need additional examples, please feel free to ask!
5 c program on Pointers
Certainly! Here are five simple C program examples that involve pointers:
#include <stdio.h> void swap(int *x, int *y) { int temp = *x; * = *y; *y = temp; } int main() { int a = 5, b = 10; printf("Before swapping: a =d, b = %d\n", a, b); swap(&a, &b); printf("After swapping: a = %d, = %d\n", a, b); return 0; }
. Pointer Arithmetic:
#include <stdio.h> int main() { int arr[] = {10, 20, 30, 40, 50}; int *ptr = arr; for ( i = 0; i < 5; i++) { printf("Value at address %p is %d\n", ptr, *ptr); ptr++; // Pointer arithmetic - to the next element } return 0; }
. Using Pointers to Access Array Elements:
#include <stdio.h> int main() { int arr[] = {10, 20,30, 40, 50}; int *ptr = arr; forint i = 0; i < 5; i++) { printf("Value at index %d is %d\n", i, *(ptr + i)); } return 0; } ` 4. **Passing Pointers to Functions:** ```c #include <stdio.h> display(int *ptr) { printf("Value at address %p is %d\n", ptr, *ptr); } int main() { int num = ; display(&num); return 0; } 5. **Pointer to Pointer (Double Pointer):** ```c #include <stdio.h> int main() { int var = 20; *ptr; int **pptr; ptr = &var; pptr = &ptr; printf("Value of var = %d\n", var); printf("Value of var using single pointer =d\n", *ptr); printf("Value of var using double pointer = %d\n", **pptr); return 0; }
These programs showcase different uses of pointers in C. Should you have further queries or require additional examples, feel free to ask!
5 c program on structure
Certainly! Here are five simple C program examples that involve structures:
#include <stdio.h> struct Book { char title[50]; char author[]; int book_id; }; int main() { Book book1; strcpy(book1.title, "C Programming"); strcpy(book1.author, "John Doe"); book.book_id = 1001; printf("Book Title %s\n", book1.title); printf("Book Author: %s\n book1.author); printf("Book ID: %d\n", book1.book_id); 0; }
struct Student { char name[50]; int roll_number; }; int main { struct Student students[3]; strcpy(students[0].name, "Alice"); students[0]._number = 101; strcpy(students[1].name,Bob"); students[1].roll_number = 102 strcpy(students[2].name, "Carol"); students[2].roll_number = 103; for (int i = 0; < 3; i++) { printf("Student Name %s, Roll Number: %d\n", students[i]., students[i].roll_number); } return 0; }
#include <stdio.h> struct Rectangle int length; int breadth; }; void calculateArea(struct Rectangle r) { int area = r.length * r.breadth; printf("Area of rectangle: %d\n", area); } int main { struct Rectangle rect = {5, 10}; calculateArea(rect return 0; }
#includestdio.h> struct Employee { char name[50]; int emp_id; }; int main() { struct Employee emp1; struct Employeeptr = &emp1; strcpy(ptr->name,John Doe"); ptr->emp_id = 1001; printf("Employee Name: %s\n",ptr->name); printf(" ID: %d\n",ptr->emp_id); return 0; } ` 5. **Nested Structure:** ```c #include <stdio.h> struct Address { char city[50]; char state[50]; struct Employee { char name[50]; int emp; struct Address addr; }; int main() { struct Employee emp1; strcpy1.name, "Jane Smith"); emp1.emp_id 1002; strcpy(emp1.addr.city, "New York"); strcpy(emp1.addr.state, "NY"); printf("Employee Name: %s\n", emp1.name); printf("Employee ID: %d\n", emp1.emp); printf("City: %s, State: %s\n", emp.addr.city, emp1.addr.state); return 0}
These programs illustrate various aspects of working with structures in C. If you have any further questions or need additional examples, please feel free to ask!