Basic Programming Exercises

Basic Programming Exercises

3 min read Jun 23, 2024
Basic Programming Exercises

Basic Programming Exercises

This article will provide you with a series of basic programming exercises designed to help you solidify your understanding of fundamental programming concepts. These exercises are suitable for beginners and can be solved using a variety of programming languages like Python, JavaScript, C++, Java, etc.

Exercise 1: Hello, World!

Objective: Write a program that prints the message "Hello, World!" to the console.

Example (Python):

print("Hello, World!")

Exercise 2: Area of a Triangle

Objective: Write a program that calculates the area of a triangle given its base and height.

Formula: Area = (1/2) * base * height

Example (JavaScript):

const base = 5;
const height = 10;
const area = (1/2) * base * height;
console.log("The area of the triangle is:", area);

Exercise 3: Temperature Converter

Objective: Write a program that converts temperatures between Celsius and Fahrenheit.

Formulas:

  • Celsius to Fahrenheit: F = (C * 9/5) + 32
  • Fahrenheit to Celsius: C = (F - 32) * 5/9

Example (C++):

#include 

using namespace std;

int main() {
  double celsius, fahrenheit;

  cout << "Enter temperature in Celsius: ";
  cin >> celsius;

  fahrenheit = (celsius * 9/5) + 32;
  cout << celsius << " degrees Celsius is equal to " << fahrenheit << " degrees Fahrenheit." << endl;

  return 0;
}

Exercise 4: Even or Odd

Objective: Write a program that determines if a given number is even or odd.

Example (Java):

import java.util.Scanner;

public class EvenOdd {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter a number: ");
    int number = scanner.nextInt();

    if (number % 2 == 0) {
      System.out.println(number + " is even.");
    } else {
      System.out.println(number + " is odd.");
    }
  }
}

Exercise 5: Factorial of a Number

Objective: Write a program that calculates the factorial of a given number.

Formula: Factorial of n (n!) = n * (n-1) * (n-2) * ... * 2 * 1

Example (Python):

def factorial(n):
  if n == 0:
    return 1
  else:
    return n * factorial(n-1)

number = 5
print("The factorial of", number, "is", factorial(number))

Conclusion:

These exercises cover various fundamental programming concepts and provide a solid foundation for your programming journey. Remember to practice consistently and explore different programming languages to enhance your skills. Good luck!

Featured Posts