C Program

C Program to Increment every Element of the Array by one & Print Incremented Array

c program
#include <stdio.h> // Include the standard I/O header file for using printf and other I/O functions

void main() {
int i; // Declare an integer variable 'i' for loop indexing
int array[4] = {10, 20, 30, 40}; // Initialize an array of 4 integers with values 10, 20, 30, and 40

// First loop: Increment each element of the array by 1
for (i = 0; i < 4; i++) {
array[i]++; // Increment the value at array[i] by 1
}

// Second loop: Print the updated values of the array
for (i = 0; i < 4; i++) {
printf("%d\t", array[i]); // Print each element of the array followed by a tab space
}
}

Explanation of the Code:

  1. Header file inclusion:

    • #include <stdio.h>: This line tells the compiler to include the Standard Input Output library. This library is required for functions like printf() that we use for output.
  2. Main function:

    • void main(): The main function is the entry point of the program. In this case, it has a return type of void, meaning it does not return a value.
  3. Array Initialization:

    • int array[4] = {10, 20, 30, 40};: This line declares an integer array named array with 4 elements. The array is initialized with values 10, 20, 30, 40.
  4. First for loop:

    • for (i = 0; i < 4; i++): This loop runs 4 times (i.e., from i = 0 to i = 3).
    • Inside the loop, array[i]++ increments each element of the array by 1. The ++ operator is a post-increment operator, meaning it adds 1 to the current value of the element.
  5. Second for loop:

    • for (i = 0; i < 4; i++): This loop also runs 4 times.
    • printf("%d\t", array[i]);: This prints each element of the updated array, followed by a tab (\t) for spacing between elements.

Expected Output:

After the first loop, each element of the array is incremented by 1:

  • The original array was: [10, 20, 30, 40]
  • After incrementing: [11, 21, 31, 41]

Team Educate

About Author

Leave a comment

Your email address will not be published. Required fields are marked *

You may also like

C program to test whether a given identifier is valid or not
C Program

C Program to Test Whether a Given Identifier is Valid or Not

Rules for Valid Identifiers in C: The identifier must begin with a letter or an underscore (_). It cannot start
C program to test whether a given identifier is valid or not
C Program Compiler Design

C program to implement simple code generator

ALGORITHM:1. Start2. Get address code sequence.3. Determine current location of 3 using address (for 1st operand).4. If current location not