C++ Codes - Basic to Advanced 💻
Open in Telegram
💡 Daily C++ Codes with Output & Logic Any queries, message 👉 @sai7981 🤖 Get instant code explanations: @cpp_codes_bot 🔗 Join now: @cpp_code_snippets #cpp #dsa #coding #programming
Show more3 188
Subscribers
No data24 hours
+357 days
+12030 days
Posts Archive
//Calculate the least common multiple of two numbers
public class LCMCalculator {
public static void main(String[] args) {
int num1 = 12; // Change this to the first number
int num2 = 18; // Change this to the second number
int lcm = (num1 * num2) / findGCD(num1, num2);
System.out.println("LCM: " + lcm);
}
// Function to find the Greatest Common Divisor (GCD).
private static int findGCD(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
}
//Calculate the greatest common divisor of two numbers
public class GCDCalculator {
public static void main(String[] args) {
int num1 = 24; // Change this to the first number
int num2 = 36; // Change this to the second number
while (num1 != num2) {
if (num1 > num2) {
num1 -= num2;
} else {
num2 -= num1;
}
}
System.out.println("GCD: " + num1);
}
}
//Determine whether a given year is a leap year.
import java.util.Scanner;
public class LeapYearChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a year: ");
int year = scanner.nextInt();
boolean isLeapYear = false;
if (year % 4 == 0) {
if (year % 100 != 0 || year % 400 == 0) {
isLeapYear = true;
}
}
System.out.println(year + " is a leap year? " + isLeapYear);
}
}
//Write a program to calculate the average of numbers in an array.
public class AverageCalculator {
public static void main(String[] args) {
int[] numbers = {5, 10, 15, 20, 25};
int sum = 0;
// Calculate the sum of numbers in the array.
for (int number : numbers) {
sum += number;
}
// Calculate the average.
double average = (double) sum / numbers.length;
System.out.println("Average: " + average);
}
}
//Calculate the result of raising a number to a given power.
public class PowerCalculator {
public static void main(String[] args) {
double base = 3.0; // Change this to the desired base
int exponent = 4; // Change this to the desired exponent
// Calculate the power using the Math.pow() method.
double result = Math.pow(base, exponent);
System.out.println(base + " raised to the power of " + exponent + " is: " + result);
}
}
//Determine whether a given string is a palindrome (reads the same forwards and backwards).
import java.util.Scanner;
public class PalindromeChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = scanner.nextLine();
// Remove spaces and convert to lowercase for a case-insensitive check.
input = input.replaceAll("\\s", "").toLowerCase();
boolean isPalindrome = true;
for (int i = 0; i < input.length() / 2; i++) {
if (input.charAt(i) != input.charAt(input.length() - 1 - i)) {
isPalindrome = false;
break;
}
}
System.out.println("Is it a palindrome? " + isPalindrome);
}
}
//Calculate the area of a circle given its radius
import java.util.Scanner;
public class CircleAreaCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the radius of the circle: ");
double radius = scanner.nextDouble();
// Calculate the area of the circle using the formula: A = π * r^2
double area = Math.PI * Math.pow(radius, 2);
System.out.println("The area of the circle is: " + area);
}
}
//Write a program to reverse a given string.
import java.util.Scanner;
public class StringReversal {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = scanner.nextLine();
String reversed = "";
// Reverse the string by iterating through it in reverse order.
for (int i = input.length() - 1; i >= 0; i--) {
reversed += input.charAt(i);
}
System.out.println("Reversed string: " + reversed);
}
}
//Generate the Fibonacci series up to a specified number of terms.
import java.util.Scanner;
public class FibonacciSeries {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of terms in the Fibonacci series: ");
int n = scanner.nextInt();
int firstTerm = 0, secondTerm = 1;
System.out.print("Fibonacci Series: " + firstTerm + ", " + secondTerm);
for (int i = 2; i < n; i++) {
int nextTerm = firstTerm + secondTerm;
System.out.print(", " + nextTerm);
firstTerm = secondTerm;
secondTerm = nextTerm;
}
}
}
//Determine whether a given number is prime or not.
public class PrimeChecker {
public static void main(String[] args) {
int number = 17; // Change this to the desired number
boolean isPrime = true;
// Check if the number is less than or equal to 1.
if (number <= 1) {
isPrime = false;
} else {
// Check for divisibility from 2 to the square root of the number.
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0) {
isPrime = false;
break;
}
}
}
// Print whether the number is prime or not.
System.out.println(number + " is prime? " + isPrime);
}
}
//Calculate the factorial of a given number.
public class FactorialCalculator {
public static void main(String[] args) {
int number = 5; // Change this to the desired number
long factorial = 1;
// Calculate the factorial using a loop.
for (int i = 1; i <= number; i++) {
factorial *= i;
}
// Print the factorial of the number.
System.out.println("Factorial of " + number + " is: " + factorial);
}
}
//Create a program to find the largest number in an array.
public class LargestNumber {
public static void main(String[] args) {
int[] numbers = {4, 2, 9, 7, 5, 12, 1};
// Assume the first number in the array is the largest.
int max = numbers[0];
// Iterate through the array to find the largest number.
for (int num : numbers) {
if (num > max) {
max = num;
}
}
// Print the largest number.
System.out.println("The largest number is: " + max);
}
}
//Write a program that takes two numbers as input and calculates their sum.
import java.util.Scanner;
public class SumCalculator {
public static void main(String[] args) {
// Create a Scanner object to read input from the user.
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
// Read the first number entered by the user.
double num1 = scanner.nextDouble();
System.out.print("Enter the second number: ");
// Read the second number entered by the user.
double num2 = scanner.nextDouble();
// Calculate the sum of the two numbers.
double sum = num1 + num2;
// Print the result.
System.out.println("The sum is: " + sum);
}
}
public class HelloWorld {
public static void main(String[] args) {
// The main method is the entry point of the program.
// It's where the program starts executing.
// System.out.println() is used to print to the console.
System.out.println("Hello, World!"); // This line prints "Hello, World!".
}
}
