Featured Post

Arrays in C : Examples | Class 10 | Computer Science (E) | SEBA | ASSEB

 Arrays in C : Examples



C program to find the summation of a series of numbers using an array:


#include <stdio.h>

int main()

{

    int numbers[100];  // Array to store numbers

    int totalElements, sum = 0;

 

    // Ask user for total number of elements

    printf("Enter total number of elements: ");

    scanf("%d", &totalElements);

 

    // Check if number of elements is within valid range

    if (totalElements <= 0 || totalElements > 100) {

        printf("Please enter a number between 1 and 100.\n");

        return 1; // Exit the program

    }

 

    // Taking input for array elements

    printf("\nEnter %d numbers:\n", totalElements);

    for (int i = 0; i < totalElements; i++) {

        printf("Number %d: ", i + 1);

        scanf("%d", &numbers[i]);

        sum += numbers[i];  // Add each number to sum

    }

    // Display the summation

    printf("\nSummation of the series = %d\n", sum);

 

    return 0;

}

C program to find the largest among a series of numbers using an array:

 

#include <stdio.h>

 

int main()

{

    int numbers[100];

    int totalElements, largest;

 

    // Ask user for the number of elements

    printf("Enter total number of elements: ");

    scanf("%d", &totalElements);

 

    // Check for valid input

    if (totalElements <= 0 || totalElements > 100) {

        printf("Please enter a number between 1 and 100.\n");

        return 1;

    }

 

    // Input numbers from the user

    printf("\nEnter %d numbers:\n", totalElements);

    for (int i = 0; i < totalElements; i++) {

        printf("Number %d: ", i + 1);

        scanf("%d", &numbers[i]);

    }

 

    // Assume the first number is the largest

    largest = numbers[0];

 

    // Compare with the rest of the numbers

    for (int i = 1; i < totalElements; i++) {

        if (numbers[i] > largest) {

            largest = numbers[i];

        }

    }

 

    // Display the largest number

    printf("\nThe largest number is: %d\n", largest);

 

    return 0;

}

c program to find the even numbers in a series of numbers using an array

#include <stdio.h>

 

int main()

{

    int numbers[100];

    int totalElements;

 

    // Ask user for the number of elements

    printf("Enter total number of elements: ");

    scanf("%d", &totalElements);

 

    // Check for valid input

    if (totalElements <= 0 || totalElements > 100) {

        printf("Please enter a number between 1 and 100.\n");

        return 1;

    }

 

    // Input numbers from the user

    printf("\nEnter %d numbers:\n", totalElements);

    for (int i = 0; i < totalElements; i++) {

        printf("Number %d: ", i + 1);

        scanf("%d", &numbers[i]);

    }

 

    // Display even numbers

    printf("\nEven numbers in the series are:\n");

    int found = 0;

    for (int i = 0; i < totalElements; i++) {

        if (numbers[i] % 2 == 0) {

            printf("%d ", numbers[i]);

            found = 1;

        }

    }

 

    if (!found) {

        printf("No even numbers found.");

    }

 

    printf("\n");

 

    return 0;

}

## Write a C program and declare an integer type array with capacity 7. Take input to the array from the keyboard. Display the 8th element of the array. Hint: display num[7] if num is the name of the array. Analyze the output.

 

#include <stdio.h>

int main() {

    int num[7];

    int i;

    // Input from the user

    printf("Enter 7 integers:\n");

    for (i = 0; i < 7; i++) {

        scanf("%d", &num[i]);

    }

    // Trying to access the 8th element (index 7)

    printf("The 8th element (num[7]) is: %d\n", num[7]);

 

    return 0;

}

# Write a C program and declare an integer type array with 7 elements in it. Display the address of the individual elements in the array.

 

#include <stdio.h>

int main() {

    int arr[7];

    int i;

    printf("Enter 7 integers:\n");

    for (i = 0; i < 7; i++) {

        scanf("%d", &arr[i]);

    }

    printf("Addresses of individual elements in the array:\n");

    for (i = 0; i < 7; i++) {

        printf("Address of arr[%d]: %p\n", i, (void*)&arr[i]);

    }

    return 0;

}

## Write a C program and declare two integer type arrays, each with capacity 7. Take input only to the first array. Write a loop to copy the elements of the first array to the second one. Display the elements of the second array.

 

#include <stdio.h>

int main() {

    int arr1[7], arr2[7];

    int i;

    printf("Enter 7 integers for the first array:\n");

    for (i = 0; i < 7; i++) {

        scanf("%d", &arr1[i]);

    }

    // Copy elements from arr1 to arr2

    for (i = 0; i < 7; i++) {

        arr2[i] = arr1[i];

    }

    // Display elements of arr2

    printf("Elements of the second array:\n");

    for (i = 0; i < 7; i++) {

        printf("arr2[%d] = %d\n", i, arr2[i]);

    }

    return 0;

}

## Write a c program to find the summation of the series 1+2+3+4+5+........+n

#include <stdio.h>

int main() {

    int n, sum = 0;

    // Ask the user for input

    printf("Enter a positive integer n: ");

    scanf("%d", &n);

    // Check if the input is positive

    if (n < 1) {

        printf("Please enter a positive integer greater than 0.\n");

        return 1;

    }

 

    // Calculate the sum using a loop

    for (int i = 1; i <= n; i++) {

        sum += i;

    }

    // Print the result

    printf("The summation of the series 1 + 2 + ... + %d is: %d\n", n, sum);

 

    return 0;

}

## a c program to find the all even numbers in a series of numbers using array.

#include <stdio.h>

 

int main() {

    int n, i;

 

    printf("Enter the number of elements: ");

    scanf("%d", &n);

 

    int numbers[n];  // Declare array of size n

 

    printf("Enter %d numbers:\n", n);

    for(i = 0; i < n; i++) {

        scanf("%d", &numbers[i]);

    }

 

    printf("Even numbers in the series are:\n");

    for(i = 0; i < n; i++) {

        if(numbers[i] % 2 == 0) {

            printf("%d ", numbers[i]);

        }

    }

 

    printf("\n");

    return 0;

}

### A C program to find the largest number among a series using an array.

#include <stdio.h>

 

int main() {

    int n, i, largest;

 

    printf("Enter the number of elements: ");

    scanf("%d", &n);

 

    int numbers[n];  // Declare array of size n

 

    printf("Enter %d numbers:\n", n);

    for(i = 0; i < n; i++) {

        scanf("%d", &numbers[i]);

    }

 

    largest = numbers[0];  // Assume first element is the largest

 

    for(i = 1; i < n; i++) {

        if(numbers[i] > largest) {

            largest = numbers[i];

        }

    }

 

    printf("The largest number is: %d\n", largest);

 

    return 0;

}

## A c program to search a number from a series of numbers using an array.

#include <stdio.h>

 

int main() {

    int n, i, search, found = 0;

 

    printf("Enter the number of elements: ");

    scanf("%d", &n);

 

    int numbers[n];  // Declare array of size n

 

    printf("Enter %d numbers:\n", n);

    for(i = 0; i < n; i++) {

        scanf("%d", &numbers[i]);

    }

 

    printf("Enter the number to search: ");

    scanf("%d", &search);

 

    for(i = 0; i < n; i++) {

        if(numbers[i] == search) {

            found = 1;

            break;  // Exit loop when number is found

        }

    }

 

    if(found)

{

        printf("Number %d found at position %d (index %d)\n", search, i + 1, i);

    }

else {

        printf("Number %d not found in the array.\n", search);

    }

 

    return 0;

}

## a c program to handle a char array as string.

#include <stdio.h>

 

int main() {

    char str[100];

 

    printf("Enter a string: ");

    fgets(str, 100, stdin);

 

    printf("You entered: %s", str);

 

    return 0;

}

## write a c program to display the string characters.

#include <stdio.h>

 

int main() {

    char str[100];  // Declare a string of max size 100

    int i = 0;

 

    // Ask the user to input a string

    printf("Enter a string: ");

    fgets(str, sizeof(str), stdin);  // Reads input including spaces

 

    // Display each character in the string

    printf("Characters in the string:\n");

    while (str[i] != '\0') {

        printf("Character %d: %c\n", i, str[i]);

        i++;

    }

 

    return 0;

}

## write a c program to find the largest number among a series of numbers using an array


#include <stdio.h>


int main() {

    int n, i;

    float numbers[100], max;


    // Ask user for number of elements

    printf("Enter the number of elements (up to 100): ");

    scanf("%d", &n);


    // Validate input

    if (n <= 0 || n > 100) {

        printf("Invalid number of elements.\n");

        return 1;

    }


    // Input numbers

    printf("Enter %d numbers:\n", n);

    for (i = 0; i < n; i++) {

        printf("Number %d: ", i + 1);

        scanf("%f", &numbers[i]);

    }


    // Initialize max with first element

    max = numbers[0];


    // Find the largest number

    for (i = 1; i < n; i++) {

        if (numbers[i] > max) {

            max = numbers[i];

        }

    }


    // Display the result

    printf("The largest number is: %.2f\n", max);


    return 0;

}