Tuesday, July 25, 2023

Twin Prime Number in a range

 

import java.io.*; 

import java.util.*;

 

public class Main

{static boolean checkPrimeNumber(int number) 

    { 

        int i; 

        int m = 0; 

        int flag = 0;       

        m = number/2;       

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

            return false;       

        }else{   

            for(i = 2; i <= m ;i++){       

                if(number%i == 0){       

                    flag=1;       

                    return false;       

                }       

            }       

            if(flag == 0)   

            {  

                return true; 

            }   

        } 

        return false; 

    }     

    static boolean checkTwinPrimeNumber(int number1, int number2) 

    { 

        if(checkPrimeNumber(number1) && checkPrimeNumber(number2) && Math.abs(number1 - number2) == 2) 

            return true; 

        else 

            return false; 

    } 

  

    public static void main(String[] args) 

    { 

        int startRange, endRange; 

         

        Scanner sc=new Scanner(System.in);          

      

        System.out.println("Enter start value");            

        startRange = sc.nextInt();            

                System.out.println("Enter last value");            

        endRange = sc.nextInt(); 

         

        System.out.println("The pairs of twin primes between" + startRange + " and " + endRange + "are:"); 

         

        for (int i = startRange; i < endRange; i++) { 

            if (checkTwinPrimeNumber(i, (i + 2))){ 

                System.out.printf("(%d, %d)\n", i, i + 2); 

            } 

        } 

    } 

} 

Neon Number

 import java.util.Scanner;  

import java.lang.Math;  

public class Main

{

    

public static void main(String args[])     

{     

int sum = 0, n;      

Scanner sc = new Scanner(System.in);  

System.out.print("Enter the number to check: ");  

n = sc.nextInt();  

int square = n * n; 

while(square != 0)  

{  

 

int digit = square % 10; 

sum = sum + digit;

square = square / 10;  

}  


if(n == sum)  

System.out.println(n + " is a Neon Number.");  

else  

System.out.println(n + " is not a Neon Number.");  

}  

}  



DecimalToBinary

 

import java.util.Scanner; 

import java.lang.Math; 

public class Main

{

    public static int convertDecimalToBinary(int dec) {

   

      int rem,b1=0;

      int rev = 1;

      while (dec > 0) {

         rem = dec % 2;

         dec = dec / 2;

         b1 = b1 + rem * rev;

         rev = rev * 10;

      }

    return b1;

  }

public static void main(String args[])    

{    

 

 

    Scanner in = new Scanner(System.in); 

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

 

int num = in.nextInt(); 

 

    int decimal = convertDecimalToBinary(num);

    System.out.println("Binary to Decimal");

    System.out.println(num + " = " + decimal);

  }

 

 

}

Automorphic Number

 

import java.util.Scanner; 

import java.lang.Math; 

public class Main

{

public static void main(String args[])    

{    

Scanner in = new Scanner(System.in); 

System.out.print("Enter a number to check: "); 

 

int num = in.nextInt(); 

int count=0;

int square = num*num;

int temp = num;

while(temp>0) 

{ 

count++;

temp=temp/10; 

}  

int lastDigit = (int) (square%(Math.pow(10, count)));  

if(num == lastDigit) 

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

else 

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

} 

} 

Armstrong Number

 

import java.util.Scanner; 

import java.lang.Math; 

public class Main

{

     static boolean isArmstrong(int n)  

{  

int temp, digits=0, last=0, sum=0;  

 

temp=n;  

 

while(temp>0)   

{  

temp = temp/10;  

digits++;  

}  

temp = n;  

while(temp>0)  

{

last = temp % 10;  

sum +=  (Math.pow(last, digits));

temp = temp/10;  

} 

 

if(n==sum)

return true;

else return false;  

}  

   

   

 

public static void main(String args[])    

{    

int num;  

Scanner sc= new Scanner(System.in); 

System.out.print("Enter the limit: ");

num=sc.nextInt(); 

if(isArmstrong(num)) 

System.out.print(num+ " is armstrong Number "); 

}  

} 

Fibonacci series

 

import java.util.*;

 

public class Main

{

                public static void main(String[] args) {

                        Scanner sc = new Scanner(System.in); 

                                 System.out.println( " enter a value");

 

                     int n1=0,n2=1,n3,i,count;

                     count = sc.nextInt();   

 

   

 for(i=1;i<=count;++i)//loop starts from 2 because 0 and 1 are already printed   

 {   

  n3=n1+n2;   

  System.out.print(" "+n1);   

  n1=n2;   

  n2=n3;   

 }   

 

}} 

 

Java happy Number

 

import java.util.*;

public class Main

{public static int isHappyNumber(int num){ 

        int rem = 0, sum = 0; 

         

        while(num > 0){ 

            rem = num%10; 

            sum = sum + (rem*rem); 

            num = num/10; 

        } 

        return sum; 

    } 

                public static void main(String[] args) {

                    Scanner sc = new Scanner(System.in); 

                                 System.out.println( " enter a value");

                                 int num = sc.nextInt();   

        int result = num; 

         

        while(result != 1 && result != 4){ 

            result = isHappyNumber(result); 

        } 

         

      

        if(result == 1) 

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

        else if(result == 4) 

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

    } 

} 

Java Magic Number

 

import java.util.Scanner; 

public class Main

{

                public static void main(String[] args) {

                                int n, remainder = 1, number, sum = 0; 

Scanner sc = new Scanner(System.in); 

System.out.print("Enter a number you want to check: "); 

n = sc.nextInt(); 

number = n; 

while (number > 9)               

{ 

while (number > 0) 

{ 

remainder = number % 10;  

sum = sum + remainder; 

number = number / 10;    

} 

number = sum; 

sum = 0; 

} 

if (number == 1) 

{ 

System.out.println("The given number is a magic number."); 

} 

else 

{ 

System.out.println("The given number is not a magic number."); 

} 

} 

} 

Monday, July 24, 2023

Write a program to accept a word from the user . Print the converted word where the consonants are upgraded by nearest vowel and vowels are upgraded to nearest consonant.

 import java.util.Scanner;

import java.util.Random;

public class Main

{

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

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

String word = scanner.nextLine();

scanner.close();

String convertedWord = convertWord(word);

System.out.println("Converted word: " + convertedWord);

}

private static String convertWord(String word)

{

StringBuilder converted = new StringBuilder();

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

{char convertedChar;

    

char c = word.charAt(i);

if (c!=' '){

 convertedChar = convertCharacter(c);}else{convertedChar =' ';}


converted.append(convertedChar);

}

return converted.toString();



private static char convertCharacter(char c)

{

if (isVowel(c))

{

return findNearestConsonant(c);

}

else

{

return findNearestVowel(c);

}

}



private static boolean isVowel(char c)

{

c = Character.toLowerCase(c);

return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';

}


private static char findNearestConsonant(char c)

{

while (true)

{

c--;

if (!isVowel(c))

{

return c;

}

}

private static char findNearestVowel(char c)

{

while (true)

{

c++;

if (isVowel(c))

{

return c;

}

}

}

}


Write a program to accept a string from user and display the words that are having repeating characters.

 import java.util.Scanner;

import java.util.Random;

public class Main

{

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

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

String inputString = scanner.nextLine();

String[] words = inputString.split(" ");

System.out.println("Words with repeating characters:");

for (String word : words)

{

    if (hasRepeatingCharacters(word))

    {

        System.out.println(word);

    }

}

scanner.close();

}

private static boolean hasRepeatingCharacters(String word)

{

    for (int I = 0; I < word.length() - 1; I++)

    {

        char currentChar = word.charAt(I);

        for (int j = I + 1; j < word.length(); j++)

        {

            if (currentChar == word.charAt(j))

            {

                return true;

            }

        }

    }

        return false;

}

   

}



Enter a string: bbn bnb vbn xcdxx

Words with repeating characters:

bbn

bnb

xcdxx



Write a program to form a square Matrix of characters and display the row and column having the max valued characters .

 Write a program to form a square Matrix of characters and display the row and column having the max valued characters .


import java.util.Scanner;

import java.util.Random;

public class Main

{

public static void main(String[] args) {

int size = 5;

char[][] matrix = generateRandomMatrix(size);

System.out.println("Matrix:");

displayMatrix(matrix);

int[] maxIndices = findMaxValueIndices(matrix);

int maxRow = maxIndices[0];

int maxCol = maxIndices[1];

System.out.println("\nRow and column with maximum valued character:");

System.out.println("Row: " + maxRow);

System.out.println("Column: " + maxCol);

}

public static char[][] generateRandomMatrix(int size)

{

char[][] matrix = new char[size][size];

Random random = new Random();

for (int i = 0; i < size; i++)

{

for (int j = 0; j < size; j++)

{

matrix[i][j] = (char) (random.nextInt(26) + 'A');

}

}

return matrix;

}

public static void displayMatrix(char[][] matrix)

{

int size = matrix.length;

for (int i = 0; i < size; i++)

{

for (int j = 0; j < size; j++)

{

System.out.print(matrix[i][j] + " " );

}

System.out.println();

}

}

public static int[] findMaxValueIndices(char[][] matrix)

{

int size = matrix.length;

int maxRow = 0;

int maxCol = 0;

int maxValue = matrix[maxRow][maxCol];

for (int i = 0; i < size; i++)

{

for (int j = 0; j < size; j++)

{

if (matrix[i][j] > maxValue) 

{

maxRow = i;

maxCol = j;

maxValue = matrix[i][j];

}

}

}

return new int[] { maxRow, maxCol };

}

}



Matrix:

L T B G E 

R I K G Q 

W R L O D 

S X V O B 

B Y U O G 


Row and column with maximum valued character:

Row: 4

Column: 1


Fascinating Number

 import java.util.Scanner;


public class Main

{

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

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

int number = scanner.nextInt();

scanner.close();

if (isFascinatingNumber(number))

{

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

}

else

{

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

}

}

private static boolean isFascinatingNumber(int number)

{

String concatenatedNumber = String.valueOf(number) + String.valueOf(number * 2) + String.valueOf(number * 3);

int digitCount = countDigits(concatenatedNumber);

int[] frequency = new int[10];

for (int i = 0; i < digitCount; i++)

{

char digitChar = concatenatedNumber.charAt(i);

if (!Character.isDigit(digitChar) || frequency[digitChar - '0'] > 0) 

{

return false;

}

frequency[digitChar - '0']++;

}

return true;

}

private static int countDigits(String number)

{

return number.length();

}

}

KaprekarNumbers

 KaprekarNumbers


import java.util.Scanner;


public class Main

{

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the value of p: ");

        int p = scanner.nextInt();

        System.out.print("Enter the value of q (where p < q): ");

        int q = scanner.nextInt();

        scanner.close();

        if (p >= q)

        {

            System.out.println("Invalid input. Please make sure p < q.");

            return;

        }

        int count = countKaprekarNumbers(p, q);

        System.out.println("Number of Kaprekar numbers between " + p + " and " + q + ": " + count);

        }

        private static int countKaprekarNumbers(int p, int q) 

        {

            int count = 0;

            for (int num = p; num <= q; num++)

            {

                if (isKaprekarNumber(num))

                {

                    System.out.println(num+" ");

                    count++;

                }

            }

            return count;

        }

        private static boolean isKaprekarNumber(int num)

        {

            long square = (long) num * num;

            String squareStr = String.valueOf(square);

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

            {

                String leftStr = squareStr.substring(0, i);

                String rightStr = squareStr.substring(i);

                int left = (leftStr.isEmpty()) ? 0 : Integer.parseInt(leftStr);

                int right = (rightStr.isEmpty()) ? 0 : Integer.parseInt(rightStr);

                if (left + right == num && left > 0 && right > 0)

                {

                    

                    return true;

                }

            }

        return false;

        }

}

Enter the value of p: 1
Enter the value of q (where p < q): 100
45 
55 
99 
Number of Kaprekar numbers between 1 and 100: 4



 4

 3

 2

 1

merge sort



Analysis of merge sort





The divide-and-conquer approach

 The divide-and-conquer approach

The divide-and-conquer paradigm involves three steps at each level of the recursion:

Divide the problem into a number of subproblems that are smaller instances of the

same problem.

Conquer the subproblems by solving them recursively. If the subproblem sizes are

small enough, however, just solve the subproblems in a straightforward manner.

Combine the solutions to the subproblems into the solution for the original problem.

EXAMPLE

The merge sort algorithm closely follows the divide-and-conquer paradigm. Intuitively, it operates as follows.

Divide: Divide the n-element sequence to be sorted into two subsequences of n=2

elements each.

Conquer: Sort the two subsequences recursively using merge sort.

Combine: Merge the two sorted subsequences to produce the sorted answer.

Sunday, July 23, 2023

ISCE- LIST OF PROGRAMS

1 Magic number CLICK
2 Happy number CLICK
3 Fibonacci series CLICK
4 Check twin prime from a series CLICK
5 Armstrong number CLICK
6 Automorphic number CLICK
7 decimal to binary conversion CLICK
8 a+a/2! +a/3! +a/4! +.... n th term
9 neon number in java CLICK
10 KaprekarNumbers CLICK
11 Fascinating Number CLICK
12 square Matrix CLICK
13 string having repeating characters CLICK
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
PATTERN * * * * * * * * * * *

ISCE

 THEORY

APC BOOK

PRACTICAL


PROJECT


SAMPLE COPY



UNION MAKE SET FIND SET


 













void make_set(int v) {
    parent[v] = v;
}

int find_set(int v) {
    if (v == parent[v])
        return v;
    return find_set(parent[v]);
}

void union_sets(int a, int b) {
    a = find_set(a);
    b = find_set(b);
    if (a != b)
        parent[b] = a;
}











There are two ways to improve it: 
1. Path Compression
2. Union by Rank










DAA

 The divide-and-conquer approach

DEFINITION-LINK

merge sort--LINK


SORT:

merge sort--LINK


GRAPH:

UNION MAKE SET FIND SET---LINK

PRIMS--

KRUSKAL--

DYSTRAS--

DFS--

BFS--

FLOYD--

WARSHALL--

WRAPPER CLASS

 Wrapper Classes-INTRODUCTION  CLICK

WRAPPER CLASS-AUTOBOX UNBOX  CLICK

WRAPPER CLASS-INTEGER  CLICK

WRAPPER CLASS-CHARACTER  CLICK

ARTIFICIAL INTELLIGENCE

 THOERY ---LINK

PRACTICAL---

CHAPTER WISE QUESTION----

ASSIGNMENT----

CBCS SYLLABUS-----

FOL PRACTISE--LINK


Saturday, July 22, 2023

GRAPHICS ADVANCED LEARNER

 1.

The Cohen-Sutherland Algorithm

2.

The Sutherland-Hodgman Algorithm

3.

Midpoint Subdivision Algorithm

GRAPHICS SLOW LEARNER

AI-SLOW LEARNER

AI-ADVANCED LEARNER

 

1.

If P represents 'This book is good' and Q represents 'This book is cheap', write the following sentences in symbolic form:

(a) This book is good and cheap.

(b) This book is not good but cheap.

(c) This book is costly but good.

(d) This book is neither good nor cheap.

(e) This book is either good or cheap.

 

2.

Translate the following sentences into propositional forms:

(a) If it is not raining and I have the time. then I will go to a movie.

(b) It is raining and I will not go to a movie.

(c) It is not raining.

(d) I will not go to a movie.

(e) I will go to a movie only if it is not raining.

Hints:

 Let P be the proposition 'It is raining'.

Let Q be the proposition 'I have the time'.

Let R be the proposition '1 will go to a movie'.

 

3.

Write the following sentences in symbolic form: (

a) This book is interesting but the exercises are difficult.

(b) This book is interesting but the subject is difficult.

(c) This book is not interesting. the exercises are difficult but the subject is not difficult.

(d) If this book is interesting and the exercises are not difficult then the subject is not difficult.

(e) This book is interesting means that the subject is not difficult, and conversely.

(f) The subject is not difficult but this book is interesting and the exercises are difficult.

(g) The subject is not difficult but the exercises are difficult.

(h) Either the book is interesting or the subject is difficult.

 

 

4.

Derive S from the following premises using a valid argument:

(i)                  P => Q

(ii)                Q => ¬R

(iii)               P v S

(iv)               R

 

5.

Check the validity of the following argument:

If Ram has completed B.E. (Computer Science) or MBA, then he is assured of a good job.

 If Ram is assured of a good job, he is happy.

Ram is not happy.

So Ram has not completed MBA.

 

6.

Express the following sentences involving predicates in symbolic form:

1. All students are clever.

2. Some students are not successful.

3. Every clever student is successful.

4. There are some successful students who are not clever.

5. Some students are clever and successful.

7.

Discuss the validity of the following argument:

All educated persons are well behaved.

Ram is educated.

No well-behaved person is quarrelsome.

Therefore. Ram is not quarrelsome.

8.

Test the validity of the following argument:

Babies are illogical.

Nobody is despised who can manage a crocodile.

Illogical persons are despised.

Therefore babies cannot manage crocodiles.

 

9.

Show that the following argument is valid:

All men are mortal.

Socrates is a man.

So Socrates is mortal.

 

10.

Test the validity of the following argument:

If Ram is clever then Prem is well-behaved.

If Joe is good then Sam is bad and Prem is not well-behaved.

If Lal is educated then Joe is good or Ram is clever.

Hence if Lal is educated and Prem is not well-behaved then Sam is bad.

Monday, July 17, 2023

15. Write a Prolog program to implement maxlist(List,Max) so that Max is the greatest number in the list of numbers List using cut predicate.

 15. Write a Prolog program to implement maxlist(List,Max) so that Max is the greatest number in the list of numbers List using cut predicate. 


maxlist([H],H).

maxlist([H|T],R):-

maxlist(T,M1),

 H>=M1,

 R is H,!.

maxlist([H|T],R):-

maxlist(T,M1),

 H<M1,

 R is M1.


OUTPUT:.

maxlist([1,20,2],X).

X = 20

 



16. Write a Prolog program to implement GCD of two numbers.

 16. Write a Prolog program to implement GCD of two numbers. 


gcd(X,0,X).

gcd(X,Y,Z):-

 R is mod(X,Y),

gcd(Y,R,Z).

OUTPUT:

gcd(6,9,X).

X = 3


17. Write a prolog program that implements Semantic Networks/Frame Structures.

 17. Write a prolog program that implements Semantic Networks/Frame Structures.




OUTPUT:

11. Write a Prolog program to implement maxlist(List,Max) so that Max is the greatest number in the list of numbers List.

 11. Write a Prolog program to implement maxlist(List,Max) so that Max is the greatest number in the list of numbers List.


max([H|T],R):-

 length(T,L),

 L>0 -> (  max(T,R1),  (   H > R1 ->     R is H    ;     R is R1  ) ) ; R is H.



OUTPUT:

max([6,9,7],X).

X = 9

max([6,9,70],X).

X = 70