0% found this document useful (0 votes)
5 views10 pages

10 Basic Java and C++ Programs

Uploaded by

sachikamore18
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views10 pages

10 Basic Java and C++ Programs

Uploaded by

sachikamore18
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

10 basic Java programs, each with a clear explanation and complete code

1) Print “Hello World”

public class HelloWorld {

public static void main(String[] args) {

[Link]("Hello World");

2) Add Two Numbers

public class AddNumbers {

public static void main(String[] args) {

int a = 10, b = 20;

int sum = a + b;

[Link]("Sum = " + sum);

3) Check Even or Odd

import [Link];

public class EvenOdd {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

int num = [Link]();

if(num % 2 == 0)

[Link]("Even");

else

[Link]("Odd");

}
}

4) Find Largest of Three Numbers

public class LargestOfThree {

public static void main(String[] args) {

int a = 10, b = 25, c = 18;

if(a >= b && a >= c)

[Link]("Largest: " + a);

else if(b >= a && b >= c)

[Link]("Largest: " + b);

else

[Link]("Largest: " + c);

5) Print Numbers 1 to 10 using Loop

public class PrintNumbers {

public static void main(String[] args) {

for(int i = 1; i <= 10; i++) {

[Link](i);

6) Factorial of a Number

import [Link];

public class Factorial {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);


int n = [Link]();

long fact = 1;

for(int i = 1; i <= n; i++) {

fact *= i;

[Link]("Factorial = " + fact);

7) Fibonacci Series

public class Fibonacci {

public static void main(String[] args) {

int a = 0, b = 1, c;

[Link](a + " " + b);

for(int i = 2; i < 10; i++) {

c = a + b;

[Link](" " + c);

a = b;

b = c;

8) Reverse a Number

public class ReverseNumber {

public static void main(String[] args) {

int num = 1234, rev = 0;


while(num != 0) {

rev = rev * 10 + num % 10;

num /= 10;

[Link]("Reversed Number: " + rev);

9) Check Prime Number

public class PrimeCheck {

public static void main(String[] args) {

int num = 29;

boolean isPrime = true;

for(int i = 2; i <= num/2; i++) {

if(num % i == 0) {

isPrime = false;

break;

if(isPrime)

[Link](num + " is Prime");

else

[Link](num + " is Not Prime");

10) Sum of Digits


public class SumOfDigits {

public static void main(String[] args) {

int num = 987, sum = 0;

while(num > 0) {

sum += num % 10;

num /= 10;

[Link]("Sum of digits: " + sum);

10 basic C++ programs, each with a clear purpose and simple code

1. Hello World Program

#include <iostream>

using namespace std;

int main() {

cout << "Hello, World!";

return 0;

2. Add Two Numbers

#include <iostream>

using namespace std;

int main() {
int a, b;

cin >> a >> b;

cout << "Sum = " << a + b;

return 0;

3. Check Even or Odd

#include <iostream>

using namespace std;

int main() {

int n;

cin >> n;

if (n % 2 == 0)

cout << "Even";

else

cout << "Odd";

return 0;

4. Find Largest of Two Numbers

#include <iostream>

using namespace std;

int main() {

int a, b;

cin >> a >> b;

if (a > b)
cout << "Largest = " << a;

else

cout << "Largest = " << b;

return 0;

5. Simple Calculator (Addition, Subtraction, Multiplication, Division)

#include <iostream>

using namespace std;

int main() {

char op;

float a, b;

cin >> op >> a >> b;

switch(op) {

case '+': cout << a + b; break;

case '-': cout << a - b; break;

case '*': cout << a * b; break;

case '/': cout << a / b; break;

default: cout << "Invalid operator";

return 0;

6. Print Numbers From 1 to n

#include <iostream>

using namespace std;


int main() {

int n;

cin >> n;

for (int i = 1; i <= n; i++)

cout << i << " ";

return 0;

7. Factorial of a Number

#include <iostream>

using namespace std;

int main() {

int n;

long long fact = 1;

cin >> n;

for (int i = 1; i <= n; i++)

fact *= i;

cout << "Factorial = " << fact;

return 0;

8. Check Positive, Negative, or Zero

#include <iostream>

using namespace std;


int main() {

int n;

cin >> n;

if (n > 0)

cout << "Positive";

else if (n < 0)

cout << "Negative";

else

cout << "Zero";

return 0;

9. Swap Two Numbers Using Temp Variable

#include <iostream>

using namespace std;

int main() {

int a, b, temp;

cin >> a >> b;

temp = a;

a = b;

b = temp;

cout << "After swap: " << a << " " << b;

return 0;

10. Print Fibonacci Series (n terms)


#include <iostream>

using namespace std;

int main() {

int n, a = 0, b = 1, next;

cin >> n;

for (int i = 1; i <= n; i++) {

cout << a << " ";

next = a + b;

a = b;

b = next;

return 0;

You might also like