c programming exercises

C Programming Exercise with Explanations

Step 1: Declare and Initialize Variables

#include <stdio.h>

int main() {
    int num1 = 5;
    int num2 = 10;
    int sum = num1 + num2;
    printf("The sum of %d and %d is %d\n", num1, num2, sum);
    return 0;
}
  • In this step, we declare and initialize three integer variables: num1, num2, and sum. We assign the values 5 and 10 to num1 and num2 respectively. Then, we calculate the sum of num1 and num2 and store it in the variable sum. Finally, we print the sum using the printf function. Step 2: User Input and Conditional Statement
#include <stdio.h>

int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    if (num % 2 == 0) {
        printf("%d is even\n", num);
    } else {
        printf("%d is odd\n", num);
    }
    return 0;
}
  • In this step, we declare an integer variable num to store user input. We prompt the user to enter a number using printf and then use scanf to read the input and store it in num. Next, we use a conditional statement (if-else) to check if the number is even or odd and print the result accordingly. Step 3: Looping Structure
#include <stdio.h>

int main() {
    int i;
    for (i = 1; i <= 5; i++) {
        printf("%d\n", i);
    }
    return 0;
}
  • In this step, we use a for loop to print the numbers from 1 to 5. The loop initializes the variable i to 1, then executes the loop body as long as i is less than or equal to 5, and increments i by 1 in each iteration. Inside the loop, we use printf to print the value of i. Step 4: Function Definition and Call
#include <stdio.h>

int add(int a, int b) {
    return a + b;
}

int main() {
    int num1 = 5, num2 = 3;
    int result = add(num1, num2);
    printf("The sum of %d and %d is %d\n", num1, num2, result);
    return 0;
}
  • In this step, we define a function add that takes two integer parameters a and b and returns their sum. Inside the main function, we declare two integer variables num1 and num2 and initialize them with values 5 and 3. We then call the add function with num1 and num2 as arguments and store the result in the variable result. Finally, we print the result using printf. Step 5: Array Declaration and Access
#include <stdio.h>

int main() {
    int arr[5] = {2, 4, 6, 8, 10};
    for (int i = 0; i < 5; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}
  • In this step, we declare an integer array arr with 5 elements and initialize it with the values 2, 4, 6, 8, and 10. Then, we use a for loop to iterate through the array and print each element using printf. Step 6: Structure Definition and Usage
#include <stdio.h>

struct Point {
    int x;
    int y;
};

int main() {
    struct Point p1 = {3, 5};
    printf("Coordinates of p1: (%d, %d)\n", p1.x, p1.y);
    return 0;
}
  • In this step, we define a structure Point with two integer members x and y. Inside the main function, we declare a variable p1 of type Point and initialize it with the coordinates (3, 5). Then, we print the coordinates of p1 using printf.