Chapter # 3 

 
Sr.no  Topic Date Sign
1.  Reading strings from the keyboard
9‐1‐2007
2.  Changing String order
9‐1‐2007
3.  More than one class
9‐1‐2007
4.  Assigning values to variables
9‐1‐2007
5.  Diamond pattern on the console screen
9‐1‐2007
 
 
Chapter # 4 
Sr.no  Topic Date Sign
1.  Illustrating the Concept of Declaration of variables
16‐1‐2007
2.  Declaration & Additions of variables
16‐1‐2007
3.  Program with a function
16‐1‐2007
4.  Demonstrating Boxing & Unboxing
16‐1‐2007
5.  Demonstrating addition of byte type variables
16‐1‐2007
6.  Implementing some custom console output
16‐1‐2007
7.  Printing a home like figure in the console
16‐1‐2007
8.  Executing some console statements
16‐1‐2007
 
 
 
 
 
 
 
Vishal
Shah
Digitally signed by
Vishal Shah
DN: cn=Vishal Shah
Date: 2012.07.20
22:44:27 +05'30'
 
 
 
 
 
 
 
Chapter # 3 
(Overview of C#) 
 
 
 
 
 
 
 
 
 
 
 
3.1 ‐ Reading strings from the keyboard 
 
 
using System; 
class Prog3_1 
{     
  public static void Main() 
  { 
    Console.Write ("Enter Your First Name : "); // Displaying to write first name 
    string name1 = Console.ReadLine (); // Saving first name in name1 
    Console.Write ("Enter Your Last Name : "); // Displaying to write last name 
    string name2 = Console.ReadLine (); // Saving first name in name2 
    Console.WriteLine ("Hello Mr." + name1 +" " + name2); // Displaying both first & last names 
    Console.ReadLine (); // Since to stop the console for displaying last line, we use this to accept a 
keystroke frm user. (Similar to getch() in C) 
  } 

 
 
OUTPUT 
Enter Your First Name: Daljit 
Enter Your Last Name: Singh 
Hello Mr. Daljit Singh 
 
 
 
 
 
3.2 ‐ Changing String order 
 
using System; 
 
class Prog3_2 
{     
  public static void Main(String [] args) 
  { 
    Console.Write(args[2] + args[0] + args[1]); 
  } 

     
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3.3 – More than one class 
using System; 
class ClassOne 

  public void One() // A function named One 
  { 
    Console.Write("C Sharp "); 
  } 

 
 
class Mainly 

  public static void Main() // A function named Main (Main Function) 
  { 
    ClassOne demoObj = new ClassOne (); //Creating ojecct of ClassOne 
    demoObj.One (); // Will display ‐‐‐> C Sharp 
    Console.Write ("Programming"); // Will display ‐‐‐> Programming 
    // Both "C Sharp" & "Programming" will be displayed in a single line due to this line ‐‐‐‐> 
Console.Write("C Sharp "); 
    Console.ReadLine (); 
  } 
 

OUTPUT 
C Sharp Programming 
3.4 – Assigning values to variables 
 
using System; 
class SampleMath 

  public static void Main() 
  { 
    double x = 2.0; // declaring a variable named x of type double & assigning it value 2.0 
    double y = 3.0; // declaring a variable named y of type double & assigning it value 3.0 
    double z ;      // declaring a variable named z of type double 
    z = x + y;  
    Console.WriteLine("x = " + x + ", y = " + y + " & z = " + z); 
    Console.ReadLine(); 
  } 

 
 
OUTPUT 
 
X = 2.0, Y = 3.0, Z = 5.0 
 
 
 
 
 
 
 
3.5 – Diamond pattern on the console screen 
 
using A = System.Console; 
class Pattern 

                  public static void Main() 
                 { 
                       A.WriteLine ("  X  "); 
                       A.WriteLine (" XXX "); 
                       A.WriteLine ("XXXXX"); 
                       A.WriteLine (" XXX "); 
                       A.WriteLine ("  X  "); 
                       A.ReadLine (); 
                 } 

 
 
 
OUTPUT 
  X 
 XX 
XXX 
 XX 
  X 
 
 
 
 
 
 
 
 
 
 
Chapter # 4 
(Literals, Variables & Data Types) 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4.1 – Illustrating the Concept of Declaration of variables 
 
 
class Variable_Concepts 
{  
       public static void Main () 
       { 
          char ch = 'A'; // Declaring a Character variable with value = 'A' 
          byte a = 50;  // Declaring a byte variable with value = 50 
          int b = 123456789; // Declaring an Integer variable with value = 123456789 
          long c = 1234567654321; // Declaring a Long type variable with value = 1234567654321 
         bool d = true; // Declaring a Boolean type variable with TRUE value 
         float e = 0.000000345F; // Declaring a float type variable with value = 0.000000345. The value ends with a 
'F' resembeling a float data type 
       float f = 1.23e5F; // Declaring a float type exponential variable with value = 1.23E5 = 123000. The value 
contains the character 'e' resembeling an exponential value.Also, the value ends with a 'F' resembeling a float 
data type. 
       } 

 
 
 
 
 
 
 
 
 
4.2 – Declaration & Additions of variables 
using System; 
class DeclareAndDisplay 

      public static void main() 
         { 
             float x; // Declaring x of float type 
             float y; // Declaring y of float type 
             int m;   // Declaring m of integer type 
             x = 75.86F; 
             y = 43.48F; 
             m = x + y; // This line will create an ERROR. Reason given below. 
             Console.WriteLine("m = x + y = 75.86 + 43.48 = " +m); 
         } 

//*********************** Comment on the output ***************** 
//We declared 2 float type variables.  
//Added them 
//Saved the result in an Integer variable 
//Since the result of addition of 2 float numbers is a float only ... 
//We cannot save that value in an integer variable. 
//C# has strict check for data conversions taking place. 
//It does not automatically converts a larger data type to smaller one since it will create a loss of data. 
//For this purpose, we need to explicitly make the integer variable 'm' to float type. 
//If 'm' is also a float variable, then the output would have been like this ... 
//m = x + y = 75.86 + 43.48 = 119.34 
 
4.3 – Program with a function 
 
 
class ABC 

        static int m; 
        int n; 
        void fun(int x, ref int y, out int z, int [] a) 
              { 
                 int j = 10; 
              } 

 
// ***************** Comment on Output ******************* 
// The out parameter 'z' must be assigned to before the control leaves the current method 
 
 
 
 
 
 
 
 
 
 
 
 
4.4 ‐ Demonstrating Boxing & Unboxing 
 
using System; 
class Boxing 

      public static void main(string[] a) 
       { 
            // ************************ BOXING ************************** 
               int m = 10; 
               object om = m; // creates a box to hold m 
               m = 20; 
              Console.WriteLine("************************* BOXING ********************"); 
              Console.WriteLine("m = " + m); // m = 20 
              Console.WriteLine("om = " +om);// om = 10 
              Console.ReadLine(); 
            // *********************** UNBOXING *********************** 
               int n = 10; 
               object on = n; // box n (creates a box to hold n) 
               int x = (int)on; // unbox on back to an int 
              Console.WriteLine("************************* UNBOXING ********************"); 
              Console.WriteLine("n = " + n); // n = 20 
              Console.WriteLine("on = " +on);// on = 10 
              Console.ReadLine(); 
          } 

 
 
4.5 – Demonstrating addition of byte type variables 
using System; 
class addition 

          public static void Main() 
               { 
                  byte b1; 
                  byte b2; 
                  int b3; // We are required to declare b3 as byte BUT its declared as int. The reason is given below. 
                  b1 = 100; 
                  b2 = 200; 
             // Normally this is the addition statement  
            //        b3 = b1 + b2; 
           // However it gives an error that cannot convert 'int' to 'byte'. 
           // When b2 & b3 are added, we get an integer value which cannot be stored in byte b1 
           // Thus we will declare b3 as integer type & explicitly convert b2 & b3 to int. 
                 b3 = (int)b1 + (int)b2; 
                 Console.WriteLine("b1 = " + b1);  
                 Console.WriteLine("b2 = " + b2); 
                 Console.WriteLine("b3 = " + b3); 
                 Console.ReadLine(); 
              } 

OUTPUT 
b1 = 100 
b2 = 200 
b3 = 300 
4.6 – Implementing some custom console output 
using System; 
class Demo 

      public static void Main() 
         { 
               Console.WriteLine("Hello, ###BOT_TEXT###quot;Ram###BOT_TEXT###quot;!"); 
              // Output ‐‐‐> Hello, "Ram"! 
             // Reason ‐‐> Due to the ###BOT_TEXT###quot; character, the characters Ram is in double quotes 
                Console.WriteLine("*\n**\n***\n****\n"); 
             //Reason ‐‐> Due to the \n character, we get each set of * in a new line. 
               Console.ReadLine(); 
          } 

OUTPUT 
 
Hello, “Ram” ! 

** 
*** 
 
 
 
 
 
 
 
4.7 – Printing a home like figure in the console 
using System; 
class Home 

  public static void Main() 
  { 
    Console.WriteLine("      / \   "); 
    Console.WriteLine("     /   \  "); 
    Console.WriteLine("    /     \ "); 
    Console.WriteLine("   ‐‐‐‐‐‐‐‐‐ "); 
    Console.WriteLine("   ###BOT_TEXT###quot;      ###BOT_TEXT###quot; "); 
    Console.WriteLine("   ###BOT_TEXT###quot;      ###BOT_TEXT###quot; "); 
    Console.WriteLine("   ###BOT_TEXT###quot;      ###BOT_TEXT###quot; "); 
    Console.WriteLine("\n\n This is My Home."); 
    Console.ReadLine(); 
  } 

 
OUTPUT 
      / \  
    /      \ 
  /          \ 
‐‐‐‐‐‐‐‐‐‐‐‐‐ 
“              ” 
”              ” 
”              ” 
 
4.8 – Executing some console statements 
using System; 
 
class Demo 

  public static void Main() 
  { 
    int m = 100; 
    long n = 200; 
    long l = m + n; 
    Console.WriteLine("l = "+ l); 
    Console.ReadLine(); 
    // No error in the program. 
  } 

 
OUTPUT 
l = 300 
 
 
 
 
 
 
 
 
 
 
 
Chapter # 5 
Sr.no  Topic  Date  Sign 
1.  Computation of Integer Values taken from console  30/1/2007   
2.  Computation of Float Values taken from console  30/1/2007   
3.  Average of 3 numbers  30/1/2007   
4.  Finding circumference & area of a circle  30/1/2007   
5.  Checking for validity of an expression  30/1/2007   
6.  Converting Rs. To Paisa  30/1/2007   
7.  Converting temp. from Fahrenheit to Celsius  30/1/2007   
8.  Determining salvage value of an item  30/1/2007   
9.  Reading & displaying the computed output of a real no.  30/1/2007   
10.  Evaluating distance travelled by a vehicle  30/1/2007   
11.  Finding the EOQ(Economic Order Quantity) & TBO(Time between Orders)  30/1/2007   
12.  Finding the frequencies for a range of different capacitance.  30/1/2007   
 
 
Chapter # 6 
Sr.no  Topic  Date  Sign 
1. 
Adding odd & even nos from 0 – 20 & adding nos. divisible by 7 between 100 ‐ 200  6/1/07
2. 
Finding a solution of linear equation  6/1/07
3. 
Computing marks of students  6/1/07
4. 
Selecting students on the basis of some given criteria on marks  6/1/07
5. 
Printing Floyd’s triangle  6/1/07
6. 
Computing seasonal discount of a showroom  6/1/07
7. 
Reading ‘x’, Correspondingly Printing ‘y’  6/1/07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Chapter # 5 
(Operators & Expressions) 
 
 
 
 
 
 
 
 
 
 
 
 
5.1 # Computation of Integer Values taken from console 
usi ng Syst em;

cl ass i nt eger demo
{
publ i c st at i c voi d Mai n( )
{

st r i ng s1, s2;
i nt a, b;

Consol e. Wr i t e( " Ent er no 1 # " ) ; / / Di spl ay t o ent er no. 1
s1 = Consol e. ReadLi ne ( ) ; / / save t he number i n a st r i ng var i abl e s1
a = i nt . Par se ( s1) ; / / t he st r i ng s1 i s conver t ed i nt o i nt t ype var i abl e

Consol e. Wr i t e( " Ent er no 2 # " ) ; / / Di spl ay t o ent er no. 2
s2 = Consol e. ReadLi ne ( ) ; / / save t he number i n a st r i ng var i abl e s2
b = i nt . Par se ( s2) ; / / t he st r i ng s2 i s ci nver t ed i nt o i nt t ype var i abl e

/ / Her e er conver t ed bot h t he st r i ng var i abl es t o i nt because we want ed t o do
/ / i nt eger / numer i c mani pul at i on wi t h t he i nput t ed st r i ng var i abl es

Consol e. Wr i t eLi ne( " " ) ; / / Bl ank l i ne
Consol e. Wr i t eLi ne( " ********************* I nt eger mani pul at i ons
**********************" ) ;
Consol e. Wr i t eLi ne( " " ) ; / / Bl ank l i ne

/ / I nt eger mani pul at i ons

Consol e. Wr i t eLi ne( " No1 + No2 = " + ( a+b) ) ;
Consol e. Wr i t eLi ne( " No1 - No2 = " + ( a- b) ) ;
Consol e. Wr i t eLi ne( " No1 / No2 = " + ( a/ b) ) ;
Consol e. Wr i t eLi ne( " No1 * No2 = " + ( a*b) ) ;
Consol e. Wr i t eLi ne( " No1 %No2 = " + ( a%b) ) ;

Consol e. ReadLi ne( ) ;

}
}
 
 
Output: 
Ent er no 1 # 25
Ent er no 2 # 15
********************* I nt eger mani pul at i ons **********************
No1 + No2 = 40
No1 - No2 = 10
No1 / No2 = 1
No1 * No2 = 375
No1 %No2 = 10
5.2 # Computation of Float Values taken from console 
usi ng Syst em;
usi ng Syst em;

cl ass f l oat demo
{
publ i c st at i c voi d Mai n( )
{

st r i ng s1, s2;
f l oat a, b;

Consol e. Wr i t e( " Ent er no 1 # " ) ; / / Di spl ay t o ent er no. 1
s1 = Consol e. ReadLi ne ( ) ; / / save t he number i n a st r i ng var i abl e s1
a = f l oat . Par se ( s1) ; / / t he st r i ng s1 i s conver t ed i nt o f l oat t ype var i abl e

Consol e. Wr i t e( " Ent er no 2 # " ) ; / / Di spl ay t o ent er no. 2
s2 = Consol e. ReadLi ne ( ) ; / / save t he number i n a st r i ng var i abl e s2
b = f l oat . Par se ( s2) ; / / t he st r i ng s2 i s ci nver t ed i nt o f l oat t ype var i abl e

/ / Her e er conver t ed bot h t he st r i ng var i abl es t o f l oat because we want ed t o
do
/ / f l oat / numer i c mani pul at i on wi t h t he i nput t ed st r i ng var i abl es

Consol e. Wr i t eLi ne( " " ) ; / / Bl ank l i ne
Consol e. Wr i t eLi ne( " ********************* I nt eger mani pul at i ons
**********************" ) ;
Consol e. Wr i t eLi ne( " " ) ; / / Bl ank l i ne

/ / I nt eger mani pul at i ons

Consol e. Wr i t eLi ne( " No1 + No2 = " + ( a+b) ) ;
Consol e. Wr i t eLi ne( " No1 - No2 = " + ( a- b) ) ;
Consol e. Wr i t eLi ne( " No1 / No2 = " + ( a/ b) ) ;
Consol e. Wr i t eLi ne( " No1 * No2 = " + ( a*b) ) ;
Consol e. Wr i t eLi ne( " No1 %No2 = " + ( a%b) ) ;

Consol e. ReadLi ne( ) ;

}
}
 
 
Output: 
Ent er no 1 # 25. 64
Ent er no 2 # 15. 87
********************* Fl oat mani pul at i ons **********************
No1 + No2 = 41. 51
No1 - No2 = 9. 77
No1 / No2 = 1. 615627
No1 * No2 = 406. 9068
No1 %No2 = 9. 77
5.3 # Average of 3 numbers 
usi ng Syst em;

cl ass aver age
{
publ i c st at i c voi d Mai n( )
{
f l oat a = 25;
f l oat b = 75;
f l oat c = 100;
f l oat avg = ( a+b+c) / 3;
Consol e. Wr i t eLi ne( " The aver age of 25, 75 & 100 = " + avg) ;
Consol e. ReadLi ne( ) ;
}
}

Out put :
The aver age of 25, 75 & 100 = 6. 6666666
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5. 4 # Finding circumference & area of a circle 


usi ng Syst em;

cl ass ci r cl e
{
publ i c st at i c voi d Mai n( )
{
f l oat r adi us = 12. 5F;
f l oat ci r cumf r ence, ar ea;
f l oat pi = 3. 1487F;

ci r cumf r ence = 2 * pi * r adi us;
ar ea = pi * r adi us * r adi us;

Consol e. Wr i t eLi ne( " The Radi us of t he ci r cl e = " + r adi us) ;
Consol e. Wr i t eLi ne( " " ) ; / / Bl ank Li ne
Consol e. Wr i t eLi ne( " I t s Ci r cumf r ence = " + ci r cumf r ence) ;
Consol e. Wr i t eLi ne( " I t s Ar ea = " + ar ea) ;
Consol e. ReadLi ne( ) ;

}

 
Output: 
The Radi us of t he ci r cl e = 12. 5
I t s Ci r cumf er ence = 78. 7175
I t s ar ea = 491. 9844
 
 
 
 
 
 
 
 
 
 
 
5.5 # Checking for validity of an expression 
usi ng Syst em;

cl ass CheckExpr essi on
{
publ i c st at i c voi d Mai n( )
{
i nt x, y, a, b;
x - y = 100;
/ / gi ves er r or
/ / " The l ef t - hand si de of an assi gnment must be a var i abl e, pr oper t y or
i ndexer "

x - ( y = 100) ;
/ / gi ves er r or
/ / " Onl y assi gnment , cal l , i ncr ement , decr ement , and new obj ect expr essi ons
/ / can be used as a st at ement "


}
}




















5. 6 # Converting Rs. To Paisa 
usi ng Syst em;

cl ass Money
{
publ i c st at i c voi d Mai n( )
{
f l oat RsF;
st r i ng s;
Consol e. Wr i t e( " Ent er t he amount i n Rs. : " ) ;
s = Consol e. ReadLi ne( ) ;
RsF = f l oat . Par se( s) ;
Consol e. Wr i t eLi ne( " Amount i n pai se = " +( RsF*100) ) ;
Consol e. ReadLi ne( ) ;
}
}

 
Out put :
Ent er t he amount i n Rs. : 15
Amount i n pai se = 1500















5. 7#Converting temp. from Fahrenheit to Celsius 
usi ng Syst em;

cl ass Temper at ur e
{
publ i c st at i c voi d Mai n( )
{
f l oat f ahr enhei t , cel ci us;
st r i ng s;
Consol e. Wr i t e( " Ent er t he t emper at ur e i n f ahr enhei t : " ) ;
s = Consol e. ReadLi ne( ) ;
f ahr enhei t = f l oat . Par se( s) ;
cel ci us = ( f l oat ) ( ( f ahr enhei t - 32) / 1. 8) ;
Consol e. Wr i t eLi ne( " The Temper at ur e i n cel ci us = " +cel ci us) ;
Consol e. ReadLi ne( ) ;
}
}

Out put :
Ent er t he t emper at ur e i n f ahr enhei t : 98 
Temper at ur e i n cel ci us = 36. 66667


















5. 8 # Determining salvage value of an item 
usi ng Syst em;

cl ass depr eci at i on
{
publ i c st at i c voi d Mai n( )
{
f l oat depr eci at i on, Pur chasePr i ce, Yr s, Sal vageVal ue;
st r i ng d, p, y;
/ / st r i ng var i abl es ar e t o st or e t he val ues i nput t ed i n t he consol e
/ / each st r i ng var i abl e has i t s char act er as t hat of t he cor r espondi ng
/ / st ar t i ng char act er of f l oat t ype var i abl e

Consol e. Wr i t e( " Ent er t he Depr eci at i on : " ) ;
d = Consol e. ReadLi ne( ) ;
depr eci at i on = f l oat . Par se( d) ;

Consol e. Wr i t e( " Ent er t he Pur chasePr i ce : " ) ;
p = Consol e. ReadLi ne( ) ;
Pur chasePr i ce = f l oat . Par se( p) ;

Consol e. Wr i t e( " Ent er t he Amount of Year s : " ) ;
y = Consol e. ReadLi ne( ) ;
Yr s = f l oat . Par se( y) ;

Sal vageVal ue = ( f l oat ) ( Pur chasePr i ce - ( depr eci at i on * Yr s) ) ;

Consol e. Wr i t eLi ne( " Sal vageVal ue = " + Sal vageVal ue) ;

Consol e. ReadLi ne( ) ;



}
}


Out put :
Ent er t he Depr eci at i on : 50
Ent er t he Pur chasePr i ce : 15000
Ent er t he Amount of Year s : 15
Sal vageVal ue = 3456. 4564

 
 
 
5.11 # Evaluating distance travelled by a vehicle 
usi ng Syst em;

cl ass Di st ance
{
publ i c st at i c voi d Mai n( )
{
f l oat di st ance, u, t , a;
st r i ng u1, t 1, a1, r epl y;

/ / u = I ni t i al vel oci t y
/ / t = Ti me i nt er val s
/ / a = Accel er at i on
/ / r epl y i s t he val ue used t o check f or agai n r est ar t t he pr ogr amwi t h
di f f er ent val ues

i nt r epl yf or r est ar t , count er ;
/ / r epl yf or r est ar t wi l l t ake val ues ei t her 0 or 1.
/ / 1 means r est ar t f or next set of val ues, 0 means exi t t he pr ogr am
/ / count er i s used f or checki ng t he no. of t i mes t he set of val ues occur s

Consol e. Wr i t eLi ne( " ******** Thi s wi l l cal cul at e t he di st ance t r avel l ed by a
vehi cl e **********" ) ;

count er = 1;
/ / For t he f i r st r un, count er = 1

st ar t f r omher e: / / The pr ogr amwi l l r est ar t f r omher e f or anot her set of
val ues.

di st ance = u = t = a = 0. 0F; / / r eset t i ng al l val ues t o 0

Consol e. Wr i t eLi ne( " " ) ; / / Bl ank Li ne

Consol e. Wr i t eLi ne( " Set of val ue = " + count er ) ;
/ / Di spl ays t he no. of set of val ue

Consol e. Wr i t eLi ne( " " ) ; / / Bl ank Li ne

Consol e. Wr i t e( " Ent er t he t i me i nt er val ( t ) : " ) ;
t 1 = Consol e. ReadLi ne( ) ;
t = f l oat . Par se( t 1) ;

Consol e. Wr i t e( " Ent er t he i ni t i al vel oci t y ( u) : " ) ;
u1 = Consol e. ReadLi ne( ) ;
u = f l oat . Par se( u1) ;

Consol e. Wr i t e( " Ent er t he Accel er at i on ( a) : " ) ;
a1 = Consol e. ReadLi ne( ) ;
a = f l oat . Par se( a1) ;

di st ance = u*t + a*t *t / 2;

Consol e. Wr i t eLi ne( " Di st ance t r avel l ed by t he vehi cl e = " + di st ance) ;

Consol e. Wr i t eLi ne( " " ) ; / / Bl ank Li ne






Consol e. Wr i t e( " Do you want t o check f or anot her val ues ( 1 f or Yes / 0 t o Exi t )
? : " ) ;

r epl y = Consol e. ReadLi ne( ) ;

r epl yf or r est ar t = i nt . Par se( r epl y) ;

i f ( r epl yf or r est ar t == 1)
{
count er = count er + 1;
Consol e. Wr i t eLi ne( " " ) ; / / Bl ank Li ne
Consol e. Wr i t eLi ne( "
************************************************************************ " ) ;
got o st ar t f r omher e;
}
el se
{
/ / Do not hi ng . . . Si mpl y pr ogr amexi t s
}


}

 
Output: 
******** Thi s wi l l cal cul at e t he di st ance t r avel l ed by a vehi cl e ********** 
Set of val ue = 1
Ent er t he t i me i nt er val ( t ) : 15
Ent er t he i ni t i al vel oci t y ( u) : 10
Ent er t he Accel er at i on ( a) : 150
Di st ance t r avel l ed by t he vehi cl e = 17025
Do you want t o check f or anot her val ues ( 1 f or Yes / 0 t o Exi t ) ? : 1
************************************************************************ 
Set of val ue = 2
Ent er t he t i me i nt er val ( t ) : 25
Ent er t he i ni t i al vel oci t y ( u) : 5
Ent er t he Accel er at i on ( a) : 540
Di st ance t r avel l ed by t he vehi cl e = 168875
Do you want t o check f or anot her val ues ( 1 f or Yes / 0 t o Exi t ) ? : 0



5. 11#Finding the EOQ(Economic Order Quantity) & TBO(Time between Orders) 

usi ng Syst em;

cl ass I nvent or yManagement
{
publ i c st at i c voi d Mai n( )
{
f l oat dr , sc, cpu;
/ / dr = Demand r at e, sc = set up cost s, cpu = cost per uni t

doubl e EOQ, TBO;
/ / EOQ = Economi c Or der Quai t i t y
/ / TBQ = Opt i mal Ti me Bet ween or der s

Consol e. Wr i t eLi ne( " \ t \ t ***** I nvent or y Management Syst em*****" ) ;

Consol e. Wr i t eLi ne( " " ) ; / / Bl ank Li ne

Consol e. Wr i t e( " Ent er t he Demand Rat e : " ) ;
dr = f l oat . Par se( Consol e. ReadLi ne( ) ) ;

Consol e. Wr i t e( " Ent er t he Set up Cost s : " ) ;
sc = f l oat . Par se( Consol e. ReadLi ne( ) ) ;

Consol e. Wr i t e( " Ent er t he Cost Per Uni t : " ) ;
cpu = f l oat . Par se( Consol e. ReadLi ne( ) ) ;

Consol e. Wr i t eLi ne( " " ) ; / / Bl ank Li ne

EOQ = Mat h. Sqr t ( 2*dr *sc/ cpu) ; / / Cal cul at i ng EOQ

TBO = Mat h. Sqr t ( 2*sc/ ( dr *cpu) ) ; / / Cal cul at i ng TBO

Consol e. Wr i t eLi ne( " Economi c Or der Quai t i t y = " +EOQ) ;
Consol e. Wr i t eLi ne( " Opt i mal Ti me Bet ween or der s = " +TBO) ;

Consol e. ReadLi ne( ) ;
}
}
 
Output: 
Ent er t he Demand Rat e : 150
Ent er t he Set up Cost s : 250
Ent er t he Cost Per Uni t : 25

Economi c Or der Quai t i t y = 54. 772255
Opt i mal Ti me Bet ween or der s = 0. 3654837167 
 
 
5. 12 # Finding the frequencies for a range of different capacitance. 

usi ng Syst em;

cl ass El ect r i cal Ci r cui t
{
publ i c st at i c voi d Mai n( )
{
f l oat L, R, C, Fr equency;

/ / L = I nduct ance
/ / R = Resi st ance
/ / C = Capaci t ance

/ / doubl e Fr equency;

Consol e. Wr i t eLi ne( " ****** Cal cul at i ng f r equenci es f or di f f er ent val ues of
Capaci t ance ******" ) ;

Consol e. Wr i t eLi ne( " " ) ; / / Bl ank Li ne

Consol e. Wr i t e( " Ent er t he I nduct ance ( L) : " ) ;
L = f l oat . Par se( Consol e. ReadLi ne( ) ) ;

Consol e. Wr i t e( " Ent er t he Resi st ance ( R) : " ) ;
R = f l oat . Par se( Consol e. ReadLi ne( ) ) ;

Consol e. Wr i t eLi ne( " " ) ; / / Bl ank Li ne

f or ( C = 0. 01F; C <= 0. 1; C = C + 0. 01F)
{

Fr equency = ( f l oat ) ( Mat h. Sqr t ( ( 1/ L*C) - ( ( R*R) / ( 4*C*C) ) ) ) ;
Consol e. Wr i t eLi ne( " For Capaci t ance " + C + " , The Fr equency = " +
Fr equency) ;
}

Consol e. ReadLi ne( ) ;
}
}











Out put :
****** Cal cul at i ng f r equenci es f or di f f er ent val ues of Capaci t ance ******
Ent er t he I nduct ance ( L) : 0. 00004
Ent er t he Resi st ance ( R) : 0. 00008

For Capaci t ance 0. 01, The Fr equency = 15. 81139
For Capaci t ance 0. 02, The Fr equency = 22. 36068
For Capaci t ance 0. 03, The Fr equency = 27. 38613
For Capaci t ance 0. 04, The Fr equency = 31. 62278
For Capaci t ance 0. 05, The Fr equency = 35. 35534
For Capaci t ance 0. 06, The Fr equency = 38. 72983
For Capaci t ance 0. 07, The Fr equency = 41. 833
For Capaci t ance 0. 08, The Fr equency = 44. 72136
For Capaci t ance 0. 09, The Fr equency = 47. 43416
For Capaci t ance 0. 1, The Fr equency = 50






















Chp - 6
(Decision Making & Branching)





 
6.1 # Adding odd & even nos from 0 – 20 & adding nos. divisible by 7 between 100 – 200 
usi ng Syst em;

cl ass SumOf Odds
{
publ i c st at i c voi d Mai n( )
{
i nt x=0, sumodd=0, sumeven=0, sumdi v7 = 0 , t ot al no7 = 0, i ;
/ / her e . . .
/ / " sumodd" wi l l cont ai n sumof al l odd t he number s f r om1 - 20
/ / " sumeven" wi l l cont ai n sumof al l even t he number s f r om1 - 20
/ / " sumdi v7" wi l l cont ai n t he sumof al l number s f r om100 - 200 di vi si bl e by 7
/ / " t ot al no7" wi l l cont ai n t he t ot al no. of al l number s f r om100 - 200
di vi si bl e by 7
/ / " i " i s a var i abl e used i n l oops
/ / " x" i s a t empor ar y var i abl e whi ch check f or t he condi t i ons i mposed on i t


/ / checki ng f or t he odd & even number s
f or ( i =0 ; i <=20 ; i ++)
{
x = i %2;
i f ( x ! = 0)
{
sumodd = sumodd + i ;
}
i f ( x == 0)
{
sumeven = sumeven + i ;
}
}



/ / checki ng f or t he sum& no. of number s di vi si bl e by 7

x = 0; / / r eset t i ng t he val ue of ' x'
f or ( i =100; i <=200; i ++)
{
x = i %7;
i f ( x == 0)
{
sumdi v7 = sumdi v7 + i ;
t ot al no7 = t ot al no7 + 1;
}
}



Consol e. Wr i t eLi ne( " Sumof al l odd number s f r om1 - 20 = " + sumodd + " \ n" ) ;
Consol e. Wr i t eLi ne( " Sumof al l even number s f r om1 - 20 = " + sumeven + " \ n" ) ;
Consol e. Wr i t eLi ne( " Sumof al l number s f r om100 - 200, di vi si bl e by 7 = " +
sumdi v7 + " \ n" ) ;
Consol e. Wr i t eLi ne( " Tot al number s f r om100 - 200, di vi si bl e by 7 = " +
t ot al no7 + " \ n" ) ;

Consol e. ReadLi ne( ) ;
}
}


Out put :
Sumof al l odd number s f r om1 - 20 = 100
Sumof al l even number s f r om1 - 20 = 110
Sumof al l number s f r om100 - 200, di vi si bl e by 7 = 2107
Tot al number s f r om100 - 200, di vi si bl e by 7 = 14



























6. 2 # Fi ndi ng Sol ut i on of l i near equat i ons

usi ng Syst em;

cl ass Li near Equat i ons
{
publ i c st at i c voi d Mai n( )
{
i nt r esponse;
f l oat a, b, c, d, m, n, t emp;
doubl e x1, x2;

Ent er NewVal uesAgai n:

Consol e. Wr i t eLi ne( " " ) ; / / Bl ank Li ne
Consol e. Wr i t eLi ne( " ********************** Li near Equat i on
********************* " ) ;
Consol e. Wr i t eLi ne( " " ) ; / / Bl ank Li ne
/ / Readi ng t he val ue of a
Consol e. Wr i t e( " Ent er t he val ue of a : " ) ;
a = f l oat . Par se( Consol e. ReadLi ne( ) ) ;

/ / Readi ng t he val ue of b
Consol e. Wr i t e( " Ent er t he val ue of b : " ) ;
b = f l oat . Par se( Consol e. ReadLi ne( ) ) ;

/ / Readi ng t he val ue of c
Consol e. Wr i t e( " Ent er t he val ue of c : " ) ;
c = f l oat . Par se( Consol e. ReadLi ne( ) ) ;

/ / Readi ng t he val ue of d
Consol e. Wr i t e( " Ent er t he val ue of d : " ) ;
d = f l oat . Par se( Consol e. ReadLi ne( ) ) ;


t emp = a*d - b*c;

i f ( t emp == 0)

{
Consol e. Wr i t eLi ne( " " ) ; / / Bl ank Li ne
Consol e. Wr i t eLi ne( " The denomi nat or equal s t o zer o ( 0) ; Cannot pr oceed
f ur t her . . . " ) ;
Consol e. Wr i t e( " Do You want t o ent er new val ues ( 1 For Yes / 0 For No) ?
" ) ;
r esponse = i nt . Par se( Consol e. ReadLi ne( ) ) ;

i f ( r esponse == 0)
{
got o Exi t ;
}
el se
{
got o Ent er NewVal uesAgai n;
}
}

el se

{
/ / Readi ng t he val ue of m
Consol e. Wr i t e( " Ent er t he val ue of m: " ) ;
m= f l oat . Par se( Consol e. ReadLi ne( ) ) ;

/ / Readi ng t he val ue of n
Consol e. Wr i t e( " Ent er t he val ue of n : " ) ;
n = f l oat . Par se( Consol e. ReadLi ne( ) ) ;

x1 = ( ( m*d) + ( b*n) ) / ( ( a*d) - ( c*b) ) ;
x2 = ( ( n*a) + ( m*c) ) / ( ( a*d) - ( c*b) ) ;

Consol e. Wr i t eLi ne( " " ) ; / / Bl ank Li ne

Consol e. Wr i t eLi ne( " Val ue of x1 = " + x1) ;
Consol e. Wr i t eLi ne( " Val ue of x2 = " + x2) ;

Consol e. Wr i t eLi ne( " " ) ; / / Bl ank Li ne

Consol e. Wr i t e( " Do You want t o ent er new val ues ( 1 For Yes / 0 For No) ?
" ) ;
r esponse = i nt . Par se( Consol e. ReadLi ne( ) ) ;
i f ( r esponse == 0)
{
got o Exi t ;
}
el se
{
got o Ent er NewVal uesAgai n;
}

}

Exi t :
Consol e. Wr i t eLi ne( " " ) ; / / Bl ank Li ne
Consol e. Wr i t eLi ne( " Thank You For usi ng t hi s smal l pr ogr am. . . : ) " ) ;
Consol e. ReadLi ne( ) ;

}
}














Out put :
********************** Li near Equat i on *********************
Ent er t he val ue of a : 5
Ent er t he val ue of b : 5
Ent er t he val ue of c : 5
Ent er t he val ue of d : 5
The denomi nat or equal s t o zer o ( 0) ; Cannot pr oceed f ur t her . . .
Do You want t o ent er new val ues ( 1 For Yes / 0 For No) ? 1
********************** Li near Equat i on *********************
Ent er t he val ue of a : 15
Ent er t he val ue of b : 5
Ent er t he val ue of c : 3
Ent er t he val ue of d : 20
Ent er t he val ue of m: 5
Ent er t he val ue of n : 6

Val ue of x1 = 0. 4561
Val ue of x2 = 0. 364821
Do You want t o ent er new val ues ( 1 For Yes / 0 For No) ? 0

You For usi ng t hi s smal l pr ogr am. . . : )











6.5 # Computing marks of students 

usi ng Syst em;

cl ass Mar ksRange
{
publ i c st at i c voi d Mai n( )
{
i nt i , count 80 = 0, count 60 = 0, count 40 = 0, count 0 = 0;
f l oat [ ] mar ks =
{57. 5F, 45. 9F, 98. 01F, 56. 4F, 46. 5F, 80, 82, 67, 76, 49, 91, 55, 78, 79, 19. 5F, 25. 8F, 35, 36, 35, 28, 25. 8F, 4
6, 55, 59, 68, 97, 85, 48. 5F, 67, 84};

f or ( i = 0; i <=29; i ++)

{
i f ( mar ks[ i ] > 80 && mar ks [ i ] < 101)
{
count 80 = count 80 + 1;
}
el se i f ( mar ks [ i ] > 60 && mar ks[ i ] < 81)
{
count 60 = count 60 + 1;
}
el se i f ( mar ks [ i ] > 40 && mar ks[ i ] < 61)
{
count 40 = count 40 + 1;
}
el se
{
count 0 = count 0 + 1;
}
}

Consol e. Wr i t eLi ne( " St udent s i n t he r ange of 81 - 100 : " + count 80) ;
Consol e. Wr i t eLi ne( " St udent s i n t he r ange of 61 - 80 : " + count 60) ;
Consol e. Wr i t eLi ne( " St udent s i n t he r ange of 41 - 60 : " + count 40) ;
Consol e. Wr i t eLi ne( " St udent s i n t he r ange of 0 - 40 : " + count 0) ;

Consol e. ReadLi ne( ) ;

}
}

Out put :
St udent s i n t he r ange of 81 - 100 : 6
St udent s i n t he r ange of 61 - 80 : 7
St udent s i n t he r ange of 41 - 60 : 10
St udent s i n t he r ange of 0 - 40 : 7



6. 7 # Selecting students on the basis of some given criteria on marks 
usi ng Syst em;

cl ass Admi ssi on
{
publ i c st at i c voi d Mai n( )
{
f l oat mksMat hs, mksPhysi cs, mksChemi st r y, mksTot al , Mat hsPhysi cs;
i nt r esponse;

begi nni ng:

Consol e. Wr i t eLi ne( " " ) ; / / Bl ank Li ne

Consol e. Wr i t eLi ne( " ********** St udent s Enr ol l ment Checki ng Cr i t er i a
********** " ) ;

Consol e. Wr i t eLi ne( " " ) ; / / Bl ank Li ne

Consol e. Wr i t e( " Ent er t he mar ks i n Mat hs : " ) ;
mksMat hs = f l oat . Par se( Consol e. ReadLi ne( ) ) ;

Consol e. Wr i t e( " Ent er t he mar ks i n Chemi st r y : " ) ;
mksChemi st r y = f l oat . Par se( Consol e. ReadLi ne( ) ) ;

Consol e. Wr i t e( " Ent er t he mar ks i n Physi cs : " ) ;
mksPhysi cs = f l oat . Par se( Consol e. ReadLi ne( ) ) ;

mksTot al = ( f l oat ) ( mksMat hs + mksChemi st r y + mksPhysi cs) ;

Mat hsPhysi cs = ( f l oat ) ( mksMat hs + mksPhysi cs) ;

i f ( ( mksMat hs >= 60 && mksPhysi cs >= 50 && mksChemi st r y >= 40) | | ( mksTot al >=
200 | | ( mksMat hs + mksPhysi cs) >= 150) )
{
Consol e. Wr i t eLi ne( " Congr at ul at i ons ! ! ! The candi dat e i s sel ect ed . . . " ) ;
}
el se
{
Consol e. Wr i t eLi ne( " Sor r y, The candi dat e i s r ej ect ed . . . Bet t er l uck f or
next year . " ) ;
}

Consol e. Wr i t eLi ne( " " ) ; / / Bl ank Li ne

Consol e. Wr i t e( " Ent er 1 f or next candi dat e, 0 t o exi t : " ) ;
r esponse = i nt . Par se( Consol e. ReadLi ne( ) ) ;

i f ( r esponse == 1)
got o begi nni ng;
el se
got o end;

end:
Consol e. ReadLi ne( ) ;
}
}



Out put :

********** St udent s Enr ol l ment Checki ng Cr i t er i a **********

Ent er t he mar ks i n Mat hs : 50
Ent er t he mar ks i n Chemi st r y : 40
Ent er t he mar ks i n Physi cs : 35
Sor r y, The candi dat e i s r ej ect ed . . . Bet t er l uck f or next year .
Ent er 1 f or next candi dat e, 0 t o exi t : 1

********** St udent s Enr ol l ment Checki ng Cr i t er i a **********

Ent er t he mar ks i n Mat hs : 70
Ent er t he mar ks i n Chemi st r y : 80
Ent er t he mar ks i n Physi cs : 85
Congr at ul at i ons ! ! ! The candi dat e i s sel ect ed . . .
Ent er 1 f or next candi dat e, 0 t o exi t : 0


 











6. 8 # Fl oyd’ s Tr i angl e
usi ng Syst em;

cl ass Fl oydsTr i angl e1
{
publ i c st at i c voi d Mai n( )
{
i nt i , j , k=1;
Consol e. Wr i t eLi ne( " ************ Fl oyd' s Tr i angl e - Nor mal Numer i c Mode
**************** " ) ;

f or ( i =1; i <=13; i ++)
{ / / 13 i s t he hei ght of t he t r i angl e
f or ( j =1; j <i +1; j ++)
{ / / each t i me t he number per l i ne i s i ncr ement ed by 1
Consol e. Wr i t e( k++ + " " ) ; / / k i s t he act ual dat a ( number ) whi ch
wi l l be pr i nt ed.
}
Consol e. Wr i t e( " \ n" ) ; / / t hen we go t o t he next l i ne.
}

Consol e. ReadLi ne( ) ;

}
}

Out put :
************ Fl oyd' s Tr i angl e - Nor mal Numer i c Mode ****************

1
2 3
4 5 6
7 8 9 10
11 12 13 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 86 87 88 89 90 91

6.9 # Computing seasonal discount of a showroom 
usi ng Syst em;

cl ass Seasonal Di scount
{
publ i c st at i c voi d Mai n( )
{
i nt amt ;
f l oat Mi l l _di sc, Hand_di sc, Di scount edAmt ;

Consol e. Wr i t eLi ne( " ************ Seasonal Di scount of a Mal l
************** " ) ;

Consol e. Wr i t e( " Ent er t he Pur chase amount : " ) ;
amt = i nt . Par se( Consol e. ReadLi ne( ) ) ;


i f ( amt >= 0 && amt <= 100)
{
Mi l l _di sc = amt * 0. 0F;
Hand_di sc = amt * 0. 05F;

Consol e. Wr i t eLi ne( " \ n\ n You Made a pur chase of : " +amt + " Rs. " ) ;
Consol e. Wr i t eLi ne( " \ nYou ar e el i gi bl e t o r eci eve a di scount of : \ n" +
( amt - Mi l l _di sc) + " Rs. = ( 0%) on Mi l l Cl ot h & \ n" + ( amt - Hand_di sc) + " Rs. = ( 5%) on
HandLoomI t ems. " ) ;
Di scount edAmt = amt - ( Mi l l _di sc + Hand_di sc) ;
Consol e. Wr i t eLi ne( " \ nAf t er al l t he di scount s, you need t o pay a sumof "
+ Di scount edAmt + " i nst ead of " + amt + " , \ nt hus maki ng a Pr of i t of " + ( Mi l l _di sc +
Hand_di sc) + " Rs. " ) ;

}

el se i f ( amt >= 101 && amt <= 200)
{
Mi l l _di sc = amt * 0. 05F;
Hand_di sc = amt * 0. 75F;

Consol e. Wr i t eLi ne( " \ n\ n You Made a pur chase of : " +amt + " Rs. " ) ;
Consol e. Wr i t eLi ne( " \ nYou ar e el i gi bl e t o r eci eve a di scount of : \ n" +
( amt - Mi l l _di sc) + " Rs. = ( 5%) on Mi l l Cl ot h & \ n" + ( amt - Hand_di sc) + " Rs. = ( 7. 5%)
on HandLoomI t ems. " ) ;
Di scount edAmt = amt - ( Mi l l _di sc + Hand_di sc) ;
Consol e. Wr i t eLi ne( " \ nAf t er al l t he di scount s, you need t o pay a sumof "
+ Di scount edAmt + " i nst ead of " + amt + " , \ nt hus maki ng a Pr of i t of " + ( Mi l l _di sc +
Hand_di sc) + " Rs. " ) ;

}
el se i f ( amt >= 201 && amt <= 300)
{
Mi l l _di sc = amt * 0. 75F;
Hand_di sc = amt * 0. 1F;

Consol e. Wr i t eLi ne( " \ n\ n You Made a pur chase of : " +amt + " Rs. " ) ;
Consol e. Wr i t eLi ne( " \ nYou ar e el i gi bl e t o r eci eve a di scount of : \ n" +
( amt - Mi l l _di sc) + " Rs. = ( 7. 5%) on Mi l l Cl ot h & \ n" + ( amt - Hand_di sc) + " Rs. = ( 10%)
on HandLoomI t ems. " ) ;
Di scount edAmt = amt - ( Mi l l _di sc + Hand_di sc) ;
Consol e. Wr i t eLi ne( " \ nAf t er al l t he di scount s, you need t o pay a sumof "
+ Di scount edAmt + " i nst ead of " + amt + " , \ nt hus maki ng a Pr of i t of " + ( Mi l l _di sc +
Hand_di sc) + " Rs. " ) ;

}
el se i f ( amt > 300)
{
Mi l l _di sc = amt * 0. 1F;
Hand_di sc = amt * 0. 15F;

Consol e. Wr i t eLi ne( " \ n\ n You Made a pur chase of : " +amt + " Rs. " ) ;
Consol e. Wr i t eLi ne( " \ nYou ar e el i gi bl e t o r eci eve a di scount of : \ n" +
( amt - Mi l l _di sc) + " Rs. = ( 10%) on Mi l l Cl ot h & \ n" + ( amt - Hand_di sc) + " Rs. = ( 15%)
on HandLoomI t ems. " ) ;
Di scount edAmt = amt - ( Mi l l _di sc + Hand_di sc) ;
Consol e. Wr i t eLi ne( " \ nAf t er al l t he di scount s, you need t o pay a sumof "
+ Di scount edAmt + " i nst ead of " + amt + " , \ nt hus maki ng a Pr of i t of " + ( Mi l l _di sc +
Hand_di sc) + " Rs. " ) ;

}

Consol e. ReadLi ne( ) ;
}
}

Out put :
*************** Seasonal Di scount of a Mal l **************
Ent er t he Pur chase amount : 250

You Made a pur chase of : Rs. 100
You ar e el i gi bl e t o r ecei ve a di scount of :
0 Rs. ( 0%) on Mi l l I t ems
5Rs. ( 5 %) on Handl oomI t ems
nAf t er al l t he di scount s, you need t o pay a sumof 95, i nst ead of 100, t hus maki ng a
pr of i t of 5Rs.











6. 10 # Reading ‘x’, Correspondingly Printing ‘y’ 

usi ng Syst em;

cl ass Changi ngVal uesOf Y
{
publ i c st at i c voi d Mai n( )
{
i nt x, y;

Consol e. Wr i t e( " Ent er t he val ue of x : " ) ;
x = i nt . Par se( Consol e. ReadLi ne( ) ) ;

Consol e. Wr i t eLi ne( " " ) ; / / Bl ank Li ne
Consol e. Wr i t eLi ne( " " ) ; / / Bl ank Li ne

Consol e. Wr i t eLi ne( " ********* Changi ng val ues of Y by nest ed i f
st at ement s *********" ) ;

Consol e. Wr i t eLi ne( " " ) ; / / Bl ank Li ne

i f ( x ! = 0)
{
i f ( x > 0)
{
Consol e. Wr i t eLi ne( " Y = 1" ) ;
}

i f ( x < 0)
{
Consol e. Wr i t eLi ne( " Y = - 1" ) ;
}

}
el se
{
Consol e. Wr i t eLi ne( " Y = 0" ) ;
}


Consol e. Wr i t eLi ne( " " ) ; / / Bl ank Li ne

Consol e. Wr i t eLi ne( " ********* Changi ng val ues of Y by el se i f
st at ement s *********" ) ;

Consol e. Wr i t eLi ne( " " ) ; / / Bl ank Li ne


i f ( x == 0)
{
Consol e. Wr i t eLi ne( " Y = 0" ) ;
}
el se i f ( x > 0)
{
Consol e. Wr i t eLi ne( " Y = 1" ) ;
}
el se
{
Consol e. Wr i t eLi ne( " Y = - 1" ) ;
}



Consol e. Wr i t eLi ne( " " ) ; / / Bl ank Li ne

Consol e. Wr i t eLi ne( " ********* Changi ng val ues of Y by condi t i onal
oper at or *********" ) ;

Consol e. Wr i t eLi ne( " " ) ; / / Bl ank Li ne

y = ( x ! = 0) ?( ( x>0) ?1: - 1) : 0;

Consol e. Wr i t eLi ne( " Y = " +y) ;

Consol e. ReadLi ne( ) ;


}
}


Out put :
Ent er t he val ue of x : 5

********* Changi ng val ues of Y by nest ed i f st at ement s *********
Y = 1
********* Changi ng val ues of Y by el se i f st at ement s *********
Y = 1
********* Changi ng val ues of Y by condi t i onal oper at or *********
Y = 1












CHAPTER # 7
Sr.no Topic Date Sign
1. Reversing the numbers 13/02/2007
2. Finding the factorial of a given number 13/02/2007
3. Calculating the sum of digits of the given number 13/02/2007
4. Printing & adding Fibonacci series 13/02/2007
5. Investment Equation 13/02/2007
6. Converting $ into Rs. 13/02/2007
7. Demonstrating use of break, continue & goto 13/02/2007

INDEX FOR CHP 8, 9 & 10

Sr.no Topic Date Sign
1.
Printing triangles into various formats
27/2/07

2.
Calculate standard deviation & mean of the
array elements
27/2/07

3.
Finding the maximum & minimum of 3
numbers entered
27/2/07

4.
Finding largest array element & average of
array elements via methods
27/2/07

5.
Sorting 2 arrays & merging into 1
27/2/07

6.
Accepting a list of 5 items
27/2/07

7.
Counting number of words in a string
27/2/07

8.
Reversing array by creating a method
‘Reverse’
27/2/07

9.
Read an array & sort it
27/2/07


7.1 # Reversing the numbers 
usi ng Syst em;

cl ass Rever seNumber
{
publ i c st at i c voi d Mai n( )
{
i nt num, r em, i , count er =0, t emp;
/ / num: Cont ai ns t he act ual number i nput t ed vi a t he consol e
/ / r em: r emai nder of t he number ' num' when di vi ded by 10
/ / i : l oop var i abl e
/ / count er : det er mi nes t he no. of di gi t s i n t he i nput t ed number ' num'
/ / t emp : t empor ar y var i abl e used t o save t he val ue of ' num' ( Expl ai ned
f ur t her )

Consol e. Wr i t e( " Ent er an i nt eger number ( Not mor e t han 9 di gi t s) : " ) ;
num= i nt . Par se( Consol e. ReadLi ne( ) ) ;

t emp = num;
/ / Her e we ar e savi ng ' num' i n ' t emp' coz i t s val ue af t er det er mi ni ng t he no.
of di gi t s wi l l l oose.
/ / So af t er i t s wor k i s done, ' num' wi l l cont ai n val ue = 0
/ / The val ue of ' num' i s r eset t ed t o i t s or i gi nal val ue l at er f r om' t emp'
var i abl e


/ / ************** Det er mi ne t he no. of di gi t s i n t he i nput t ed number
**********************
whi l e( num> 0)
{
r em= num%10;
num= num/ 10;
i f ( num<= 0)
{
br eak;
}
el se
{
count er = count er + 1;
}
}

Consol e. Wr i t eLi ne( " Number of di gi t s ar e = " + ( count er +1) ) ;


Consol e. Wr i t e( " The r ever sed di gi t s ar e : " ) ;

r em= 0;
/ / r eset t i ng t he val ue of r emai nder ' r em'

num= t emp;
/ / r eset t i ng t he l ost val ue of ' num' f r om' t emp'


/ / ************** Det er mi ne t he r ever sed of i nput t ed di gi t s
**********************
/ / Funda :
/ / 1) Di vi de t he number by 10 & det er mi ne t he r emai nder . ( Save t he r emai nder
i n ' r em' )



/ / Thi s wi l l gi ve us t he l ast di gi t i n t he act ual i nput t ed number .
/ /
/ / 2) Wr i t e t he number so obt ai ned i nt o t he consol e
/ /
/ / 3) Di vi de t he same number by 10 & get t he quot i ent t hi s t i me.
/ / Si nce di vi si on i s bet ween t he i nt eger s, we wi l l get t he new number ,
depr i ved of t he l ast di gi t .
/ / Then agai n got o st ep 1) & cont i nue unt i l & unl ess t he count er i s equal t o
' i ' ( coz t hat s t he l oop var i bal e)

f or ( i = 0; i <=count er ; i ++)
{
r em= num%10;
Consol e. Wr i t e( r em) ;
num= num/ 10;
}

Consol e. ReadLi ne( ) ;

}
}


Out put :
Ent er an i nt eger number ( Not mor e t han 9 di gi t s) : 3547786
Number of di gi t s ar e = 7
The r ever sed di gi t s ar e : 6877453















7.2 # Finding the factorial of a given number 
usi ng Syst em;

cl ass Fact or i al
{
publ i c st at i c voi d Mai n( )
{
i nt no, i , f act =1;
Consol e. Wr i t e( " Ent er a number t o f i nd i t s f act or i al : " ) ;
no = i nt . Par se( Consol e. ReadLi ne( ) ) ;

i f ( no ! = 0)
{
f or ( i = no; i >=1; i - - )
{
f act = f act * i ;
}
Consol e. Wr i t eLi ne( " Fact or i al = " +f act ) ;
}
el se
{
Consol e. Wr i t eLi ne( " You ent er ed 0, not val i d. " ) ;
}

Consol e. ReadLi ne( ) ;
}
}

Out put :
Ent er a number t o f i nd i t s f act or i al : 9
Fact or i al = 362880













7.3 #Calculating the sum of digits of the given number 
usi ng Syst em;

cl ass SumOf Number s
{
publ i c st at i c voi d Mai n( )
{
i nt num, r em, i , count er =0, t emp, sum=0;
/ / num: Cont ai ns t he act ual number i nput t ed vi a t he consol e
/ / r em: r emai nder of t he number ' num' when di vi ded by 10
/ / i : l oop var i abl e
/ / count er : det er mi nes t he no. of di gi t s i n t he i nput t ed number ' num'
/ / t emp : t empor ar y var i abl e used t o save t he val ue of ' num' ( Expl ai ned
f ur t her )

Consol e. Wr i t e( " Ent er an i nt eger number ( Not mor e t han 9 di gi t s) : " ) ;
num= i nt . Par se( Consol e. ReadLi ne( ) ) ;

t emp = num;
/ / Her e we ar e savi ng ' num' i n ' t emp' coz i t s val ue af t er det er mi ni ng t he no.
of di gi t s wi l l l oose.
/ / So af t er i t s wor k i s done, ' num' wi l l cont ai n val ue = 0
/ / The val ue of ' num' i s r eset t ed t o i t s or i gi nal val ue l at er f r om' t emp'
var i abl e


/ / ************** Det er mi ne t he no. of di gi t s i n t he i nput t ed number
**********************
whi l e( num> 0)
{
r em= num%10;
num= num/ 10;
i f ( num<= 0)
{
br eak;
}
el se
{
count er = count er + 1;
}
}

Consol e. Wr i t eLi ne( " Number of di gi t s ar e = " + ( count er +1) ) ;


r em= 0;
/ / r eset t i ng t he val ue of r emai nder ' r em'

num= t emp;
/ / r eset t i ng t he l ost val ue of ' num' f r om' t emp'


/ / ************** Det er mi ne t he r ever sed of i nput t ed di gi t s
**********************
/ / Funda :
/ / 1) Di vi de t he number by 10 & det er mi ne t he r emai nder . ( Save t he r emai nder
i n ' r em' )
/ / Thi s wi l l gi ve us t he l ast di gi t i n t he act ual i nput t ed number . ( Same as
r ever si ng number s l ogi c)
/ /


/ / 2) Add t he number so obt ai ned i nt o t he var i abl e ' sum'
/ /
/ / 3) Di vi de t he same number by 10 & get t he quot i ent t hi s t i me.
/ / Si nce di vi si on i s bet ween t he i nt eger s, we wi l l get t he new number ,
depr i ved of t he l ast di gi t .
/ / Then agai n got o st ep 1) & cont i nue unt i l & unl ess t he count er i s equal t o
' i ' ( coz t hat s t he l oop var i bal e)

f or ( i = 0; i <=count er ; i ++)
{
r em= num%10;
sum= sum+ r em;
num= num/ 10;
}
Consol e. Wr i t eLi ne( " Sum= " +sum) ;
Consol e. ReadLi ne( ) ;

}
}


Out put :
Ent er an i nt eger number ( Not mor e t han 9 di gi t s) : 65478457
Number of di gi t s : 8
Sumof di gi t s : 46















7.4 # Printing & adding Fibonacci series 
usi ng Syst em;

cl ass Fi bonacci
{
publ i c st at i c voi d Mai n( )
{
i nt f i r st = 1, second = 1, t hi r d, no, count = 0;
l ong sum= 2;
/ / ' f i r st ' , ' second' , ' t hi r d' ar e t he f i r st , second & t hi r d number s i n t he
f i bonacci ser i es
/ / ' f i r st ' & ' second' ar e bot h i ni t i al i sed t o 1
/ / sumof ' f i r st ' & ' second' ar e added t o t he ' t hi r d' var i abl e
/ / ' sum' wi l l cont ai n t he sumof al l t he di gi t s i n t he f i bonacci ser i es. I t i s
i ni t i al i es t o 2 coz sumof f i r st 2 di gi t s i s 2
/ / ' no' i s t he number i nput t ed f r omt he consol e up t i l l whi ch t he f i bonacci
ser i es i s di spl ayed
/ / ' count ' count s t he number of di gi t s i n t he f i bonacci ser i es

Consol e. Wr i t e( " Ent er t he number upt i l l whi ch you want t he f i bonacci number s :
" ) ;
no = i nt . Par se( Consol e. ReadLi ne( ) ) ;

i f ( no >= 45)
{
/ / checki ng f or val ues out of r ange.
Consol e. Wr i t eLi ne( " Out of r ange val ues. Dont ent er mor e t han 45. " ) ;
got o exi t ;
}

Consol e. Wr i t e( " Fi bonacci Ser i es : 1 1" ) ;
/ / I ni t i al 2 number s of t he f i bonacci ser i es ar e j ust ' 1' & ' 1' , t hus wr i t i ng
i t di r ect l y

do
{
t hi r d = f i r st + second;
/ / addi ng ' t hi r d' = ' f i r st ' + ' second'

Consol e. Wr i t e( " " +t hi r d) ;
/ / di spl ay t he ' t hi r d' di gi t i n t he ser i es

f i r st = second;
/ / make ' f i r st ' di gi t , t he ' second' one
second = t hi r d;
/ / make ' second' di gi t , t he ' t hi r d' one

/ / we di d t hi s coz i n f i bonacci ser i es, each di gi t i s a sumof pr evi ous
2 di gi t s


count = count + 1;
/ / i ncr ement t he count er

sum= sum+ t hi r d;
/ / add t he sumi n t he ' sum' var i abl e f r om' t hi r d' var i abl e

}
whi l e( ( count + 3) <= no) ;
/ / we ent er ed t he ' no' f r omt he consol e & al so t he f i r st 2 di gi t s ar e not f r om
t hi s l oop
/ / t hus we added +3 her e t o t he ' count ' var i abl e so t hat we get t he exact
speci f i ed no. of di gi t s.
/ / i f we di dnt added 3, t hen t he ser i es wi l l go beyond t he speci f i ed number of
di gi t s f r omt he consol e vi a ' no'


Consol e. Wr i t eLi ne( " \ nSumof al l f i bonacci di gi t s : " +sum) ;
/ / Di spl ay t he sum
exi t :
Consol e. ReadLi ne( ) ;
}
}



Out put :
Ent er t he number upt i l l whi ch you want t he f i bonacci number s : 8
Fi bonacci Ser i es : 1 1 2 3 5 8 13 21 34
Sumof al l Fi bonacci di gi t s : 88

 
 
 
 
 
 
 
 
 
 
 
 
7.5 #Investment Equation 
usi ng Syst em;

cl ass I nvest ment
{
publ i c st at i c voi d Mai n( )
{
i nt P=1000, n;
f l oat r =0. 1F;
doubl e V;

Consol e. Wr i t eLi ne( " ************** I nvest ement Opt i on of 10 yr s
***************** " ) ;

Consol e. Wr i t eLi ne( " " ) ; / / Bl ank Li ne



Consol e. Wr i t eLi ne( " Pr i nci pal ( P) Rat e( r ) Number Of Yr s( n) Val ue
Of Money( V) \ n" ) ;
Consol e. Wr i t eLi ne( " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - \ n" ) ;
V = P * ( 1 + r ) ;
f or ( n=1; n<=10; n++)
{
Consol e. Wr i t eLi ne ( " " + P + " " + r + "
" + n + " " + V) ;
P = P + 1000;
r = r + 0. 01F;
V = P * ( 1 + r ) ;
}

Consol e. ReadLi ne( ) ;
}
}

Out put :


 
 
7.7 # Converting $ into Rs. 

usi ng Syst em;

cl ass Dol l ar ToRupees
{
publ i c st at i c voi d Mai n( )
{
f l oat dol , r s, cur r ent ;
i nt i ;

Consol e. Wr i t e( " What i s t he cur r ent val ue of 1 $ as per I NDI AN Rs. : " ) ;
cur r ent = f l oat . Par se( Consol e. ReadLi ne( ) ) ;

Consol e. Wr i t eLi ne( " " ) ; / / Bl ank Li ne

f or ( i =1; i <=5; i ++)
{
Consol e. Wr i t e( " Ent er val ue " + i + " i n Dol l ar s : " ) ;
dol = f l oat . Par se( Consol e. ReadLi ne( ) ) ;
r s = dol * cur r ent ;
Consol e. Wr i t eLi ne( dol + " $ = " +r s + " Rs. " ) ;
Consol e. Wr i t eLi ne( " " ) ; / / Bl ank Li ne
}

Consol e. ReadLi ne( ) ;
}
}

Out put :





7.10 #Demonstrating use of break, continue & goto 
usi ng Syst em;

cl ass Br eakCont i uneGot o
{
publ i c st at i c voi d Mai n( )
{
i nt n = 10;
whi l e( n<200)
{
i f ( n<100)
{
i f ( n<50)
{
got o l esst han50;
}
Consol e. Wr i t e( " " +n) ;
n = n + 20;
cont i nue;
}

l esst han50:
{
Consol e. Wr i t e( " " +n) ;
n = n + 10;
cont i nue;
}

i f ( n==50)
{
Consol e. Wr i t eLi ne( " " ) ;
n = n + 10;
cont i nue;
}

i f ( n > 90)
br eak;
Consol e. Wr i t e( " " +n) ;
n = n + 10;
}
Consol e. Wr i t eLi ne( ) ;
Consol e. ReadLi ne( ) ;
}
}

Out put :
10 20 30 40 50 60 70 80 90 110 120 130 140 150 160 170 180 190




7.6 - PRINTING TRIANGLES INTO VARIOUS FORMATS
a)

usi ng Syst em;

cl ass Dol l ar Desi gn
{
publ i c st at i c voi d Mai n( )
{
i nt no=1, i , j ;

f or ( i = 1 ; i < 6 ; i ++) / / Out er l oop f or i ncr ememt i ng t he number s t o be
di spl ayed
{
Consol e. Wr i t eLi ne( " " ) ; / / Leave a l i ne af t er each new number
f or ( j = 1; j < 6; j ++) / / I nner l oop t o speci f y t he numer of t i mes t he
par t i cul ar number i s t o be pr i nt ed.
{
Consol e. Wr i t e( no) ;
i f ( i == j )
/ / I f a number i s pr i nt ed t hat many number of t i mes . . .
/ / e. g. I f 3 i s t her e. The i f 3 i s pr i nt ed 3 t i mes, t hen
t hi s condi t i on ar i ses
{
no = no + 1; / / I ncr ement t he number
got o l oop1; / / Got o out er l oop
}
}
l oop1: cont i nue;
}
Consol e. ReadLi ne( ) ;
}
}


Output:
 

22 
333 
4444 
55555 
 
 
 
 
 
 
 
b) 
 
usi ng Syst em;

cl ass Tr i angl eDol l ar
{
publ i c st at i c voi d Mai n( )
{
i nt i , j , k;
st r i ng d=" $" ;
f or ( i =1; i <=5; i ++)
{
f or ( k=1; k<=i ; k++)
Consol e. Wr i t e( " " ) ;
f or ( j =5; j >=i ; j - - )
{
Consol e. Wr i t e ( " $" , +j ) ; / / Ent er t he space wi t h a ' $' si gn
/ / Thi s i s anot her synt ax of Consol e. Wr i t e met hod. Her e t he di gi t af t er t he comma ‘ , ’
si gni f i es t he posi t i on of t he f i r st char act er ‘ $’ on t he out put scr een.
}

Consol e. Wr i t e( " \ n" ) ; / / t hen we go t o t he next l i ne.
}
Consol e. ReadLi ne( ) ;
}
}
 
 
Output: 
 
$$$$$ 
  $$$$ 
    $$$ 
      $$ 
        $ 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c) 
 

usi ng Syst em;
cl ass Pyr ami dNumber s
{
publ i c st at i c voi d Mai n( )
{
i nt i , j , num=5, k;
f or ( i =1; i <=num; i ++)
{
f or ( k=num; k>=i ; k- - ) / / Loop f or t he bl ank spaces
{
Consol e. Wr i t e( " " ) ;
}

f or ( j =1; j <=i ; j ++) / / Loop f or det er mi ni ng t he number of t i mes t he number
i s t o be wr i t t en
{
Consol e. Wr i t e( " " +i ) ; / / " " i s a space needed i n bet ween t he
number s
}

Consol e. Wr i t e( " \ n" ) ; / / Go t o t he next l i ne f or next number
}
Consol e. ReadLi ne( ) ;
}
}
 
 
 
Output: 
 

 2  2 
3  3  3 
4  4  4  4 
5  5  5  5  5 
 
 
 
 
 
 
 
 
 
 
 
 
8.6 – CALCULATE STANDARD DEVIATION & MEAN OF THE ARRAY
ELEMENTS

usi ng Syst em;

cl ass St dDevi at i on
{
publ i c st at i c voi d Mai n( )
{
f l oat [ ] nos = {3. 5F, 57, 2, 6, 24, 14, 95, 23, 74, 23};
i nt n = nos. Lengt h;
f l oat sum= 0. 0F, sumof sq = 0. 0F, mean;
doubl e devi at i on;

Consol e. Wr i t e( " Ar r ay Li st consi st s of : " ) ;

f or ( i nt i = 0; i < n; i ++)
{
Consol e. Wr i t e( nos[ i ] + " " ) ;
}


f or ( i nt i = 0; i < n; i ++)
{
sum= sum+ nos[ i ] ;
}

f or ( i nt i = 0; i < n; i ++)
{
sumof sq = sumof sq + ( nos[ i ] *nos[ i ] ) ;
}

mean = sum/ n;
devi at i on = Mat h. Sqr t ( sumof sq / 8. 0) ;

Consol e. Wr i t eLi ne( " \ n\ n Sum= " +sum) ;
Consol e. Wr i t eLi ne( " \ n Mean = " +mean) ;
Consol e. Wr i t eLi ne( " \ n Devi at i on = " +devi at i on ) ;
Consol e. ReadLi ne( ) ;
}
}

Out put :

Ar r ay Li st consi st s of : 3. 5 57 2 6 24 14 95 23 74 23
Sum= 321. 5
Mean = 32. 15
Devi at i on = 49. 5381797202












8.13 & 8.14 - FINDING THE MAXIMUM & MINIMUM OF 3 NUMBERS ENTERED

usi ng Syst em;
cl ass Lar gest Smal l est
{
publ i c st at i c voi d Mai n( )
{
i nt a, b, c, l ar gest , smal l est ;

Consol e. Wr i t e( " Ent er No 1 : " ) ;
a = i nt . Par se( Consol e. ReadLi ne( ) ) ;

Consol e. Wr i t e( " Ent er No 2 : " ) ;
b = i nt . Par se( Consol e. ReadLi ne( ) ) ;

Consol e. Wr i t e( " Ent er No 3 : " ) ;
c = i nt . Par se( Consol e. ReadLi ne( ) ) ;

i f ( a > b)
{
i f ( a > c)
{
l ar gest = a;
}
el se
{
l ar gest = c;
}
}
el se
{
i f ( c>b)
{
l ar gest = c;
}
el se
{
l ar gest = b;
}
}


i f ( a < b)
{
i f ( a < c)
{
smal l est = a;
}
el se
{
smal l est = c;
}
}
el se
{
i f ( c<b)
{
smal l est = c;
}
el se
{
smal l est = b;
}
}

Consol e. Wr i t eLi ne( " \ n\ n The Lar gest Number = " +l ar gest ) ;
Consol e. Wr i t eLi ne( " \ n The Smal l est Number = " +smal l est ) ;
Consol e. ReadLi ne( ) ;
}
}


Out put :

Ent er No 1 : 15
Ent er No 2 : 54
Ent er No 3 : 21

The Lar gest Number = 54
The Smal l est Number = 15































8.15 – FINDING LARGEST ARRAY ELEMENT & AVERAGE OF
ARRAY ELEMENTS VIA METHODS.


usi ng Syst em;

cl ass Ar r ayFunct i on
{
publ i c st at i c voi d Mai n( )
{
l ong Lar gest ;
doubl e Aver age;
i nt c;
i nt num;
i nt [ ] ar r ay1;

Consol e. Wr i t e( " Ent er t he number of El ement s i n an Ar r ay : " ) ;

c=i nt . Par se( Consol e. ReadLi ne( ) ) ;

ar r ay1=new i nt [ c] ;

f or ( i nt i =0 ; i >c ; i ++)
{
Consol e. Wr i t eLi ne( " Ent er t he el ement " + i ) ;
num=i nt . Par se( Consol e. ReadLi ne( ) ) ;
ar r ay1[ i ] =num;
}

f or each ( i nt i i n ar r ay1)
{
Consol e. Wr i t e( " " + i ) ;
}

Consol e. Wr i t eLi ne ( ) ;

Lar gest = Lar ge( ar r ay1) ;
Aver age = Avg( ar r ay1) ;

Consol e. Wr i t eLi ne ( " \ n The l ar gest el ement i n t he ar r ay i s " +
Lar gest ) ;
Consol e. Wr i t eLi ne ( " The Aver age of el ement s i n t he ar r ay i s " +
Aver age) ;
Consol e. ReadLi ne( ) ;

}

/ / Det er mi ni ng t he l ar gest ar r ay el ement
st at i c i nt Lar ge ( par ams i nt [ ] ar r )
{
i nt t emp=0;

f or ( i nt i = 0; i < ar r . Lengt h; i ++)
{
i f ( t emp <= ar r [ i ] )
{
t emp = ar r [ i ] ;
}
}
r et ur n( t emp) ;
}

/ / Det er mi ni ng t he aver age of ar r ay el ement s
st at i c doubl e Avg ( par ams i nt [ ] ar r )
{
doubl e sum=0;

f or ( i nt i = 0; i < ar r . Lengt h; i ++)
{
sum= sum+ ar r [ i ] ;
}
sum= sum/ ar r . Lengt h;

r et ur n( sum) ;
}
}


Output:

Ent er t he number of El ement s i n an Ar r ay : 5

Ent er t he el ement 1 : 5
Ent er t he el ement 2 : 7
Ent er t he el ement 3 : 3
Ent er t he el ement 4 : 1
Ent er t he el ement 5 : 8

l ar gest el ement i n t he ar r ay i s 8
The Aver age of el ement s i n t he ar r ay i s 4. 8
























9.7 – SORTING 2 ARRAYS & MERGING INTO 1

usi ng Syst em;
cl ass Sor t Ar r ay
{
publ i c st at i c voi d Mai n( )
{
i nt [ ] A={127, 157, 240, 550, 510};
i nt [ ] B={275, 157, 750, 255, 150};
i nt CLengt h=( A. Lengt h +B. Lengt h) ;
i nt [ ] C=new i nt [ CLengt h] ;
i nt i =0, j =0, k;

Consol e. Wr i t el i ne ( “Sor t ed ar r ay l i st : ”) ;
f or ( k=0; k<=( i +j ) ; k++)
{
i f ( A[ i ] <=B[ j ] )
{
C[ k] =A[ i ] ;
Consol e. Wr i t e ( C[ k] + " " ) ;
i f ( i <4)
{
i ++;
}
}
el se
{
C[ k] =B[ j ] ;
Consol e. Wr i t e ( C[ k] + " " ) ;
i f ( j <4)
{
j ++;
}
}
}
f or ( i =0; i <CLengt h; i ++)
{
Consol e. Wr i t e( C[ i ] + " " ) ;
}
Consol e. ReadLi ne( ) ;
}
}

Out put :

Sorted array list : 127 150 157 157 240 255 275 510 550 750












9.11 - ACCEPTING A LIST OF 5 ITEMS

usi ng Syst em;
usi ng Syst em. Col l ect i ons;

cl ass Shoppi ngLi st
{
publ i c st at i c voi d Mai n( st r i ng [ ] ar gs)
{
Ar r ayLi st n = new Ar r ayLi st ( ) ;
n. Add( ar gs[ 0] ) ;
n. Add( ar gs[ 1] ) ;
n. Add( ar gs[ 2] ) ;
n. Add( ar gs[ 3] ) ;
n. Add( ar gs[ 4] ) ;

n. Sor t ( ) ;
Consol e. Wr i t eLi ne ( " The i t ems i n t he Shoppi ng Li st ar e : " ) ;

f or ( i nt i =0; i < nCount ; i ++)
{
Consol e. Wr i t eLi ne( ( i +1) + " " +n[ i ] ) ;
}
Consol e. Wr i t eLi ne( ) ;

n. Remove( 2) ; / / Del et es an i t emf r ml i st
n. Add( 3) = " Dal j i t " ; / / Adds an i t emi n t he l i st
n. Add( 5) = " End" ; / / Adds i n t he end of t he l i st

Consol e. Wr i t eLi ne ( " The i t ems i n t he Shoppi ng Li st Af t er modi f yi ng ar e : " ) ;

f or ( i nt i =0; i < nCount ; i ++)
{
Consol e. Wr i t eLi ne( ( i +1) + " " +n[ i ] ) ;
}

Consol e. ReadLi ne( ) ;
}
}

Output:

The i t ems i n t he Shoppi ng Li st ar e : Kar an Gi r i sh Neha Gaur av Raj u

The i t ems i n t he Shoppi ng Li st Af t er modi f yi ng ar e : Kar an Gi r i sh Raj u Dal j i t End













10.8 – COUNTING NUMBER OF WORDS IN A STRING

usi ng Syst em;

cl ass Count Wor ds
{
publ i c st at i c voi d Mai n( )
{
st r i ng s = " " ; / / Decl ar e ' s' of st r i ng t ype

Consol e. Wr i t e( " Ent er t he st r i ng : " ) ;

s = Consol e. ReadLi ne( ) ; / / Read st r i ng f r omt he consol e & save i n ' s'

st r i ng [ ] wor ds = s. Spl i t ( nul l ) ;
/ / ' wor ds' i s an ar r ay of st r i ng t ype
/ / st r i ng ' s' wi l l spl i t when a space ( nul l ) i s encount er ed
/ / Thi s wi l l be saved i nt o ar r ay wor ds

i nt i = wor ds. Lengt h; / / J ust count t he l enght of ar r ay ' wor ds'

Consol e. Wr i t eLi ne( " The t ot al number of wor ds i n t he ent er ed st r i ng : " +i ) ;

Consol e. ReadLi ne( ) ;
}
}



Output:

Ent er t he st r i ng : Dal j i t i s maki ng pr ogr ams

The t ot al number of wor ds i n t he ent er ed st r i ng : 4
















9.13 – REVERSING ARRAY BY CREATING A METHOD ‘REVERSE’


usi ng Syst em;
publ i c cl ass Rever seAr r ay
{
publ i c st r i ng Rever se( par ams st r i ng [ ] ar r )
{
st r i ng [ ] j ;
st r i ng [ ] k;

Consol e. Wr i t e( " The ar r ay l i st wi t hout r ever si ng i s : " ) ;
f or each ( i nt i i n ar r )
{
Consol e. Wr i t e( " " +i ) ;
j = new st r i ng[ i ] ; / / Save al l t he cont ent s i n t he ar r ay ' j '
i ++;
}

f or ( i nt a = 0; a < j . Lengt h ; a ++)
{
k[ a] = j [ a] ; / / Savi ng t he ar r ay i n anot her ar r ay
}

f or ( i nt i = 0; i < j . Lengt h ; i ++)
{
j [ i ] = k[ k. Lengt h] ; / / Her e we ar e r ever si ng t he ar r ay el ement s
k. Lengt h - - ;
}

Consol e. Wr i t e( " The r ever sed ar r ay now has : " ) ;

f or each ( i nt i i n j )
{
Consol e. Wr i t e( " " +j ) ; / / Pr i nt t he el ement s of t he ar r ay ' j '
i ++;
}

}
}






















10.9 – READ AN ARRAY & SORT IT

usi ng Syst em;
usi ng Syst em. Col l ect i ons; / / We need t o i mpl ement col l ect i on cl ass

cl ass Ar r ayLi st
{
publ i c st at i c voi d Mai n( st r i ng [ ] ar gs)
{
Ar r ayLi st n = new Ar r ayLi st ( ) ;

/ / Read al l t he ar r ay i t ems f r omt he consol e
n. Add( ar gs[ 0] ) ;
n. Add( ar gs[ 1] ) ;
n. Add( ar gs[ 2] ) ;
n. Add( ar gs[ 3] ) ;
n. Add( ar gs[ 4] ) ;

Consol e. Wr i t eLi ne ( " The i t ems i n t he Ar r ay Li st bef or e sor t i ng ar e : " ) ;

f or ( i nt i =0; i < n. Count ; i ++)
{
Consol e. Wr i t e ( i + " : " +n[ i ] ) ; / / Pr i nt each ar r ay el ement
}

n. Sor t ( ) ; / / Sor t t he ar r ay l i st

Consol e. Wr i t eLi ne ( " The i t ems i n t he Ar r ay Li st af t er sor t i ng ar e : " ) ;

f or ( i nt i =0; i < n. Count ; i ++)
{
Consol e. Wr i t e ( i + " : " +n[ i ] ) ; / / Pr i nt each ar r ay el ement
}
Consol e. ReadLi ne( ) ;
}
}

Output:

The i t ems i n t he Ar r ay Li st bef or e sor t i ng ar e : Raj awnt Kar an Gi r i sh Zeenat Dal j i t

The i t ems i n t he Ar r ay Li st bef or e sor t i ng ar e : Dal j i t Gi r i sh Kar an Raj awnt Zeenat