Advertisement

Responsive Advertisement

Instructions in c

Learning Sections          show


Instructions in C

Arithmetic Instructions


#include<stdio.h>  // Include the standard input-output library

int main() {
    // Arithmetic operations
    int a = 5, b = 3;
    int sum = a + b;  // Addition
    int difference = a - b;  // Subtraction
    int product = a * b;  // Multiplication
    int quotient = a / b;  // Division

    // Print results
    printf("Sum: %d\n", sum);
    printf("Difference: %d\n", difference);
    printf("Product: %d\n", product);
    printf("Quotient: %d\n", quotient);

    return 0;
}
        

Explanation: This program demonstrates basic arithmetic operations: addition, subtraction, multiplication, and division. It prints the results using printf.

Conditional Instructions


#include<stdio.h>  // Include the standard input-output library

int main() {
    // Conditional statement
    int num = 10;

    // Check if the number is positive, negative, or zero
    if (num > 0) {
        printf("The number is positive.\n");
    } else if (num == 0) {
        printf("The number is zero.\n");
    } else {
        printf("The number is negative.\n");
    }

    return 0;
}
        

Explanation: This program uses an if-else if-else statement to check if a number is positive, negative, or zero, and prints the result.

Looping Instructions


#include<stdio.h>  // Include the standard input-output library

int main() {
    // Looping statement

    // For loop to print numbers from 1 to 5
    for (int i = 1; i < 6; i++) {
        printf("Number: %d\n", i);
    }

    return 0;
}
        

Explanation: This program uses a for loop to print numbers from 1 to 5.

Jump Instructions


#include<stdio.h>  // Include the standard input-output library

int main() {
    // Jump statement
    
    // Using break in a loop
    for (int i = 1; i < 10; i++) {
        if (i == 5) {
            break;  // Exit the loop
        }
        printf("Number: %d\n", i);
    }

    // Using continue in a loop
    for (int j = 1; j < 10; j++) {
        if (j == 5) {
            continue;  // Skip the rest of the loop body
        }
        printf("Number: %d\n", j);
    }

    return 0;
}
        

Explanation: This program demonstrates the use of the break and continue statements in loops.

Explanation:

This HTML document contains several simple C programs with syntax highlighting using CSS. Each section demonstrates a different type of instruction in C:

  • Arithmetic Instructions: Demonstrates basic arithmetic operations: addition, subtraction, multiplication, and division.
  • Conditional Instructions: Uses if-else if-else statements to check conditions and execute different code blocks.
  • Looping Instructions: Uses a for loop to iterate over a range of numbers.
  • Jump Instructions: Demonstrates the use of break to exit a loop and continue to skip the rest of the current loop iteration.