Monday 1 November 2021

Java program to print first 'n' prime numbers

This program shows how to get input from a command line argument. In this program all possible error conditions are verified. 

PROGRAM

class Prime
{
     public static void main(String[] arg)
    {
       if (arg.length>0)
      {
          int n=Integer.parseInt(arg[0]);
          int p=1,num,cnt;
          if (n>0)
         {
          System.out.println("The first "+n+" prime numbers are");
        cnt = 1;
        System.out.println(2);
                num=3;
                while (cnt<n)
                {
                     p=1;
             for(int i=2;i<num;i++)
                     {
                                     if (num%i == 0)
                                     {
                                          p=0;
                                          break;
                                      }
                      }
                       if (p==1)
                       {
                                 System.out.println(num);
                                   ++cnt;
                       }
                       ++num;
                   }
           }
          else         
          {
              System.out.println("Enter a positive integer");
              System.exit(0);
          }
   }
   else
    {
              System.out.println("Enter a positive integer in the command line");
              System.exit(0);
      }
    
   }
 }                        
D:\Java\20CSC>java Prime 0
Enter a positive integer

D:\Java\20CSC>java Prime
Enter a positive integer in the command line

 

OUTPUT

D:\Java\20CSC>java Prime 5

The first 5 prime numbers are

2

3

5

7

11


D:\Java\20CSC>java Prime 10

The first 10 prime numbers are

2

3

5

7

11

13

17

19

23

29


D:\Java\20CSC>java Prime 1

The first 1 prime numbers are

2


D:\Java\20CSC>java Prime 2

The first 2 prime numbers are

2

3


D:\Java\20CSC>java Prime 3

The first 3 prime numbers are

2

3

5

Thursday 14 October 2021

Java program to find the maximum and minimum number in an array given as command line argument

 Given an array of numbers as input, this Java program finds the maximum number in the array using a linear search approach. Through this program, we can learn how to pass arrays as an argument to a function.

PROGRAM

class MinMax

{

   static int findMin(int[] a)

   {

      int min=a[0];

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

       {

         if (a[i]<min)

           min=a[i];

        }

        return min;

    }

   static int findMax(int[] a)

   {

      int max=a[0];

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

       {

         if (a[i]>max)

           max=a[i];

        }

        return max;

    }

    public static void main(String[] arg)

    {

      int n=arg.length;

      int[] num=new int[n];

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

       num[i]=Integer.parseInt(arg[i]);

      System.out.println("The original array");

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

      System.out.println(num[i]+"\t");

      System.out.println("The smallest number in the array = "+findMin(num));

      System.out.println("The largest number in the array = "+findMax(num));

    }

}


OUTPUT

E:\Java\Programs>java MinMax 3 4 6 2 9 1 -4 8

The original array

3

4

6

2

9

1

-4

8

The smallest number in the array = -4

The largest number in the array = 9


Thursday 26 August 2021

Java program for method overloading

 In Java, it is possible to define two or more methods with the same name but their functionality will differ based on the number of arguments and type of arguments. 

Method overloading implements one of the important object oriented programming concept called polymorphism. Poly means many. Morph means taking different forms. Here in method overloading methods with same name are defined to carry out different task.

Whenever an overloaded method is invoked, they are identified by the type and number of arguments. The overloaded methods may also differ by the return type.


PROGRAM

import java.io.*;
class Method_Overload
{
       int sum(int x,int y)
       {  
           return x+y;
       }
       float sum(int x, float a)
        {
           return x+a;
        }
        int sum(int z,char c)
        {
              return z+c;
        }
       public static void main(String args[]) throws IOException
       {
             int x,y,z;
             float a,b;
             char c;
             Method_Overload m=new Method_Overload();
             BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
             System.out.println("Enter x and y");
             x=Integer.parseInt(in.readLine());
             y=Integer.parseInt(in.readLine());
             System.out.println("Sum(x,y) = "+m.sum(x,y));
             System.out.println("Enter a value");
             a=Float.parseFloat(in.readLine());
             System.out.println("Sum(x,a) = "+m.sum(x,a));
             System.out.println("Enter a character");
             z=in.read();
             System.out.println("Sum(z,c) = "+m.sum(x,z));
          }
}

  

OUTPUT

E:\Java\Programs>javac Method_Overload.java


E:\Java\Programs>java Method_Overload

Enter x and y

5

10

Sum(x,y) = 15

Enter a value

4.5

Sum(x,a) = 9.5

Enter a character

B

Sum(z,c) = 71


Monday 28 June 2021

Python program to Expand Alphabet

Expand Alphabets

This program takes a string Str as input. The input string contains a sequence of an integer followed by an alphabet. The output must print the alphabets as many times as the related integer on its left.

Input

1a2b

Output

abb

Input 

a3r

In this case, the output will be rrr.

If only the input is given in the correct format, the output will be displayed. If the input is incorrect then no output is produced.

Incorrect Input

a3

PROGRAM

Str = input("Enter the input string:")

num = ""

l=len(Str)

i=0

while (i < l):

    if (Str[i] >= '0' and Str[i] <= '9'):

        while (i < l and Str[i] >= '0' and Str[i] <= '9' ):

            num=num+Str[i]

            i=i+1

        num=int(num)

        if (i < l):

            while (num > 0):

                print(Str[i],end="")

                num=num-1

    i=i+1

    num=""


OUTPUT

Enter the input string:100q2w

qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqww


Enter the input string:a3r

rrr

Saturday 26 June 2021

Counting alphabets and lines in a sentence

This program reads the contents of the file which is given as input. Then counts the number of lines in each sentence and also the alphabets in each sentence. Each English sentence ends with a period(.). 

For example, if the file contains 

This program reads the contents of the file which is given as input. Then counts the number of lines in each sentence and also the alphabets in each sentence. Each English sentence ends with a period. 


Here the first sentence ends in the first line so line count is 1

The second sentence starts in first line and ends in second line, so the line count is 2. 

The third sentence ends in second line, so the line count is 1.


Program

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>


int main()

{

    FILE *f1;

    char fname[20],c,pc;

    int lc=0,ac=0,sc=0,i,flag=0;

    int Line[50],Alpha[50];


    printf("Enter the file name\n");

    gets(fname);

    

    f1 = fopen(fname, "r");

    if (f1==NULL)

    {

printf("File open error");

getch();

exit(0);

    }

    printf("\n\nFile %s contents\n\n",fname);

    while((c=fgetc(f1))!=EOF)

        putchar(c);

    fclose(f1);

    printf("\n\n");

    

    f1=fopen(fname,"r");

    while(!feof(f1))

    {

c=fgetc(f1);

if (c=='\n' && pc !='.')

  ++lc;

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

  ++ac;

if (c=='.')

{

  ++sc;

  ++lc;

  Line[sc]=lc;

  Alpha[sc]=ac;

  lc=0;

  ac=0;

}

pc=c;

    }

    fclose(f1);

    for(i=1;i<=sc;i++)

    {

          printf("\nThe number of lines and alphabets in sentence %d is %d, %d", i, Line[i],                Alpha[i]);

     } 

   getch();

   return 0;

}


OUTPUT

Enter the file name

test.txt

File test.txt contents


Welcome to the world of programming. It is

fun to learn programming through blogs and

online tutorials.

Happy learning.


The number of lines and alphabets in sentence 1 is 1, 30

The number of lines and alphabets in sentence 2 is 3, 55

The number of lines and alphabets in sentence 3 is 1, 13


Friday 18 June 2021

Shell script to work with command line arguments

In this shell script, we will learn the use of the commands to display the first command-line argument and the total number of command-line arguments in a shell script.

$0 - always refers to the shell program being executed

$# - returns the total number of arguments given in the command line

$@ - returns the entire list of the arguments in the command line

$* - returns the entire list of arguments in the command line double quoted

$1, $2,.............., $9 -  returns the arguments in the position specified


PROGRAM

#Shell script to work with command line arguments
echo -e "\nThe first argument in a command line refers to the program name"
echo -e "The first argument here is $0 \n"
echo "The list of arguments given in the command line are"
echo -e "$* \n"
echo "The total number of command line arguments is " : $#



OUTPUT

$ sh clarg.sh hello welcome to unix programming


The first argument in a command line refers to the program name

The first argument here is clarg.sh


The list of arguments given in the command line are

hello welcome to unix programming


The total number of command line arguments is  : 5

Thursday 17 June 2021

MCQs in RDBMS

Relational Database Management System is a core subject taught in almost all computer science curriculum. The following 30 multiple-choice questions will help you to check your basic knowledge in this subject. It will also be a useful resource for the teachers to conduct quiz tests in RDBMS.


Choose the best answer (Multiple choice questions)

 

1. A relational database consists of a collection of ____________ .

A) Tables                     B) Fields                     C) Records                  D) Keys

2. A ________ in a table represents a relationship among a set of values.

A) Column                  B) Key                         C) Row                       D) Entry

3. The term attribute refers to a ___________ of a table.

A) Record                   B) Column                  C) Tuple                      D) Key

4. For each attribute in a relation, there is a set of permitted values, called the ________ of that attribute.

A) Domain                  B) Relation                  C) Set                          D) Schema

5. The result of the operation which contains all pairs of tuples from the two relations, regardless of whether their attribute values match.

A) Join                        B) Cartesian product   C) Intersection             D) Set difference

6. The _______operation performs a set union of two “similarly structured” tables

A) Union                     B) Join                         C) Product                  D) Intersect

7. Using which language can a user request information from a database?

A) Query                     B) Relational               C) Structural               D) Compiler

8. Student (ID, name, dept name, tot_cred) In this relation which attributes form the primary key?

A)  name                     B) dept                          C) tot_cred                  D) ID

9.  Which one of the following is a procedural language?

A) Domain relational calculus                  B) Tuple relational calculus

C) Relational algebra                                 D) Query language

10. The_____ operation allows the combining of two relations by merging pairs of tuples, one from each relation, into a single tuple.

A) Select         B) Join                         C) Union                     D) Intersection

11. The most commonly used operation in relational algebra for projecting a set of tuple from a relation          is _____________ .

A) Join             B) Projection               C) Select                      D) Union

12. The _________ provides a set of operations that take one or more relations as input and return a              relation as an output

A) Schematic representation               B) Relational algebra     

C) Scheme diagram                             D) Relation flow

13. A relation schema consists of a list of attributes and their corresponding _________ .

A)    Variables               B) Instances                C) Domains                 D) Tuples

14. Which one of the following is used to define the structure of the relation, deleting relations and    relating schema

A) DML(Data Manipulation Language)         B) DDL(Data Definition Language)

C)  Query                                                        D) Relational Schema

15. Select * from employee. What type of statement is this?

A) DML                      B) DDL                       C) View          D) Integrity constraint

16. The basic data type char(n) is a _____ length character string and varchar(n) is _____ length                     character.

A) Fixed, equal           B) Equal, variable       C) Fixed, variable       D) Variable, equal

17. To remove a relation from an SQL database, we use the ______ command.

A) Delete                    B) Purge                      C) Remove                  D) Drop table

18. Updates that violate __________ are not allowed.

A) Integrity constraints                       B) Transaction control

C) Authorization                                 D) DDL constraints

19. In SQL the spaces at the end of the string are removed by _______ function.

            A) Upper                     B) String                      C) Trim                    D) Lower

 20. Which one of the following sort rows in SQL?

            A) SORTBY               B) ALIGNBY             C) ORDERBY           D) GROUPBY

21. The _______ menu command in SQL * PLUS is used to store the query results in a file.

            A) Save                       B) Spool                      C) Run                      D)  Screen Buffer

22. SQL * PLUS is a ___________________.

            A) editor                     B) query language       C) command-line tool D)  interpreter

23. Which of the following is used to store movie and image files?

A) CLOB                      B) BLOB                       C) Binary                    D) Image

24. To include integrity constraint in an existing relation use :

A) Create table            B) Modify table          C) Alter table              D) Drop table

25.  Which of the following is used to define code that is executed/fired when certain actions or event         occur?

A) Replace                  B) Keyword                C) Trigger                    D) Cursor

26. Which of the following is used to input the entry and give the result in a variable in a procedure?

A) Put and get                        B) Get and put                        C) Out and In             D) In and out

27. The default extension for an Oracle SQL*Plus file is:

A) .txt                         B) .pls                          C) .ora                         D) .sql

28. The variables in the triggers are declared using

A) –                             B) @                           C) /                              d) &

29. Which of the following is not a PL/SQL unit?

A) Table                      B) Type                       C) Trigger                    D) Package

30. Which of the following statements can be used to terminate a PL/SQL loop?

A) GOTO                    B) EXIT WHEN         C) CONTINUE WHEN         D) BREAK

ANSWERS

1.      A               2. C                 3. B                 4.  A                5. B                 6. A    

7.  A                8. D                 9. C                 10. B               11. C               12. B              

13. C               14. B               15. A               16. C               17.  D              18. A  

19. C               20. C               21.  B              22. C               23. B               24. C              

25. C               26. D               27. D               28. B               29. A               30. B

Data Science in Tamil

You can learn Python and the necessary tools to become a Data Scientist by joining this telegram channel. 


https://t.me/joinchat/HJ27sc61uqk5MDEx

Daily online class is conducted online from 9PM and all the class materials are shared through Google classroom. The classes are very interactive and very useful. Everything is given free of cost.

இந்த குழுவில் என்ன கற்று தர போகிறோம்?

1. Python 

2. NumPy 

 3.Pandas

4. MatPlotLib

5. SciPy 

6. Scikit-learn

7. TensorFlow 

எந்த வகையில் உங்களுக்கு பயன் தரும்?

1.நீங்கள் Data scientist ஆகலாம்.

2. Python programmer ஆகலாம். Games, Websites, web applications தயாரிக்கும் கம்பெனிகளில் வேலைக்கு சேரலாம்.

3. கணிப்பொறி சிறப்பு அலுவலர் (Specialised IT officer) ஆகலாம்.


குழுவில் நண்பர்களை இணைத்து பயன் பெற செய்யுங்கள். 👇


https://t.me/joinchat/HJ27sc61uqk5MDEx

Tuesday 15 June 2021

Artificial Intelligence MCQ

 Multiple Choice Questions in Artificial Intelligence

Artificial Intelligence has taken up the Software Industry to a greater height with a lot of research being devoted to Artificial Intelligence techniques and Machine Learning algorithms. One who is interested to test his knowledge can do so by answering as many quizzes or multiple choice questions in Artificial Intelligence. This post will help the users to test their knowledge in Artificial Intelligence basics.


Choose the best answer (Multiple choice questions)

1.      Artificial Intelligence is about_____.

A)       Playing a game on Computer                             

B)    Making a machine Intelligent

C)    Programming on Machine with your Own Intelligence

D)    Putting your intelligence in Machine

 

2.      Who is known as the -Father of AI"?

A)    Fisher Ada                        B) Alan Turing            C) John McCarthy      D) Allen Newell

 

3.      Which of the following is not the type of AI?

A) Reactive machines             B) Unlimited memory            

C) Theory of mind                  D) Self-awareness

 

4.      What is the term used for describing the judgmental or common sense part of problem-solving?

A)    Heuristic                B) Critical       C) Value based           D) Analytical

 

5.      Among the given options, which search algorithm requires less memory?

A)    Optimal Search                             B) Depth First Search

C)  Breadth-First Search                     D) Linear Search

 

6.      The available ways to solve a problem of state-space-search.

A)    1                B) 2                 C) 3                 4

 

7.      Which of the given language is not commonly used for AI?

A)    LISP                      B) PROLOG               C) Python                    D) Perl


8. A technique that was developed to determine whether a machine could or could not demonstrate the artificial intelligence is known as the___.

A) Boolean Algebra    B) Turing Test             C) Logarithm              D) Algorithm


9. Which algorithm is used in the Game tree to make decisions of Win/Lose?

A) Heuristic Search Algorithm                       B) DFS/BFS algorithm

C)  Greedy Search Algorithm                         D) Min/Max algorithm


10. Which term describes the common-sense of the judgmental part of problem-solving?

A)  Values-based         B) Critical                   C) Analytical               D) Heuristic


11 A constraint satisfaction is a ______ procedure that operates in a space of constraint sets.

A)  Sorting                  B) Algorithmic            C) search         D) Algebraic


12. The need to backtrack in constraint satisfaction problem can be eliminated by ____________

A) Forward Searching                             

B) Constraint Propagation

C) Backtrack after a forward search  

D) Omitting the constraints and focusing only on goals


13. Among the given options, which is also known as inference rule?

A) Reference               B) Reform                   C) Resolution              D) Substitution


14. What is the name of the clause that is produced as a result of a resolution?

A) Contradiction         B) Parent         C) Disjunction             D) Resolvent


15. What is meant by factoring?

A) Removal of redundant variable                 B) Removal of redundant literal

C) Addition of redundant literal                    D) Addition of redundant variable


16.  A PROLOG program is composed of logical assertions called as _______________.

            A)  Horn clauses         B) Empty clauses        C) inference rules        D) productions


17.  Forward chaining systems are _______ and backward chaining systems are ________.

A) Goal-driven, goal-driven                           B) Goal-driven, data-driven

C) Data-driven, goal-driven                            D) Data-driven, data-driven


18. Which is mainly used for automated reasoning?

A)  Backward chaining                                   B) Forward chaining  

C) Logic programming                                    D) Parallel programming


19. Reasoning with some kind of measure of certainty is ___________.

            A) Statistical Reasoning                                 B) Monotonic reasoning        

            C) Non-monotonic reasoning                          D) Logical Reasoning


20. Default reasoning is another type of  _____________.

A) Analogical reasoning                                 B) Bitonic reasoning

C) Non-monotonic reasoning                         D) Monotonic reasoning


21. _________ is/are the well known Expert System/s for medical diagnosis systems.

A) MYCIN                 B) CADUCEUS         C) DENDRAL           D) SMH.PAL


22. The Bayesian network graph does not contain any cyclic graph. Hence, it is known as 

A) DCG                      B) DAG                      C) CAG                      D) SAG

 

23. A rudimentary form of learning, where the computed data is cached is called _______.

            A) Note learning                      B) Machine learning    

            C) Rote learning                      D) Stochastic learning


24.  Knowledge is generally acquired through ______________.

            A) Programming         B) Learning                 C) Inference                D) Experience


25.  The neural network that can be used as a content addressable memory is _______ networks.

            A) Hopfield          B) Perceptron              C) Bayesian       D) Backpropagation


26. The first program to support explanation and knowledge acquisition is ___________.

            A) MOLE              B) TEIRESIAS           C) XCON            D) PROSPECTOR


27.  The problem of translating text into speech is called as _____________.

            A) Speech Recognition                       B) Speech Production            

C) Speech translation                          D) Speech Transition


28. Initially, each expert system was created from scratch using ________ language.

            A) PROLOG              B) FORTRAN                        C) LISP                       D) Python


29.  Expert systems are  ___________ AI programs.

            A) weak                      B) complex                  C) strong                     D) simple


30. The first AI program to use learning techniques to construct the production rules automatically was _____________.

            A) MOLE              B) SALT              C) DENDRAL           D) META-DENDRAL

ANSWERS

        1)  B         2)  C         3)  B         4)  A                5)  B

        6)  B         7)  D         8)  B         9)  D         10) D

        11) C         12) A         13) C         14) D         15) B

        16) A         17) C         18) C         19) A         20) C

        21) A         22) B         23) C         24) D         25) A

        26) B         27) B         28) C         29) B         30) D