1. Hello World:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Output:
Hello, World!
2. Variables and Data Types:
public class VariablesExample {
public static void main(String[] args) {
// Integer variable
int age = 25;
// Floating-point variable
double height = 5.8;
// Character variable
char gender = 'M';
// Boolean variable
boolean isStudent = true;
System.out.println("Age: " + age);
System.out.println("Height: " + height);
System.out.println("Gender: " + gender);
System.out.println("Is a student? " + isStudent);
}
}
3. Conditional Statements:
public class ConditionalExample {
public static void main(String[] args) {
int number = 10;
if (number > 0) {
System.out.println("Number is positive");
} else if (number < 0) {
System.out.println("Number is negative");
} else {
System.out.println("Number is zero");
}
}
}
4. Loops:
public class LoopExample {
public static void main(String[] args) {
// For loop
for (int i = 1; i <= 5; i++) {
System.out.println("Count: " + i);
}
// While loop
int j = 1;
while (j <= 5) {
System.out.println("Count: " + j);
j++;
}
// Do-while loop
int k = 1;
do {
System.out.println("Count: " + k);
k++;
} while (k <= 5);
}
}
5. ARRAY:
public class ArrayExample {
public static void main(String[] args) {
// Array declaration and initialization
int[] numbers = {1, 2, 3, 4, 5};
// Accessing elements of an array
System.out.println("First element: " + numbers[0]);
System.out.println("Second element: " + numbers[1]);
// Modifying elements of an array
numbers[2] = 10;
// Iterating over an array
for (int i = 0; i < numbers.length; i++) {
System.out.println("Element at index " + i + ": " + numbers[i]);
}
}
}
C++
1. Hello World
#include
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
2. Variables and Data Types:
#include
int main() {
// Integer variable
int age = 25;
// Floating-point variable
double height = 5.8;
// Character variable
char gender = 'M';
// Boolean variable
bool isStudent = true;
std::cout << "Age: " << age << std::endl;
std::cout << "Height: " << height << std::endl;
std::cout << "Gender: " << gender << std::endl;
std::cout << "Is a student? " << isStudent << std::endl;
return 0;
}
3. Conditional Statements:
#include
int main() {
int number = 10;
if (number > 0) {
std::cout << "Number is positive" << std::endl;
} else if (number < 0) {
std::cout << "Number is negative" << std::endl;
} else {
std::cout << "Number is zero" << std::endl;
}
return 0;
}
4. Loops:
#include
int main() {
// For loop
for (int i = 1; i <= 5; i++) {
std::cout << "Count: " << i << std::endl;
}
// While loop
int j = 1;
while (j <= 5) {
std::cout << "Count: " << j << std::endl;
j++;
}
// Do-while loop
int k = 1;
do {
std::cout << "Count: " << k << std::endl;
k++;
} while (k <= 5);
return 0;
}
5. ARRAY:
#include
int main() {
// Array declaration and initialization
int numbers[] = {1, 2, 3, 4, 5};
// Accessing elements of an array
std::cout << "First element: " << numbers[0] << std::endl;
std::cout << "Second element: " << numbers[1] << std::endl;
// Modifying elements of an array
numbers[2] = 10;
// Iterating over an array
for (int i = 0; i < sizeof(numbers) / sizeof(numbers[0]); i++) {
std::cout << "Element at index " << i << ": " << numbers[i] << std::endl;
}
return 0;
}
In this 20th century we really need more on codes like this and this is massive let's do more on being creative and create technic codes
Thank you so much @Audrey. Definitely will post more codes..
perfect
You must be logged in to post a comment.