0% found this document useful (0 votes)
13 views35 pages

Codes

The document contains a series of Java and C# programs that demonstrate various programming concepts such as character classification, number properties, mathematical calculations, and algorithms for common tasks. Each section includes code snippets for functionalities like checking if a character is a vowel or consonant, calculating the area of a circle, finding LCM and GCD, and determining if a number is prime or a palindrome. The document serves as a programming reference for basic to intermediate level coding tasks.

Uploaded by

chandru m
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)
13 views35 pages

Codes

The document contains a series of Java and C# programs that demonstrate various programming concepts such as character classification, number properties, mathematical calculations, and algorithms for common tasks. Each section includes code snippets for functionalities like checking if a character is a vowel or consonant, calculating the area of a circle, finding LCM and GCD, and determining if a number is prime or a palindrome. The document serves as a programming reference for basic to intermediate level coding tasks.

Uploaded by

chandru m
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/ 35

1.

A character is a vowel or consonant


2. A character is an alphabet or not
3. Ascii values of a character
4. Uppercase, Lowercase or special character
5. A number is positive or negative
6. A number is even or odd
7. Swap two numbers without third variable
8. Area of a circle
9. LCM of two numbers
10. GCD of two numbers
11. Greatest of two numbers
12. Greatest of three numbers
13. Number of digits in an integer
14. Sum of digits of a number
15. Sum of N natural numbers
16. Sum of numbers in a given range
17. Factorial of a number
18. Fibonacci series up to n
19. Leap year or not
20. Prime number or not
21. Palindrome or not
22. Armstrong number or not
23. Strong number or not
24. Perfect number or not
25. Friendly pair or not (amicable or not)
26. Automorphic number or not
27. Harshad number or not
28. Abundant number or not
29. Power of a number
30. Factors of a number
31. Add two fractions
32. Reverse a given number
1

// java program to check whether input

// character is a vowel or consonant

import java.io.*;

public class geek {

// Function to find whether an input

// character is vowel or not

static void Vowel_Or_Consonant(char y)

if (y == 'a' || y == 'e' || y == 'i' || y == 'o'

|| y == 'u')

System.out.println("It is a Vowel.");

else

System.out.println("It is a Consonant.");

// The Driver code

static public void main(String[] args)

Vowel_Or_Consonant('b');

Vowel_Or_Consonant('u');

}
2

import java.util.*;

public class Main{

public static void main(String[] args){

// Initializing a character

char Prep = 'A';

// if-else to check whether a character is alphabet or not

if( (Prep >= 'a' && Prep <= 'z') || (Prep >= 'A' && Prep <= 'Z'))

// If prep is an alphabet

System.out.println(Prep + " is an alphabet.");

else

// if prep is not an alphabet

System.out.println(Prep + " is not an alphabet.");

}
3

// Java program to print ASCII Value of Character

// by assigning variable to integer

public class GFG {

// Main driver method

public static void main(String[] args)

// Character whose ASCII is to be computed

char ch = '}';

// Creating a new variable of type int

// and assigning the character value.

int ascii = ch;

/* Java stores the ascii value there itself*/

// Printing the ASCII value of above character

System.out.println("The ASCII value of " + ch

+ " is: " + ascii);

}
4

// C# program to count the uppercase,

// lowercase, special characters

// and numeric values

using System;

class Count {

public static void Main()

String str = "#GeeKs01fOr@gEEks07";

int upper = 0, lower = 0;

int number = 0, special = 0;

for(int i = 0; i < str.Length; i++)

char ch = str[i];

if (ch >= 'A' && ch <= 'Z')

upper++;

else if (ch >= 'a' && ch <= 'z')

lower++;

else if (ch >= '0' && ch <= '9')

number++;

else

special++;

Console.WriteLine("Upper case letters : " + upper);

Console.WriteLine("Lower case letters : " + lower);

Console.WriteLine("Number : " + number);

Console.Write("Special characters : " + special);

}
5

/*package whatever //do not write package name here */

import java.io.*;

import java.util.Scanner;

import java.util.*;

public class Main {

public static void main(String[] args) {

int number =45;

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.");

}
6

import java.io.*;

import java.util.Scanner;

// Main class

class GFG {

// Main Driver Method

public static void main(String[] args)

// Declaring and initializing integer variable

int num = 10;

// Checking if number is even or odd number

// via remainder

if (num % 2 == 0) {

// If remainder is zero then this number is even

System.out.println("Entered Number is Even");

else {

// If remainder is not zero then this number is

// odd

System.out.println("Entered Number is Odd");

}
7
// Java Program to Swap Two values using third variable
// using temp variable

// Importing generic libraries


import java.util.*;

class GFG {

// Function to swap two numbers


// Using temporary variable
static void swapValuesUsingThirdVariable(int m, int n)
{
// Swapping the values
int temp = m;
m = n;
n = temp;
System.out.println("Value of m is " + m
+ " and Value of n is " + n);
}

// Main driver code


public static void main(String[] args)
{
// Random integer values
int m = 9, n = 5;

// Calling above function to


// reverse the numbers
swapValuesUsingThirdVariable(m, n);
}
}
8

// Java program to calculate the area of the

public class GFG {

public static void main(String[] args)

int radius;

double pi = 3.142, area;

radius = 5;

// calculating the area of the circle

area = pi * radius * radius;

// printing the area of the circle

System.out.println("Area of circle is :" + area);

}
9
// Java Program to find
// the LCM of two numbers
import java.io.*;

// Driver Class
class GFG {
// main function
public static void main(String[] args)
{
// Numbers
int a = 15, b = 25;

// Checking for the largest


// Number between them
int ans = (a > b) ? a : b;

// Checking for a smallest number that


// can de divided by both numbers
while (true) {
if (ans % a == 0 && ans % b == 0)
break;
ans++;
}

// Printing the Result


System.out.println("LCM of " + a + " and " + b
+ " : " + ans);
}
}
10
class Main {
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);


}
}
11
// Write a program to find the largest of two numbers in java
public class Main
{
public static void main (String[]args)
{

int num1 = 50, num2 = 20;


if (num1 == num2)
System.out.println ("both are equal");
else if (num1 > num2)
System.out.println (num1 + " is greater");

else
System.out.println (num2 + " is greater");

}
}
12
// Java Program to Find the Biggest of 3 Numbers
import java.io.*;

class GFG {

// Function to find the biggest of three numbers


static int biggestOfThree(int x, int y, int z)
{

return z > (x > y ? x : y) ? z : ((x > y) ? x : y);


}

// Main driver function


public static void main(String[] args)
{
// Declaring variables for 3 numbers
int a, b, c;

// Variable holding the largest number


int largest;
a = 5;
b = 10;
c = 3;
// Calling the above function in main
largest = biggestOfThree(a, b, c);

// Printing the largest number


System.out.println(largest
+ " is the largest number.");
}
}
13

// JAVA Code to count number of

// digits in an integer

import java.util.*;

class GfG {

static int countDigit(int n){

if (n / 10 == 0)

return 1;

return 1 + countDigit(n / 10);

public static void main(String[] args) {

int n = 58964;

System.out.print(countDigit(n));

}
14
// Java program to compute
// sum of digits in number.
import java.io.*;

class GFG {

/* Function to get sum of digits */


static int getSum(int n)
{
int sum = 0;

while (n != 0)
{
sum = sum + n % 10;
n = n/10;
}

return sum;
}

// Driver program
public static void main(String[] args)
{
int n = 687;

System.out.println(getSum(n));
}
}

// This code is contributed by Gitanjali


15
// Java Program to Display Numbers
// from 1 to N Using For Loop and
// sum of First N Natural Number
import java.io.*;

// Driver Class
class GFG {
// main function
public static void main(String[] args)
{
int N = 10;
int sum = 0;
System.out.println("Finding Sum of " + N + " numbers using for loop");

// we initialize the value of the variable i


// with 1 and increment each time with 1
for (int i = 1; i <= N; i++) {
sum += i;
}

System.out.println("Sum of first " + N


+ " Natural Number = " + sum);
}
}
16
public class Main
{
public static void main (String[]args)
{
int a = 5;
int b = 10;

int sum = 0;

for (int i = a; i <= b; i++)


sum = sum + i;
System.out.println ("The sum is " + sum);
}
}
17
// Java program to find factorial
// of given number

// Driver Class
class Test {
// method to find factorial
// of given number
static int factorial(int n)
{
if (n == 0)
return 1;

return n * factorial(n - 1);


}

// main method
public static void main(String[] args)
{
int num = 5;
System.out.println("Factorial of " + num + " is "
+ factorial(5));
}
}
18
// Fibonacci series program in java

import java.io.*;

class GFG {
// Function to print N Fibonacci Number
static void Fibonacci(int N)
{
int num1 = 0, num2 = 1;

for (int i = 0; i < N; i++) {


// Print the number
System.out.print(num1 + " ");

// Swap
int num3 = num2 + num1;
num1 = num2;
num2 = num3;
}
}

// Driver Code
public static void main(String args[])
{
// Given Number N
int N = 10;

// Function Call
Fibonacci(N);
}
}
19

// Java program to find a leap year

import java.io.*;

public class GeeksforGeeks {

public static void isLeapYear(int year)

// flag to take a non-leap year by default

boolean is_leap_year = false;

is_leap_year = (year % 4 == 0 && year % 100 != 0

|| year % 400 == 0)

? true

: false;

if (!is_leap_year)

System.out.println(year + " : Non Leap-year");

else

System.out.println(year + " : Leap-year");

public static void main(String[] args)

// Calling our function by

// passing century year not divisible by 400

isLeapYear(2000);

// Calling our function by

// passing Non-century year

isLeapYear(2002);

}
20
// Java Program to demonstrate Brute Force Method to check if a number is prime
class GFG {
static boolean isPrime(int n)
{
// Corner case
if (n <= 1)
return false;

// Check from 2 to n-1


for (int i = 2; i < n; i++)
if (n % i == 0)
return false;

return true;
}

// Driver Program
public static void main(String args[])
{
if (isPrime(11))
System.out.println(" true");
else
System.out.println(" false");
if (isPrime(15))
System.out.println(" true");
else
System.out.println(" false");
}
}
21
// Java Program to Check if a
// String is a Palindrome
import java.io.*;

public class Palindrome {

public static boolean isPalindrome(String s) {


s = s.toLowerCase();
// Reverse the string
String rev = "";
for (int i = s.length() - 1; i >= 0; i--) {
rev = rev + s.charAt(i);
}
return s.equals(rev);
}

public static void main(String[] args) {

// Input string
String s = "level";
// Check if the string is a palindrome
boolean res = isPalindrome(s);

// Print the result


if (res) {
System.out.println("\"" + s + "\" is a palindrome.");
} else {
System.out.println("\"" + s + "\" is not a palindrome.");
}
}
}
22

public class Armstrong {

public static void main(String[] args) {

int number = 371, originalNumber, remainder, result = 0;

originalNumber = number;

while (originalNumber != 0)

remainder = originalNumber % 10;

result += Math.pow(remainder, 3);

originalNumber /= 10;

if(result == number)

System.out.println(number + " is an Armstrong number.");

else

System.out.println(number + " is not an Armstrong number.");

}
23
// Java program to check if
// a number is Strong or not

class CheckStrong
{
static int f[] = new int[10];

// Fills factorials of digits from 0 to 9.


static void preCompute()
{
f[0] = f[1] = 1;
for (int i = 2; i<10; ++i)
f[i] = f[i-1] * i;
}

// Returns true if x is Strong


static boolean isStrong(int x)
{
int factSum = 0;

// Traverse through all digits of x.


int temp = x;
while (temp>0)
{
factSum += f[temp%10];
temp /= 10;
}

return (factSum == x);


}
// main function
public static void main (String[] args)
{
// calling preCompute
preCompute();

// first pass
int x = 145;
if(isStrong(x))
{
System.out.println("Yes");
}
else
System.out.println("No");

// second pass
x = 534;
if(isStrong(x))
{
System.out.println("Yes");
}
else
System.out.println("No");
}
}
24
// Java program to check if a given
// number is perfect or not

class GFG {

// Returns true if n is perfect


static boolean isPerfect(int n)
{
// 1 is not a perfect number
if (n == 1)
return false;

// sum will store the sum of proper divisors


// As 1 is a proper divisor for all numbers
// initialised sum with 1
int sum = 1;

// Looping through the numbers to check if they are


// divisors or not
for (int i = 2; i * i <= n; i++) {

if (n % i == 0) {

// n is a perfect square
// let's take 25
// we need to add 5 only once
// sum += i + n / i will add it twice

if (i * i == n)
sum += i;
else
sum += i + (n / i);
}
}

// If sum of divisors is equal to


// n, then n is a perfect number

if (sum == n)

return true;

return false;
}

// Driver program
public static void main(String[] args)
{
int n = 6;

// Call isPerfect function to


// check if the number is perfect or not.
if (isPerfect(n))

System.out.println(n + " is a perfect number");

else
System.out.println(
n + " is not a perfect number");
}
}
26
// Java program to check if a number is Automorphic
import java.io.*;
class Test {
// Function to check Automorphic number
static boolean isAutomorphic(int N)
{
// Store the square
if(N < 0) N = -N;
int sq = N * N;

// Start Comparing digits


while (N > 0) {
// Return false, if any digit of N doesn't
// match with its square's digits from last
if (N % 10 != sq % 10)
return false;

// Reduce N and square


N /= 10;
sq /= 10;
}
return true;
}
// Driver method
public static void main(String[] args)
{
int N = 5;

System.out.println(isAutomorphic(N) ? "Automorphic" : "Not Automorphic");


}
}
27

// Java program to check if a number is Harshad

// Number or not

public class GFG {

// method to check Harshad Number

static boolean checkHarshad(int n)

// calculate sum of digits

int sum = 0;

for (int temp = n; temp > 0; temp /= 10)

sum += temp % 10;

// Return true if sum of digits is multiple

// of n

return (n % sum == 0);

// Driver program to test above functions

public static void main(String[] args)

System.out.println(checkHarshad(12) ? "Yes" : "No");

System.out.println(checkHarshad(15) ? "Yes" : "No");

}
28
#include <bits/stdc++.h>
using namespace std;
bool isAbundantNumber(int n)
{
// To store the sum of divisors
int sum = 0;

// Loop through the numbers [1,n-1]


for (int i = 1; i < n; i++) {
if (n % i == 0) {
sum += i;
}
}
if (sum > n) {
return true;
}
else {
return false;
}
}
int main()
{
// Function call
if (isAbundantNumber(12)) {
cout << "YES" << endl;
}
else {
cout << "NO" << endl;
}
}
29
// Java program to find the power of a number
// using Recursion

class GFG {

// Function to calculate N raised to the power P


static int power(int N, int P)
{
if (P == 0)
return 1;
else
return N * power(N, P - 1);
}

// Driver code
public static void main(String[] args)
{
int N = 2;
int P = 3;

System.out.println(power(N, P));
}
}
30
// Java implementation of Naive method to print all
// divisors

class Test {
// method to print the divisors
static void printDivisors(int n)
{
for (int i = 1; i <= n; i++)
if (n % i == 0)
System.out.print(i + " ");
}

// Driver method
public static void main(String args[])
{
System.out.println("The divisors of 100 are: ");
printDivisors(100);
;
}
}
31
//Java program to add two fractions
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
//scanner class declaration
Scanner sc = new Scanner(System.in);
//input from the user
System.out.print("Enter numerator for first fraction : ");
int num1 = sc.nextInt();
System.out.print("Enter denominator for first fraction : ");
int den1 = sc.nextInt();
System.out.print("Enter numerator for second fraction : ");
int num2 = sc.nextInt();
System.out.print("Enter denominator for second fraction : ");
int den2 = sc.nextInt();
int num, den, x;
System.out.print("("+num1+" / "+den1+") + ("+num2+" / "+den2+") = ");
//logic for calculating sum of two fractions
if(den1 == den2)
{
num = num1 + num2 ;
den = den1 ;
}
else{
num = (num1*den2) + (num2*den1);
den = den1 * den2;
}
if(num > den)
x = num;
else
x = den;
for(int i = 1 ; i <= x ; i++)
{
if(num%i == 0 && den%i == 0)
{
num = num/i;
den = den/i;
}
}
//logic for getting simplified fraction
int n = 1;
int p = num;
int q = den;
if( num != den)
{
while(n != 0)
{
//storing remainder
n = num % den;
if(n != 0)
{
num = den;
den = n;
}
}
}
System.out.println("("+p/den+" / "+q/den+")");
//closing scanner class(not compulsory, but good practice)
sc.close();
}
}
32
// Java program to reverse a number
import java.io.*;

// Driver Class
class GFG {
// Function to reverse the number
static int reverse(int n)
{
// reversed number
int rev = 0;
// remainder
int rem;

while (n > 0) {
rem = n % 10;
rev = (rev * 10) + rem;
n = n / 10;
}

return rev;
}

// Driver Function
public static void main(String[] args)
{
int n = 4526;

System.out.print("Reversed Number is "


+ reverse(n));
}
}

You might also like