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

Small Programs List

The document provides a collection of small programs in Java and C that demonstrate various algorithms and functionalities, including calculating factorials, generating Fibonacci series, finding minimum and maximum values in an array, string comparison, checking for prime numbers, reversing numbers, determining leap years, identifying vowels and consonants, calculating the greatest common divisor (GCD), and finding the least common multiple (LCM) using GCD. Each program is presented with code snippets and explanations of their logic. The document serves as a reference for basic programming concepts and implementations.

Uploaded by

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

Small Programs List

The document provides a collection of small programs in Java and C that demonstrate various algorithms and functionalities, including calculating factorials, generating Fibonacci series, finding minimum and maximum values in an array, string comparison, checking for prime numbers, reversing numbers, determining leap years, identifying vowels and consonants, calculating the greatest common divisor (GCD), and finding the least common multiple (LCM) using GCD. Each program is presented with code snippets and explanations of their logic. The document serves as a reference for basic programming concepts and implementations.

Uploaded by

ranjan patali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Small programs list

Factorial of a number

Using recurseion

static int factorial(int n)

if (n == 0)

return 1;

return n * factorial(n - 1);

Using non recursion

int factorial(int n)

int res = 1, i;

for (i = 2; i <= n; i++)

res *= i;

return res;

int factorial(int n)

// single line to find factorial

return (n == 1 || n == 0) ? 1
: n * factorial(n - 1);

Fibonacci series

using iteration
public static void main(String[] args) {

int n = 10, firstTerm = 0, secondTerm = 1;

System.out.println("Fibonacci Series till " + n + " terms:");

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

System.out.print(firstTerm + ", ");

// compute the next term

int nextTerm = firstTerm + secondTerm;

firstTerm = secondTerm;

secondTerm = nextTerm;

}
using recursion
static int fib(int n)

// Base Case

if (n <= 1)

return n;

// Recursive call

return fib(n - 1) + fib(n - 2);

Program to find min and max in an Array in java

public class MinMaxArray {

public static void main(String[] args) {

int[] numbers = {5, 2, 9, 1, 5, 6};

int min = numbers[0];

int max = numbers[0];

for (int i = 1; i < numbers.length; i++) {

if (numbers[i] < min) {

min = numbers[i];

if (numbers[i] > max) {

max = numbers[i];

}
}

System.out.println("Smallest element: " + min);

System.out.println("Largest element: " + max);

String comparison without using equals method in java

public class StringComparison {

public static boolean compareStrings(String str1, String str2) {


if (str1.length() != str2.length()) {
return false;
}

for (int i = 0; i < str1.length(); i++) {


if (str1.charAt(i) != str2.charAt(i)) {
return false;
}
}

return true;
}

to find length of array in c

int main() {
int numbers[] = {10, 20, 30, 40, 50};
int length = sizeof(numbers) / sizeof(numbers[0]);
printf("The length of the array is: %d\n", length);
return 0;
}
check prime number or not

public class Main {

public static void main(String[] args) {

int num = 29;

boolean flag = false;

// 0 and 1 are not prime numbers

if (num == 0 || num == 1) {

flag = true;

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

// condition for nonprime number

if (num % i == 0) {

flag = true;

break;

if (!flag)

System.out.println(num + " is a prime number.");

else

System.out.println(num + " is not a prime number.");


}

Reverse a number

Reverse a Number using a while loop in Java

class Main {

public static void main(String[] args) {

int num = 1234, reversed = 0;

System.out.println("Original Number: " + num);

// run loop until num becomes 0

while(num != 0) {

// get last digit from num

int digit = num % 10;

reversed = reversed * 10 + digit;

// remove the last digit from num

num /= 10;

System.out.println("Reversed Number: " + reversed);

}
}

Leap year or not

ava Program to Check a Leap Year

public class Main {

public static void main(String[] args) {

// year to be checked

int year = 1900;

boolean leap = false;

// if the year is divided by 4

if (year % 4 == 0) {

// if the year is century

if (year % 100 == 0) {

// if year is divided by 400

// then it is a leap year

if (year % 400 == 0)

leap = true;

else

leap = false;

}
ublic class VowelConsonant {

public static void main(String[] args) {

char ch = 'i';

if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' )

System.out.println(ch + " is vowel");

else

System.out.println(ch + " is consonant");

public class VowelConsonant {

public static void main(String[] args) {

char ch = 'z';

switch (ch) {

case 'a':

case 'e':

case 'i':

case 'o':
case 'u':

System.out.println(ch + " is vowel");

break;

default:

System.out.println(ch + " is consonant");

Greatest common divisor

public static void main(String[] args) {

// find GCD between n1 and n2

int n1 = 81, n2 = 153;

// initially set to gcd

int gcd = 1;

for (int i = 1; i <= n1 && i <= n2; ++i) {

// check if i perfectly divides both n1 and n2

if (n1 % i == 0 && n2 % i == 0)

gcd = i;

System.out.println("GCD of " + n1 +" and " + n2 + " is " + gcd);

}
}

Calculate LCM using GCD

public class Main {

public static void main(String[] args) {

int n1 = 72, n2 = 120;

int gcd = findGCD(n1, n2);

int lcm = (n1 * n2) / gcd;

System.out.printf("The LCM of %d and %d is %d.", n1, n2, lcm);

public static int findGCD(int a, int b) {

if (b == 0)

return a;

return findGCD(b, a % b);

You might also like