Day 7 of Programming in C

Take 10 elements in an array. Find a particular elememt in the array with its index.


#include <stdio.h>
void main(){
    int arr[10],num,count = 0;
    for(int i=0;i<10;i++){
        printf("Enter the element at index %d = ",i);
        scanf(" %d",&arr[i]);
    }
    printf("Enter the number you want to search : ");
    scanf("%d",&num);
    for(int i=0;i<10;i++){
        if(num == arr[i]){
            printf("The number %d is found on index %d\n",num,i);
            count++;
        }
    }
    if(count==0){
        printf("The number is not present in the array");
    }
}
    
    
Step 1: Include the standard input/output library `stdio.h`.
Step 2: Define the `main` function (note: `void main()` is generally not recommended, `int main()` is preferred).
Step 3: Declare an integer array `arr` of size 10, an integer variable `num`, and an integer variable `count` initialized to 0.
Step 4: Start a `for` loop, initializing integer `i` to 0, continuing as long as `i` is less than 10, and incrementing `i` by 1 in each iteration.
Step 5: Inside the loop, prompt the user to enter an element at index `i` using `printf`.
Step 6: Read an integer from the input and store it in `arr[i]` using `scanf`.
Step 7: The `for` loop continues until `i` is greater than or equal to 10, filling the array `arr` with user-provided values.
Step 8: Prompt the user to enter the number they want to search for using `printf`.
Step 9: Read an integer from the input and store it in `num` using `scanf`.
Step 10: Start another `for` loop, initializing integer `i` to 0, continuing as long as `i` is less than 10, and incrementing `i` by 1 in each iteration.
Step 11: Inside the second loop, check if `num` is equal to `arr[i]` using an `if` statement.
Step 12: If `num` is equal to `arr[i]`, print a message indicating that the number `num` is found at index `i` using `printf`, and increment the `count` variable by 1.
Step 13: The second `for` loop continues until `i` is greater than or equal to 10, checking all elements of the array.
Step 14: After the second loop, check if `count` is equal to 0 using an `if` statement.
Step 15: If `count` is equal to 0, print a message indicating that the number is not present in the array using `printf`.
Step 16: The program completes.

Take 100 elements in an array between 0 to 5.Print the number of occurences of 1,2,3,4 and 5.


#include <stdio.h>
void main(){
    int arr[100],zero,one,two,three,four,five;
    zero=one=two=three=four=five=0;
    for(int i=0;i<100;i++){
        printf("Enter a number between 0 to 5 : ");
        while(scanf("%d ",&arr[i])!= 1 || !(arr[i] >= 0 && arr[i] <= 5)){
            printf("Invalid input!! Please enter a number in the given range\n");
        }
    }
    for(int i=0;i<100;i++){
        switch (arr[i]) {
            case 0: zero++; break;
            case 1: one++; break;
            case 2: two++; break;
            case 3: three++; break;
            case 4: four++; break;
            case 5: five++; break;
        }
    }
    printf("Number of zeros = %d\nNumber of ones = %d\nNumber of twos = %d\nNumber of threes = %d\nNumber of fours = %d\nNumber of fives = %d",zero,one,two,three,four,five);
}

        
    
    
Step 1: Include the standard input/output library `stdio.h`.
Step 2: Define the `main` function (note: `void main()` is generally not recommended, `int main()` is preferred).
Step 3: Declare an integer array `arr` of size 100, and integer variables `zero`, `one`, `two`, `three`, `four`, and `five`.
Step 4: Initialize all count variables (`zero`, `one`, `two`, `three`, `four`, `five`) to 0.
Step 5: Start a `for` loop, initializing integer `i` to 0, continuing as long as `i` is less than 100, and incrementing `i` by 1 in each iteration.
Step 6: Inside the loop, prompt the user to enter a number between 0 and 5 using `printf`.
Step 7: Start a `while` loop that continues until valid input is received.
  • Substep 7.1: Attempt to read an integer from the input and store it in `arr[i]` using `scanf`.
  • Substep 7.2: Check if `scanf` successfully read an integer (returns 1) and if the entered number is within the range 0 to 5.
  • Substep 7.3: If the input is invalid (either `scanf` fails or the number is out of range), print an error message using `printf`.
Step 8: The `while` loop repeats until a valid input is received, ensuring `arr[i]` contains a number between 0 and 5.
Step 9: The first `for` loop continues until all 100 elements of `arr` are filled with valid inputs.
Step 10: Start a second `for` loop, initializing integer `i` to 0, continuing as long as `i` is less than 100, and incrementing `i` by 1 in each iteration.
Step 11: Inside the second loop, use a `switch` statement to check the value of `arr[i]`.
  • Substep 11.1: If `arr[i]` is 0, increment `zero`.
  • Substep 11.2: If `arr[i]` is 1, increment `one`.
  • Substep 11.3: If `arr[i]` is 2, increment `two`.
  • Substep 11.4: If `arr[i]` is 3, increment `three`.
  • Substep 11.5: If `arr[i]` is 4, increment `four`.
  • Substep 11.6: If `arr[i]` is 5, increment `five`.
  • Substep 11.7: The `break` statements ensure that only the corresponding case is executed.
Step 12: The second `for` loop continues until all 100 elements of `arr` are processed, counting the occurrences of each number.
Step 13: Print the counts of each number (0 to 5) using `printf`.
Step 14: The program completes.

Take 5 elements from the keyboard inside an array and sort it in ascending order.


#include <stdio.h>
void main()
{
    int arr[5];
    for (int i = 0; i < 5; i++)
    {
        printf("Enter the value: ");
        scanf("%d", &arr[i]);
    }
    printf("Before Sorting\n");
    for (int i = 0; i < 5; i++)
    {
        printf("%d ", arr[i]);
    }
    for (int j = 0; j < 4; j++)
    {
        for (int i = 0; i < 4 - j; i++)
        {
            if (arr[i] > arr[i + 1])
            {
                arr[i] = arr[i] + arr[i + 1] - (arr[i + 1] = arr[i]);
            }
        }
    }
    printf("\nAfter Sorting\n");
    for (int i = 0; i < 5; i++)
    {
        printf("%d ", arr[i]);
    }
}

        
    
Step 1: Include the standard input/output library `stdio.h`.
Step 2: Define the `main` function (note: `void main()` is generally not recommended, `int main()` is preferred).
Step 3: Declare an integer array `arr` of size 5.
Step 4: Input array elements using a `for` loop and `scanf`.
Step 5: Print the array elements before sorting.
Step 6: Implement the Bubble Sort algorithm using nested `for` loops.
  • Substep 6.1: The outer `for` loop (controlled by `j`) iterates from 0 to 3, representing the passes of the Bubble Sort. Each pass aims to move the largest unsorted element to its correct position at the end.
  • Substep 6.2: The inner `for` loop (controlled by `i`) iterates from 0 to `4 - j - 1`. Its range decreases with each pass of the outer loop, as the largest elements are already sorted at the end.
  • Substep 6.3: Inside the inner loop, the `if` statement `if (arr[i] > arr[i + 1])` compares adjacent elements. If `arr[i]` is greater than `arr[i + 1]`, it means they are in the wrong order.
  • Substep 6.4: The line `arr[i] = arr[i] + arr[i + 1] - (arr[i + 1] = arr[i]);` performs a swap without using a temporary variable.
    • Substep 6.4.1: `arr[i + 1] = arr[i]` assigns the value of `arr[i]` to `arr[i + 1]`.
    • Substep 6.4.2: `arr[i] = arr[i] + arr[i + 1] - arr[i + 1]` calculates the original value of `arr[i + 1]` (which is now in `arr[i]`) and assigns it to `arr[i]`.
    • Substep 6.4.3: This swap effectively moves larger elements towards the end of the array, simulating the "bubbling" effect.
  • Substep 6.5: The inner loop repeats, comparing and swapping adjacent elements until the largest unsorted element is at its correct position.
  • Substep 6.6: The outer loop repeats for each pass, gradually sorting the array.
Step 7: Print the array elements after sorting.

Take 50 elements in an array from the keyboard.Find their mean,variance and standard deviation.


 #include <stdio.h>
 #include <math.h>
void main() {
    float arr[50], sum = 0, mean, variance = 0, std_dev;

    printf("Enter 50 numbers:\n");
    for (int i = 0; i < 50; i++) {
        scanf("%f", &arr[i]);
        sum += arr[i];
    }

    mean = sum / 50;

    for (int i = 0; i < 50; i++) {
        variance += (arr[i] - mean) * (arr[i] - mean);
    }
    variance /= 50;
    std_dev = sqrt(variance);
    printf("\nMean = %.2f", mean);
    printf("\nVariance = %.2f", variance);
    printf("\nStandard Deviation = %.2f\n", std_dev);
}   
 
    
Step 1: Include the standard input/output library `stdio.h`.
Step 2: Include the math library `math.h`. This library provides the `sqrt()` function for calculating the square root.
Step 3: Define the `main` function (note: `void main()` is generally not recommended, `int main()` is preferred).
Step 4: Declare a float array `arr` of size 50 to store the input numbers, and float variables `sum`, `mean`, `variance`, and `std_dev`. Initialize `sum` and `variance` to 0.
Step 5: Prompt the user to enter 50 numbers using `printf`.
Step 6: Read 50 floating-point numbers from the user and store them in the `arr` array using a `for` loop and `scanf`.
  • Substep 6.1: The `for` loop iterates from 0 to 49, reading one number per iteration.
  • Substep 6.2: `scanf("%f", &arr[i])` reads a floating-point number and stores it in `arr[i]`.
  • Substep 6.3: `sum += arr[i]` accumulates the sum of the input numbers.
Step 7: Calculate the mean of the numbers by dividing the `sum` by 50 and store it in the `mean` variable.
  • Substep 7.1: `mean = sum / 50;` computes the average of the 50 numbers.
Step 8: Calculate the variance of the numbers using another `for` loop.
  • Substep 8.1: The `for` loop iterates from 0 to 49.
  • Substep 8.2: `variance += (arr[i] - mean) * (arr[i] - mean);` calculates the squared difference between each number and the mean, and adds it to the `variance`. This step sums up the squared deviations.
  • Substep 8.3: `variance /= 50;` divides the sum of squared deviations by 50 to get the average squared deviation, which is the variance.
Step 9: Calculate the standard deviation by taking the square root of the `variance` using the `sqrt()` function from the `math.h` library, and store it in the `std_dev` variable.
  • Substep 9.1: `std_dev = sqrt(variance);` calculates the square root of the variance, giving the standard deviation.
Step 10: Print the calculated mean, variance, and standard deviation using `printf`, formatted to two decimal places.
  • Substep 10.1: `printf("\nMean = %.2f", mean);` displays the mean with two decimal places.
  • Substep 10.2: `printf("\nVariance = %.2f", variance);` displays the variance with two decimal places.
  • Substep 10.3: `printf("\nStandard Deviation = %.2f\n", std_dev);` displays the standard deviation with two decimal places, followed by a newline.
Step 11: The program completes.

Write a program to reverse an integer using array.


#include <stdio.h>
void main(){
    int arr[20], num,count=0;
    printf("Enter the number : ");
    scanf("%d",&num);
    while(num!=0){
        arr[count] = num%10;
        num /= 10;
        count++;
    }
    printf("Reversed number = ");
    for(int i=0;i<count;i++){
        printf("%d", arr[i]);
    }
}


Step 1: Include the standard input/output library `stdio.h`.
Step 2: Define the `main` function (note: `void main()` is generally not recommended, `int main()` is preferred).
Step 3: Declare an integer array `arr` of size 20, an integer variable `num`, and an integer variable `count` initialized to 0.
Step 4: Prompt the user to enter a number using `printf`.
Step 5: Read an integer from the input and store it in the `num` variable using `scanf`.
Step 6: Start a `while` loop that continues as long as `num` is not equal to 0.
  • Substep 6.1: `arr[count] = num % 10;` calculates the remainder when `num` is divided by 10, which gives the last digit of `num`, and stores it in `arr[count]`.
  • Substep 6.2: `num /= 10;` divides `num` by 10, effectively removing the last digit.
  • Substep 6.3: `count++;` increments the `count` variable, which keeps track of the number of digits extracted.
Step 7: The `while` loop continues until `num` becomes 0, extracting all digits of the input number in reverse order and storing them in the `arr` array.
Step 8: Print the string "Reversed number = " using `printf`.
Step 9: Start a `for` loop, initializing integer `i` to 0, continuing as long as `i` is less than `count`, and incrementing `i` by 1 in each iteration.
Step 10: Inside the loop, print the value of `arr[i]` using `printf`. This prints the digits stored in the array in the order they were extracted, which is the reversed order of the original number's digits.
Step 11: The `for` loop continues until all digits in the array are printed.
Step 12: The program completes, displaying the reversed number on the console.

Comments

Popular Posts