0% found this document useful (0 votes)
36 views6 pages

CPP Programs Unique Part1 5

The document presents five Java programs, each with its output. The programs include calculating the sum of digits, converting decimal to binary, finding the largest element in an array, implementing a simple calculator, and checking for a leap year. Each program is accompanied by example input and output to demonstrate functionality.

Uploaded by

suryanshkumar85
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)
36 views6 pages

CPP Programs Unique Part1 5

The document presents five Java programs, each with its output. The programs include calculating the sum of digits, converting decimal to binary, finding the largest element in an array, implementing a simple calculator, and checking for a leap year. Each program is accompanied by example input and output to demonstrate functionality.

Uploaded by

suryanshkumar85
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
You are on page 1/ 6

Java Programs with Output

This document contains 5 Java programs along with their outputs. Each
program is displayed on a separate page.
1. Calculate the Sum of Digits

#include <iostream>
using namespace std;

int sumOfDigits(int num) {


int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
return sum;
}

int main() {
int num;
cout << "Enter a number: ";
cin >> num;
cout << "Sum of digits: " << sumOfDigits(num) << endl;
return 0;
}

Output:
Enter a number: 1234
Sum of digits: 10
2. Convert Decimal to Binary

#include <iostream>
using namespace std;

void decimalToBinary(int num) {


int binary[32], i = 0;
while (num > 0) {
binary[i++] = num % 2;
num /= 2;
}
for (int j = i - 1; j >= 0; j--)
cout << binary[j];
}

int main() {
int num;
cout << "Enter a decimal number: ";
cin >> num;
cout << "Binary equivalent: ";
decimalToBinary(num);
cout << endl;
return 0;
}

Output:
Enter a decimal number: 10
Binary equivalent: 1010
3. Find the Largest Element in an Array

#include <iostream>
using namespace std;

int findLargest(int arr[], int size) {


int largest = arr[0];
for (int i = 1; i < size; i++)
if (arr[i] > largest)
largest = arr[i];
return largest;
}

int main() {
int arr[] = {23, 45, 12, 67, 89, 34};
cout << "Largest element: " << findLargest(arr, 6) << endl;
return 0;
}

Output:
Largest element: 89
4. Implement a Simple Calculator

#include <iostream>
using namespace std;

int main() {
double num1, num2;
char op;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "Enter operator (+, -, *, /): ";
cin >> op;

switch(op) {
case '+': cout << "Result: " << num1 + num2; break;
case '-': cout << "Result: " << num1 - num2; break;
case '*': cout << "Result: " << num1 * num2; break;
case '/': cout << (num2 != 0 ? to_string(num1 / num2) : "Error!
Division by zero"); break;
default: cout << "Invalid operator!";
}
cout << endl;
return 0;
}

Output:
Enter two numbers: 8 4
Enter operator (+, -, *, /): /
Result: 2
5. Check for Leap Year

#include <iostream>
using namespace std;

bool isLeapYear(int year) {


return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}

int main() {
int year;
cout << "Enter a year: ";
cin >> year;
cout << year << (isLeapYear(year) ? " is a leap year" : " is not a
leap year") << endl;
return 0;
}

Output:
Enter a year: 2024
2024 is a leap year

You might also like