0% found this document useful (0 votes)
32 views

Computer

Uploaded by

aayushrawat2006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Computer

Uploaded by

aayushrawat2006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Programs

Questions
1.Write a program to print the following pattern.
***** *****
**** ****
*** ***
** **
Solution
import java.util.*;
class patterndi
{
static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of rows for the diamond pattern:
");
int rows = scanner.nextInt()
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= rows - i; j++)
{
System.out.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++)
{
System.out.print("*");
}
System.out.println();
}

for (int i = rows - 1; i >= 1; i--)


{
for (int j = 1; j <= rows - i; j++)
{
System.out.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++)
{
System.out.print("*");
}
System.out.println();
}

scanner.close();
}
}
2. Write a program to print the following pattern.
*
***
******
Solution
import java.util.*;
class pattern1
{
void main(String[] args)
{
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of rows for the


diamond pattern: ");
int numRows = scanner.nextInt();
for (int i = 1; i <= numRows; i++)
{
for (int j = 1; j <= numRows - i; j++)
{
System.out.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++)
{
System.out.print(" *");
}
System.out.println();
}
for (int i = numRows - 1; i >= 1; i--)
{
for (int j = 1; j <= numRows - i; j++)
{
System.out.print(" ");
{
for (int k = 1; k <= 2 * i - 1; k++)
{
System.out.print(" *");
{
System.out.println();
}
scanner.close();
}
}
3.Create a function to find H.C.F.
Solution
class recurhcf
{
int hcf (int a ,int b)
{
if(b%a==a)
{
return a;
}
else
{
int x= hcf(b%a,a);
return x ;
}
}
}
4. Create a function to check prime number.
Solution
class recprime
{
boolean isPrime(int n,int i)
{
if(n==i)
{
return true;
}
else if(n%i==0)
{
return false;
}
else
{
return isPrime (n,i+1);
}
}
}
5. Create a function to find sum of factors.
Solution
class recfacsum
{
int factorsum(int n,int i)

{
if(i==n)
return 0;
else
{
int x= factorsum(n,i+1);
if(n%i==0)
return x+1;
else
return x;
}
}
}
6. Write a program to input an array and sort it
using bubble sort.
Solution
class bubsort
{
void main (int a[]) {
int size=a.length;
for(int i=0;i<size-1;i++)
{
for(int j=0;j<size-1-i;j++)
{
if(a[j]>a[j+1])
{
int t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
for(int k=0;k<size;k++)
System.out.print(a[i]);
}
}
}
}
7. Write a program input an array and sort it using
selection sort.
Solution
class selecsort
{
void main( int a[] )
{
int n= a.length;
for(int i= 0;i<n-1;i++){
int m= a[i],p=i;
for(int j=i+1;j<n;j++){
if(a[j]<m){
m=a[j];
p=j;
int t=a[p];
a[p]=a[i];
a[i]=t;}
for(int k=0;k<n;k++)
System.out.print(a[k]); }
}
}
}
8. Write a program to find a number in an array.
Solution

class binsearch
{
void main(int a[],int x)
{
int size=9;int start=0,end=size-1,f=0,mid=0;
while(start<=end)
{
mid=(start/end)/2;
if(a[mid]==x)
{
f=1;
}
break;
} {
if(x>a[mid])
start=mid+1;
else
end= mid-1;
}
if(f==1)
System.out.print("found");
else
System.out.print("not found");
}
}
9.Write a program to print a magic square.
Solution
import java.util.*;
class d2MagSq
{
void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the order of the magic square (odd
number): ");
int order = scanner.nextInt();
if (order % 2 == 0)
{
System.out.println("Please enter an odd number for the
order of the magic square.");
return;
}
int[][] magicSquare = generateMagicSquare(order);
System.out.println("Magic Square of order " + order +
":");
printMagicSquare(magicSquare);
}
int[][] generateMagicSquare(int order)
{
int[][] magicSquare = new int[order][order];
int num = 1;
int i = 0, j = order / 2;

while (num <= order * order)


{
magicSquare[i][j] = num++;
i--;
j++;

if (i < 0) {
i = order - 1;
}
if (j == order)
{
j = 0;
}
if (magicSquare[i][j] != 0)
{
i += 2;
j--;
}
}

return magicSquare;
}

void printMagicSquare(int[][] magicSquare)


{
for (int i = 0; i < magicSquare.length; i++) {
for (int j = 0; j < magicSquare[i].length; j++)
{
System.out.print(magicSquare[i][j] + "\t");
}
System.out.println();
}
}
}
10. Write a program to print a spiral matrix.
Solution
class spiral
{
void main(int n)
{
int a[][]=new int[n][n];
int c=1;
for(int q=0;q<=n*n;q++)
{
for(int i=q;i<n-q;i++)
{
a[q][i]=c;
c++;

}
for(int i=q+1;i<n-q;i++)
{
a[i][n-(q+1)]=c;
c++;
}
for(int i=n-q+2;i>=q;i--)
{
a[n-(q+1)][i]=c;
c++;
}
for(int i=n-q+2;i>=q+1;i--)
{
a[i][q]=c;
c++;
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(a[i][j]);
System.out.println();
}

}
}
}
11. Write a program to input an array and find
average.
Solution
class d2avg
{
void main(int a[][])
{
for(int i=0;i<4;i++)
{
double s=0;
for(int j=0;j<4;j++)
s=s+a[j][i];
s=s/4;
System.out.println(s);
}
}
}
12.Write a program to input a no. and check
whether it is a lucky no. or not.
Solution
import java.util.Scanner;
class Lucky
{
void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number to check if it's a lucky
number: ");
int number = scanner.nextInt();
if (isLuckyNumber(number))
{
System.out.println(number + " is a lucky number!");
}
else
{
System.out.println(number + " is not a lucky
number.");
}
scanner.close();
}
boolean isLuckyNumber(int num) {
int counter = 2;
while (counter <= num) {
if (num % counter == 0)
{
return false;
}
num -= num / counter;
counter++;
}
return true;
}
}
13. Write a program to input a no. and check
whether it is a vampire no. or not.
Solution
import java.util.*;
class Vampire
{
void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number to check if it's a vampire
number: ");
int number = scanner.nextInt();
if (isVampireNumber(number)) {
System.out.println(number + " is a vampire number!");
} else {
System.out.println(number + " is not a vampire
number.");
}
scanner.close();
}
boolean isVampireNumber(int num)
{
String numStr = String.valueOf(num);
int numLength = numStr.length();
if (numLength % 2 != 0) {
return false;
}
for (int i = 10; i <= Math.sqrt(num); i++)
{
if (num % i == 0) {
int factor1 = i;
int factor2 = num / i;
if ((String.valueOf(factor1) +
String.valueOf(factor2)).length() == numLength)
{
if (isSameDigits(numStr, String.valueOf(factor1)
+ String.valueOf(factor2)))
{
return true;
}
}
}
}
return false;
}
boolean isSameDigits(String str1, String str2) {
char[] charArray1 = str1.toCharArray();
char[] charArray2 = str2.toCharArray();
java.util.Arrays.sort(charArray1);
java.util.Arrays.sort(charArray2);
return java.util.Arrays.equals(charArray1, charArray2);
}}
14. Write a program to input a no. and check
whether it is a bouncy no. or not.
Solution
import java.util.*;

class Bouncy
{
boolean isBouncy(int num)
{
String strNum = Integer.toString(num);
boolean increasing = false;
boolean decreasing = false;
for (int i = 1; i < strNum.length(); i++) {
int currentDigit =
Character.getNumericValue(strNum.charAt(i));
int previousDigit=
Character.getNumericValue(strNum.charAt(i - 1));
if (currentDigit > previousDigit) {
increasing = true;
}
else if (currentDigit < previousDigit) {
decreasing = true;
}
if (increasing && decreasing) {
return true;
}
}
return false;
}
void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number to check if it's bouncy: ");
int num = scanner.nextInt();
if (isBouncy(num)) {
System.out.println(num + " is a bouncy number.");
}
else {
System.out.println(num + " is not a bouncy number.");
}
scanner.close();
}
}
15. Write a function to solve Tower Of Hanoi.
Solution
class hanoi
{
void move(int n,char source, char temp,char dest)
{
if (n>=1)
{
move(n-1,source,dest,temp);
System.out.println("move
disk"+n+"from"+source+"to"+dest);
move(n-1,temp,dest,source);
}
}
}
16. Write a program to input a string and print
palindrome words.
Solution
import java.util.*;
class strpallind
{
boolean isPalindrome(String word)
{
return word.equals(new
StringBuilder(word).reverse().toString());
}
void printPalindromeWords(String inputString)
{
Scanner scanner = new Scanner(inputString);
System.out.println("Palindrome words in the input
string:");

while (scanner.hasNext())
{
String word = scanner.next();
if (isPalindrome(word))
{
System.out.println(word);
}
}

if (scanner != null)
{
scanner.close();
}
}
\ void main(String[] args)
{
Scanner userInputScanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String userInput = userInputScanner.nextLine();
printPalindromeWords(userInput);
userInputScanner.close();
}
}
17. Write a program to input a string and replace
uppercase letters with lowercase letters and vice-
versa.
Solution
class strreplace
{
void main ( String s)
{
for(int i=0;i<s.length();i++)
{
char ch=s.charAt(i);
if(ch=='z'&&ch=='Z')
ch=(char)(ch-25);
else if(ch>=97&&ch<=122)//(ch>=65&&ch<=90))
ch=(char)(ch+1);
System.out.print(ch);
}
}
}
18. Write a program to input a string and count
vowels.
Solution
import java.util.*;
class CountVowels
{
void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String inputString = scanner.nextLine();
int vowelCount = countVowels(inputString);
import java.util.*;

class CountVowels
{
void main(String[] args)
{
Scanner scanner = new Scanner(System.in);

System.out.print("Enter a string: ");


String inputString = scanner.nextLine();
int vowelCount = countVowels(inputString);

System.out.println("Number of vowels in the input


string: " + vowelCount);

scanner.close();
}

int countVowels(String inputString)


{
String vowels = "aeiouAEIOU";
int count = 0;

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


{
char currentChar = inputString.charAt(i);
if (vowels.indexOf(currentChar) != -1)
{
count++;
}
}
return count;
}
}

You might also like