Name: Jesica Rani Kullu Class: 11Th Arts' Roll No: 32
Name: Jesica Rani Kullu Class: 11Th Arts' Roll No: 32
import java.util.*;
class Display
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
double D,F=0;
System.out.println(“Enter the distance”);
D=in.nextDouble();
if(D<=3)
F=D*5;
if(D>=4&&D<=8)
F=D*4;
if(D>=9&&D<=13)
F=D*3;
if(D>=13)
F=D*2;
System.out.println(“Fare=”+F);
}
}
Q2. Accept a number and check it is palindrome
number or not.
import java.util.*;
class Number
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int N,A,D,R=0;
System.out.println(“Enter any number”);
N=in.nextInt();
for(A=N;A>0;A=A/10)
{
D=A%10;
R=R*10+D;
}
if(R==N)
System.out.println(“Palindrome number”);
else
System.out.println(“Not a palindrome number”);
}
}
import java.util.*;
class Number
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
double n,sr=0;cr=0;c;
int ch;
System.out.println(“Press 1 for square root”);
System.out.println(“Press 2 for cube root”);
System.out.println(“Press 3 for cube”);
System.out.println(“enter your choice”);
ch=in.nextDouble();
System.out.println(“enter the number”);
n=in.nextDouble();
switch(ch)
{
case 1:
sr=Math.sqrt(n);
System.out.println(“Square root=”+sr);
break;
case 2:
cr=Math.pow(n,1.0/3);
System.out.println(“cube root=”+cr);
break;
case 3:
c=n*n*n;
System.out.println(“cube=”+c);
break;
default: System.out.println(“invalid choice”);
}
}
}
Looping control
statement
Loop
A loop is a statement used to repeat or iterate a
number of finite numbers of times.
TYPES OF LOOP
1. Fixed iteration-The loop in which the
number of iteration or repetition is known
in advance is known as fixed iteration.
Example – for loop
2. Variable iteration – The loop in which
the number of repetition is not known in
advance is known as variable iteration.
Example- while, do-while
Structure of loop :-
for(initialisation;condition;updation)
{
Body of loop
}
Q1. Write a program to print the factor of a number.
import java.util.*;
class Factor
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int N,A=0;
System.out.println(“Enter any number”);
N=in.nextInt();
for(A=1;A>n;A=A++)
{
if (N%A==0)
System.out.println(A);
}
}
}
Q2. Write a program to print even natural number from
1 to 20.
import java.util.*;
class Even_natural
{
public static void main(String args[])
{
for(int i=1;i>20;i=i+2)
{
System.out.println(i);
}
}
}
Q3. Write a program to print the sum of factors of a
number.
import java.util.*;
class Number
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int N,A, S=0;
System.out.println(“Enter a number”);
N=in.nextInt();
for(A=1;A<=N;A=A++)
{
if (N%A==0)
S=S+A;
}
System.out.println(“Sum of factors=”+S);
}
}
import java.util.*;
class Perfect_number
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int N,A,F=0;
System.out.println(“Enter any number”);
N=in.nextInt();
for(A=1;A<N;A=A++)
{
if (N%A==0)
F=F+A;
}
if(F==N)
System.out.println(“Number is perfect”);
else
System.out.println(“Number not is perfect”);
}
}
import java.util.*;
class Number
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int N=0,D=0,S=0;
System.out.println(“Enter any number”);
N=in.nextInt();
for(A=N;A>0;A=A/10)
{
D=A%10;
S=S+D;
}
System.out.println(“Sum of digits=”+s);
}
}
import java.util.*;
class Reverse
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int a=50; int b;
for(b=a;b>0;b--)
{
System.out.println(“Reverse number is= ”+b);
}
}
}
import java.util.*;
class Reverse
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
Int R=0;
While(N>0)
{
Int d=n%10;
n=n/10;
R=R*10+d;
}
System.out.println(R);
}
}
import java.util.*;
class Number
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int N,A,sq,R,D=0;
System.out.println(“Enter a number”);
N=in.nextInt();
for(A=N;A>0;A=A/10)
{
D=D+1;
}
sq=N*N;
R=sq%(int)Math.pow(10,D);
if(R==N)
System.out.println(“Automorphic number”);
else
System.out.println(“Not an Automorphic number”);
}
}
Sum of series
1.2+4+6+8+..........to 20 terms
import java.util.*;
class series
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int A,B,S=0;
for(A=2,B=1;B<=20;A=A+2,B++)
{
S=S+A;
}
System.out.println(“Sum=”+s);
}
}
2.1/2+1/6+1/10+1/14+..........to n terms
import java.util.*;
class series
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
double A,B,S=0,n;
System.out.println(“enter the value of n”);
n=in.next
for(A=2,B=1;B<=n;A=A+4,B++)
{
S=S+1/A;
}
System.out.println(“Sum=”+s);
}
}
3. 1-2+3-4+5-6+..............to n terms
import java.util.*;
class series
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int A,B,C,N,S=0;
System.out.println(“enter the value of N”);
N=in.nextInt();
for(A=1,B=2;C=1;C<=N;A=A+2;B=B+2;C=C+2)
{
S=S+A-B;
}
System.out.println(“Sum=”+s);
}
}
Pattern program
10 10 10 10 10
20 20 20 20
30 30 30
40 40
50
import java.util.*;
class Pattern
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int A,B,C=5;
System.out.println(“enter the value of n”);
n=in.nextInt;
for(A=10;A<=50;A=A+10)
{
for(B=1;B<=C;B++)
{
System.out.println(A);
}
System.out.println(c);
C=C-1;
}
}
}
2. *
*#
*#*
*#*#
*#*#*
*#*#*#
import java.util.*;
class Pattern
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int A,B,C=1,N;
System.out.println(“enter the value of N”);
n=in.nextInt;
for(A=1;A<=6;A=A+1)
{
for(B=1;B<=C;B++)
{
if(B%2==0)
System.out.print (“#”);
else
System.out.print (“*”);
}
System.out.println();
C++;
}
}
}
Encapsulation
The process of combining the data members and the
member function into a single unit is called
Encapsulation.
The unit is called Class.
Inheritance
The process of creating new class by sharing common
characteristics and behaviour of an existing class and
adding some additional features is known as
Inheritance.
In computer programming, a string is traditionally a
sequence of character other as a literal constant or
as some kind of variable. The latter may allow its
element to be mutated and the length changed, or it
may be fixed. Astring is generally understood as a
data type and is often implemented as an array data
structure of elements, typically characters, using
some characters encoding. A string may also denote
more general arrays or other sequence data types
and structures.
Depending on programing language and precise
data type used, a variable declared to be a string
may either cause storage in memory to be statically
allocated for a pre-determined maximum length or
employ dynamic allocation to allow it to hold a
variable numbers of elements.When a string appears
literally in source code, it is knoe\wn as astring
literalor an annonymous string. In a formal
language, which are used in mathematical logic and
theoritical computer science, a string is a finite
sequence of symbols that are chosen from a set
called an alphabet.
Examples of string “School”, “xy.Tmp.3x 12.PT”
WHAT IS A CHARACTER?
Any individual alphabet, digit or a symbol
enclosed within single quotes is known as
character in java. Its data type is char and size
is 2 bytes.
Eg. ‘A’ , ‘2’, ‘*’.
CHARACTER FUNCTIONS
These functions are used to perform some
operation on a character
1.Character.isletter() –
It is used to check whether the given character
is letter of an english alphabet or not.
Its return type is boolean.
Syntax :-
boolean <var> = Character.isLetter
(Character) ;
Example
boolean P = Character.isLetter(‘A’); true
2.Character.isDigit() –It is used to check
whether the given character is a digit or not. Its
return type is boolean.
Syntax :-
boolean <var> =
Character.isDigit(Character);
Example
boolean P = Character.isDigit (‘A’); false
Char P = ‘2’;
Character.isDigit (P);true
3.Character.isLetterorDigit () –
Its returns true if given character is either letter
or digit. Its return type is boolean.
Syntax :-
boolean <var> =
Character.isLetterorDigit (Character) ;
Example
boolean P = Character.isLetterorDigit
(‘A’); true
Char P = ‘2’;
Character.isLetterorDigit (P);true
Character.isLetterorDigit (P);false
4.Character.isWhiteSpace () –
It checks whether the give character is a blank
space or not. Its return type is boolean.
Syntax :-
boolean <var> = Character.isWhiteSpace
(Character);
Example - boolean P =
Character.isWhiteSpace (‘ ’); true
boolean P = Character.isWhiteSpace (‘
A’); false
4.Character.isLowerCase () –
It checks whether the given character is
lower case or not. Its return type is
boolean.
Syntax :-
boolean <var> = Character.isLowerCase
(Character) ;
Example
boolean P = Character. isLowerCase (‘a
’); true
boolean P = Character. isLowerCase (‘
A’); false
5.Character.isUpperCase () –
It checks whether the given character is
UpperCase or not. Its return type is boolean.
Syntax :-
boolean <var> = Character. isUpperCase
(Character) ;
Example
boolean P = Character. isUpperCase (‘a
’); false
boolean P = Character. isUpperCase (‘
A’); true
6.Character.toLowerCase () –
It converts the given character into lower
case letter if it is an upper case letter
otherwise returns the same character. Its
return type is char.
Syntax :-
char <var> = Character.toLowerCase
(Character) ;
Example
char P = Character. toLowerCase (‘D’); d
char P = Character. toLowerCase (‘d’); d
char P = Character. toLowerCase (‘*’); *
7.Character.toUpperCase () –
It converts the given character into upper
case letter if it is an lower case letter
otherwise returns the same character. Its
return type is char.
Syntax :-
char <var> = Character. toUpperCase
(Character) ;
Example
char P = Character. toUpperCase (‘d’); D
char P = Character. toUpperCase (‘D’); D
char P = Character. toUpperCase (‘*’); *
1.A program to input a character to check and
display whether it is letter or not. If it is letter
diplaying after changing its case.
import java.util.*;
Class Display
{
public static void main ( String args[])
{
Scanner in = new scanner ( System.in );
Char k;
System.out.println (“ Enter any character”);
k = in.next().CharAt(O);
if (character.isLetter(k))
{
System.out.println (“It is a letter”);
if (character.isUpperCase (k))
{
K= character.toLowerCase(k);
System.out.println (k);
}
Case
{
K= character.toUpperCase (k);
System.out.print (k);
}
else
System.out.println (“Not a Letter”);
}
}
}
2.A program to input a character to check and
display whether it is letter, digit or symbol
import java.util.*;
Class Display
{
public static void main ( String args[])
{
Scanner in = new scanner ( System.in );
Char k;
System.out.println (“ Enter any character”);
k = in.next().CharAt(O);
if (character.isLetter(k))
{
System.out.println (“It is a letter”);
}
else if ( Character.isDigit (k))
{
System.out.println (“It is a Digit”);
}
else
System.out.println (“It is a Symbol”);
}
}
ASCII CODE
Every character in java have an integral value
or numeric value that is known as its ASCII
Code.
Full form of ASCII is American Standard Code
for Information or Instruction Interchange.
Character
ASCII Code
A-Z
65-90
a-z 97-122
0-9 48-57
Output of the following :
1.Char P = ‘2’;
Int q = (int) P;
System.out.println (“ P =” + P);
System.out.println (“q=” + q);
Output :
P=2
q = 50
2.Int P = 120;
Char q = (Char) P;
System.out.println (“ P =” + P);
System.out.println (“q=” + q);
System.out.print ( (q+1));
Output :
P = 120
q = x121
STRING FUNCTION
String N = “PM.123 abc”, M = “BANANA”, P =
“PAPAYA”
1.length () – It is used to find the count of
number in the string. Its return type is int.
Syntax
Int <Var> = String Variable.length ();
N.String (); 10
2.CharAt () –It is used to find the character at a
given index in a string. Its return type is char.
Syntax
Char <Var> = String Variable.CharAt
(index);
M.CharAt (3); A
3. indexOf (Character) – It returns the index
of the first occurance of the given character in a
string. Its return type is int.
Syntax
Int <Var> = String Variable.indexOf
(Character);
P.indexOf (A); 1
4. indexOf (Character,index) – It returns the
index of the given character from the given
index in a string. Its return type is int.
Syntax
Int <Var> = String Variable.indexOf
(Character,index);
P.indexOf (‘A’,2); 3
M.indexOf(‘N’,2); 2
5. lastIndexOf (Character) – It returns the
index of the last occurance of the given
character in a string. Its return type is int.
Syntax
Int <Var> = String Variable.lastIndexOf
(Character);
P.lastIndexOf (‘A’); 5
M.lastIndexOf (‘N’);4
6. subString (index) – It returns the part of a
string from the given index. Its return type is
string.
Syntax
String <Var> = String Variable. subString
(index);
N.subSString(4); 23 abc
“MP3”.sbuString(2);
7. subString (index1,index2) – It returns all
the characters of the string from index 1 to
index 2. Including the character at index 1 but
excluding the character at index 2. Is return
type is string.
Syntax
String <Var> = String Variable. subString
(index1,index2);
N.subSString(4,7); 23
P.substring(2,6); PAYA
8. replace() – It is used to replace a character
with another character or some string with
another sub string of the given string. Its return
type is string.
Syntax
String <Var> = String Variable.
(Char1,Char2);
M.replace(‘A’, ‘P’ ); BPNPNP
N.replace(“abc”, “xy”);
PM.123xy
9. trim () – It is used to remove all the blank
space from the begining and end of a string. Its
return type is string.
Syntax
String <Var> = String Variable.trim();
N.trim(); PM.123abc
10. equals () –It is used to check whether the
two strings are equal or not. Its return type is
boolean.
Syntax
boolean <Var> = String
Variable.equals(String Variable 2);
M.equals(P); false
“Rakesh”.equals
(“RakesH”); false
“RAKESH”.equals(“RAKESH”); true
11. equalsIgnoreCase() –It is used to check
whether the two strings are equal or not after
ignoring the case. Its return type is boolean.
Syntax
boolean <Var> = String
IgnoreCaseVariable.equals(String Variable
2);
M.equalsIgnore(P); false
“Rakesh”. equalsIgnore (“RakesH”); true
“RAKESH”. equalsIgnore (“RAKESH”);
false
12. startsWith() –It check whether the give
string being starting with given string or not.
Its data type is boolean.
Syntax
boolean <Var> = String Variable. startsWith
(subString);
N. startsWith (“PMI”); false
M. startsWith (“BA”); true
13.endsWith () –It check whether the give string
is ending with given string or not. Its data type
is boolean.
Syntax
boolean <Var> = String Variable. endsWith
(subString);
14. valueOf () –This function returns the object
holding the value of the specified string. All
types of data value can be
passed as argument to the valueOf () function.
It just returns the string value of it.
Syntax
String <Var> = String Variable.valueOf();
15. compareTo () –This function compares
two strings and returns an integer value. It
not only checks the quality of the strings but
also checks whether astring is bigger or
smaller that the other string. Its return type is
int.
Syntax
int <Var> = String Variable. compareTo ();
16. concat () – This function concatenates two
strings. It accepts a string as an argument and
concatenates it with the calling string. Its return
type is string.
Syntax
String <Var> = String Variable.concat ();
2.A program to input a name containing three
words and display the first name, middle name
and last name separately.
N = “RAM PRASAD SHARMA”
import java.util.*;
Class String
{
public static void main ( String args[])
{
Scanner in = new scanner ( System.in );
String N,FN,MN,LM;
int A,B;
System.out.println (“ Enter a name
containing three words”);
N = in.nextLine ();
A = N.indexOf (‘’);
B = N.lastindexOf (‘ ‘);
FN = N.subString (OA);
MN = N.subString (A+1,B);
LN = N. subString (B+1);
System.out.println (“ First name =” + FN );
System.out.println (“ Middle name=” + MN);
System.out.println (“ Last name =” + LN);
}
}
3.A program to input two any words and
check and display whether they are equal in
length or not. If they are not equal display the
longer word along with its length.
import java.util.*;
Class String
{
public static void main ( String args[])
{
Scanner in = new scanner ( System.in );
String M,N;
int L1, L2;
System.out.println (“ Enter two words”);
M = in.nextLine();
N = in.nextLine();
L1 = M.length ();
L2 = N.length ();
if ( L1 = = L2 )
System.out.println (“ Both are equal in length
“);
else if ( L1>L2);
{
System.out.println (“Longer word =” + M);
System.out.println (“Its Length =” + L1);
}
else
{
System.out.println (“Longer word =” + N);
System.out.println (“Its Length =” + L2);
}
}
}
DIGITAL NUMBER
SYSTEM
In digital number representation, various
number systems are used. The most common
number system used are decimal, binary, octal,
and hexadecimal system. Let us discuss these
number system briefly.
1. Decimal to binary
2. Binary to decimal
3. Decimal to octal
4. Octal to decimal
5. Octal to binary
6. Binary to octal
7. Decimal to hex decimal
8. Hex to decimal
9. Binary to hex
10.Hex to binary
Propositional Logic & Hardware
Any statement that may either evaluate
to true or false,
Ex – it is not raining.
The above statements evaluates to
either true or false, so it is
propositional logic.
Compound preposition
It is a combination of two or more
simple preposition.
Ex – It is raining and wind is
blowing.
Operator(connective)
An operator joins simple
proposition into compound
proposition. It is also called
connective.
DIFFERENT TYPES OF
CONNECTIVE
1. Disjunctive(OR) (+,v)
2. Conjunctive (AND) (‘.’,’
^’,’&’)
3. Conditional /implication
4. Bi-conditional
5. Negation (Not) (- , ‘, ~)
pos=s.nextInt();
System.out.println(“Enter the element you want
to insert”);
x=s.nextInt();
for(int i=(n-1);i>=(pos-1);i--)
{
a[i+1]=a[i];
}
a[pos-[]]=a[i];
System.out.println(“After inserting”);
for(int i=0;i<n;i++)
{
System.ot.print(a[i]+”,”);
}
}
}
6. Deletion
import java.util.*;
class Array
{
public static void main(String args[])
{
int n,x,flag=0;loc=0,i;
Scanner in=new Scanner(System.in);
System.println(“Enter number of elements you
want in array”);
n=s.nextInt();
int a[]=new int[];
System.out.println(“Enter all the elements ”);
for(int i=0;i<n;i++)
a[i]=s.nextInt();
System.out.println(“Enter the element you want
to delete “);
x=s.nextInt();
for(int i=(n-1);i>=(pos-1);i--)
{
if(a[i]==x)
{
flag=1;
loc=i’
break;
}
}
if(flag==1)
{
for(i=loc+1;i<n;i++)
a[i-1]=a[i];
System.out.println(“After Deleting”);
for(i=0;i<n-2;i++);
System.out.print(a[i]+”,”);
}
else
System.out.println(“Enter not found”);
}
}
7.MERGING
import java.util.*;
class Merging
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int a[]=new int[5];
int b[]=new int[8];
int c[]=new int[a.length+b.length];
System.out.println(“Enter element in array
b”);
for(int=0;i<5;i++)
b[i]=in.nextInt();
int k=0;
System.out.println(“Merging begin.........”)
for(int j=0;j<8;j++)
{
if(j<5){
c(k++)=a[j];
c(k++)=b[j];
else
c(k++)=b[j];
}
System.out.println(“Printing the merged
array”);
for(int i=0;i<c.length;i++)
System.out.println(c[i]);
}
}
Types of function
1. Pure function-it does not change the value
of the received parameter/object.
2. Impure function-it modifies the value of the
received parameter/object.