Part 3: Overriding vs. Overload in JAVA with example.

Part 3: Overriding vs. Overload in JAVA with example.

Key Point on Overriding vs Overloading:

Overriding is a run-time concept while overloading is a compile-time concept. In any object-oriented programming language(OOP), The ability of a subclass to override a method. This process allows a class to inherit from a super class whose behavior is frequently similar in nature and then to modify behavior as needed. Use the “@Override” annotation to instructs the compiler that  intend to override a method in the superclass. If  the compiler detects that the method does not exist in one of the super classes, then it will generate an error.

Besides in Java OOP, two or more methods may have the same name if they differ in parameters such as different number of parameters, different types of parameters, or both. In These methods are called overloaded methods and this feature is called method overloading. These Overloading methods have the same name but accept different arguments. Method Overloading in Java with examples briefly discuss in below. Method Overloading  feature  allows a class to have more than one method having the same name.

Step by step overriding vs. overload in JAVA  features with real example is given below.

Overriding Features:

  • Same method name.
  • Same parameter list.

Overriding allows a child class to provide a specific implementation of a method that is already provided its parent class.

In below example Learn is parent class and Teach is child class and share() method is specific implementation of  method between these class.

class Learn{
 public void share(){
     System.out.println("class learn method share");
 }
}
class Teach extends Learn{
    
     public void care(){
     System.out.println("class Teach method care");
 }
    public void share(){
     System.out.println("class Teach method share");
 }
}

Here Learn and Teach both class uses same method same name,same perameter share().

Overloading Features:

  • Same method name.
  • Different parameter list.
class Learn{
 public void share(int n){
     System.out.println("class learn method share");
 }
   public void share(int n1,int n2){
     System.out.println("class Teach method share");
 }
}

During compilation time Compiler know they are different because they have different method signature.Two method signature are like:

share(int)

share(int,int)

if both method have

  • same method name.
  • same number of arguments.
  • and same type of arguments.

like below then compiler show compile error.

share(int n)

share(int n)

Draftsbook is a learning base blog. The blog share Article series based on Subject. For example:

  1. JAVA : JAVA  is a object-oriented programming(OOP) language. This language classes and objects are the two main aspects of OOP. The blog content discuss below principles of OOP:
    1. Encapsulation
    2. Inheritance
    3. Abstraction
    4. Polymorphism
  2. Data Structure: Data structure refers a several way of organizing data in a computer system.
  3. Android : Android software development  apps can be written using Kotlin, Java, and C++ languages” using the Android software development kit (SDK). This blog contains  android feature Navigation drawer implementation and its usage are mention.
  4. IELTS : IELTS  Four module with strategies are written hereby

5.Problem Solving : Problem solution on various online platform tips and solution are given here following platform.

Part 3: Overriding vs. Overload in JAVA with example.

Part 2: Method Signature in Java with Example.

Method Signature in Java:

Firstly need to know what is java method? why to use in java?

Java method refers, a method which is a block of code which only runs when it is called. Here data can pass as parameters into a method. Methods are used to perform certain actions, and they are also known as functions.

Use:

This is mainly use to frequent use a same function by call that is reuse a function.

To create a method must be declared within a class. It is followed by parentheses () with the name of the method. Java provides some pre-defined methods, such as System.out.println(), though  can also create  own user define methods to perform secific actions:

Example
Create a method inside Tree:

public class Tree{
static void treeMethod() {
// code to be executed
}
}

Example Explained

  • treeMethod() is the name of the method.
  • static means that the method belongs to the Main class and not an object of the Tree class.
  • void means that this method does not have a return value.

Now with another example brief it shortly:

Next need to know what  is method signature and it’s declaration. It is refers to a method-name to parameter-list before body part of a java class.

An example of method declaration:

public double showValue(int types,double area)
{

    //to do

}

So in method declaration uses components are:

modifiers: It controls the access level,

Modifier Description
public The public use in code which mean it is accessible for all classes.
private This Modifier refers the code is only accessible within the declared class
default The code is only accessible in the same package. This is used when  don’t want to specify a modifier.
protected The code is accessible in the same package and subclasses.

return type: A return type may be a primitive type. For instance int, float, double, a reference type or void type(returns nothing),

method name: A method name  should follow the camel case. First letter of the first word should be small and the first letters of the remaining words should be capital,

parameter list in parenthesis: Parameters act as variables inside the method.

public class Main
{
static void mMethod(String first_name)

{
System.out.println(first_name );
}

public static void main(String[] args)

{
mMethod("Harun");
mMethod("Roshid");
}
}

Output:

Harun

Roshid

exception list: Java Built-in exceptions are suitable to explain certain error situations. It also supports  users to define their own exceptions.,

  • ArithmeticException
    This exception is thrown in code when an exceptional condition has occurred in an arithmetic operation.
  • ArrayIndexOutOfBoundsException
    It is thrown to indicate that an array has been accessed either negative or greater than or equal to the size of the array.

the method body: The method body is where all of the action of a method takes place,

enclosed between braces: Start to end body of a Java class .

Among these components(six total) only two comprises the method signature.

  • a method name,
  • parameter list in parenthesis

Following the example the method signature :

showValue( int types, double area)

That means, method signature is part of the method declaration , combination of the method name and the parameter list.  Overloading method signature get emphasis to its ability to write methods that have the same name but accept different parameters.

Through the method signatures ,  Java compiler can discern the difference between the methods at compilation time.

Method Signature Examples:

public double showValue(int types,double area,float result) {

    //to do

}

The method signature in the above example is showValue (int, double, float)

public double showValue(String name) {

    //to do

}

 

The Java compiler will let us add another method like the above example because its method signature is different showValue(String) in this case.

To Call a Method :

we discuss before in Java, write the method’s name followed by two parentheses () and a semicolon;

In the following example, treeMethod() is used to print a text (the action), when it is called:

Example
Inside main, call the treeMethod() method:

public class Tree {
static void treeMethod() {
System.out.println("hello executed treeMethod!");
}

public static void main(String[] args) {
treeMethod();
}
}

// Outputs “hello executed treeMethod!”

The method can be called multiple time. Above code main method rewrite for this code:

public static void main(String[] args) {
treeMethod();

treeMethod();

treeMethod();
}
}

// Here treeMethod() call three times and everytime shows same output

Java Method Parameters and Arguments:

Value can be passed to methods as parameter. Parameters act as variables inside the method.
Parameters are specified after the method name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma.

public class Tree {
static void myMethod(String languageName) {
System.out.println(languageName+ " language");
}

public static void main(String[] args) {
myMethod("Python");
myMethod("C");
myMethod("C++");
}
}

// Python language
// C language
// C++ language

Java can support Multiple Parameters. You can have as many parameters as you like:

Example

public class Tree{
static void myMethod(String languageName, (String version) {
System.out.println(languageName + " is a" + age);
}

public static void main(String[] args) {
myMethod("Python", "language");
myMethod("C", "language");
myMethod("C++", "language");
}
}

// Python is a language
// C is a language
// C++ is a language

Draftsbook is a learning base blog. The blog share Article series based on Subject. For example:

    1. JAVA : JAVA  is a object-oriented programming(OOP) language. This language classes and objects are the two main aspects of OOP. The blog content discuss below principles of OOP:
      1. Encapsulation
      2. Inheritance
      3. Abstraction
      4. Polymorphism
  1. Data Structure: Data structure refers a several way of organizing data in a computer system.
  2. Android : Android software development  apps can be written using Kotlin, Java, and C++ languages” using the Android software development kit (SDK). This blog contains  android feature Navigation drawer implementation and its usage are mention.
  3. IELTS : IELTS  Four module with strategies are written hereby

5.Problem Solving : Problem solution on various online platform tips and solution are given here following platform.

C program reverse a given string without using build in function.

C program reverse a given string without using build in function.

C:

Reverse a given string without using build in function

 

 

#include <stdio.h>
#include<string.h>
int main()
{

char string1[100],string2[100];
char s[]="Learn and share blog";
int i,j,k,count;
for(count = 0; s[count] != '\0'; count++);
for(j=count-1,i=0 ;j>=0; j--)
{
    string1[i]=s[j];
    i++;
}
string1[i]='\0';
printf("reverse string is : %s",string1);
return 0;
}

output:

reverse string is : golb erahs dna nraeL

C program reverse a given string without using build in function.

C program count length of string using recursion(without build in function).

Count length of string using recursion(without build in function).

 

#include<stdio.h>
#include <string.h>
int length_count(char *line)
{
  static int count = 0;
    if(*line != NULL)
    {
        count++;
    length_count(++line);
    }
    return count;
}
int main()
{
char str[] ="Learn and share blog";
int count=0;
printf("The  string length is :\t");
count=length_count(str);
printf("%d",count);
return 0;
}

Output:

The string length is : 20

C program reverse a given string without using build in function.

C program reverse a string using recursion.

Reverse a string using recursion.

Procedure:

#include<stdio.h>
#include <string.h>
void reverse(char *line)
{
    if(*line)
    reverse(line+1);
    printf("%c",*line);
}
int main()
{
char str[] ="Learn and share blog";
printf("The reverse string is :\t");
reverse(str);
return 0;
}

Output:

The reverse string is : golb erahs dna nraeL

C program reverse a given string without using build in function.

C program to Find First Largest Element and Second Largest Element in an Array.

First Largest Element and Second Largest Element in an Array.

Procedure:

#include <stdio.h>
int main()
{
    int arra[500], number, i;
    int FirstMax, SecondMax;

    printf("Enter Number of Elements in array\n");
    scanf("%d", &number);

    printf("Enter %d numbers \n", number);
    for(i = 0; i < number; i++){
        scanf("%d", &arra[i]);
    }

    FirstMax = SecondMax = -1;

    for(i = 0; i < number; i++){
        if(arra[i] > FirstMax){
            SecondMax = FirstMax;
            FirstMax = arra[i];
        }
        else if (arra[i] > SecondMax && arra[i] < FirstMax){
            SecondMax = arra[i];
        }
    }

    printf("First Largest Element : %d \nSecond Largest  Element: %d", FirstMax, SecondMax);
    return 0;
}

Output:

Part 3: Overriding vs. Overload in JAVA with example.

Part 11: Java Interface tutorials with Example.

Interface in Java

Interface is a similar kind of java class. It can be called as collection of abstract methods or another way of achieving abstract method as it contains empty bodies. It can declare as:  interface <any-name>{}.

Example
/* interface */
interface Box

{

/*interface method does not have a body */
public void boxHieght();

/*interface method does not have a body */
public void run();
}

Interface Feature:

  1. Class can contains variable, method etc. Similarly for interface.
  2. Like class interface have method, variable but these method does not contain any body part. This is main difference between interface and class.
  3. When we use any class, this can extends only one class. But a class can use multiple interface and all the interface method will be used as override. These override method can’t be contained any ambiguous problem.

Class which are used interface need to use implements such as

Class Example implements Shadow {}         package interfaccepackage;

public interface Shadow{

/*implicitly public abstract method*/

void manshadow();

/*implicitly public static final*/

int x = 100;

}

 

1. Here we show that the method is contain void but implicitly here the method actually used as public abstract if we do not write down in manshadow() method.

2. Variable x is not only int but also public static final either we write it or not.

When we declare any interface and any class implements this interface then compiler show that the interface has implementations and method has override. Below we use a class named as Example, where we implements Shadow interface.

When any class implements any interface must be override all the interface methods in the class otherwise will compilation error.

 

package interfaccepackage;
public Class Example implements Shadow {
int y;
public int noOflegs();
return 10;
}
public void printmn(){
System.out.println(“Class method println statements : “);
}
public static void main(String args[]){
Example ex = new Example ();
System.out.println(ex.y);
System.out.println(x);
System.out.println(ex.noOflegs());
ex.manshadow();
ex.printmn();
}
@Override
public void manshadow(){
System.out.println(“Interface method manshadow statements prints: “);
}
}

noOflegs(), printmn() are Example class method. Manshadow () method is override and this is a public method due to interface this method is default access method. When we use method of interface we need to override the method less restricted in the class. In Example class public is less restricted than default so we override manshadow () method as public.

Like Example class more class can be implements Shadow() interface and can override all the methods as user requirements

Output of the program

run:

0

100

10

Interface method manshadow statements prints:

Class method println statements :

 

0 is for the glabal variable y’s default initialization value.

100 interface variable x’s value

10 is noOflegs() method of Example class value.

 

First println is Example class

Second println is Override method

Interface is mainly used for polymorphism. Multiple classes can implements multiple interface. Besides multiple interface can extends multiple interfaces. But a java class cannot extends multiple interface because java support single inheritance.

These interface feature are explain with example below:

Interface TextA:

public interface TextA{
public abstract void print();
public static final int var=0;
}

Interface TextB:

public interface TextB{
void print();
}

There is two interface TextA and TextB both contains same method but there will be no ambiguity problem. But if we use it in C++ then there will show ambiguity problem. Now we check ambiguity when we implements these interfaces in a class named Neon {}

public Class Noon implements TextA, TextB{
void method(){
System.out.println(“Outside method in java : “);
}
public static void main(String args[]){
Neon n = new Neon();
n.print();
n.method();
}
@Override
public void print(){
System.out.println(“Outside method in java: “);
}
}

Here is the override method print(). If we click print() method ‘I’ key this will show implements from both interface TextA and TextB and work properly what we use in this method. That means interface has no interface ambiguity problem. Another uses of interface is class Neon can be abstract so print() method need not mandatory override. But if we wish we can use override. There is a problem of using abstract class because In abstract class no object can be created. Similarly

  • Interface no object can be created.
  •  An interface can extends another interface

public interface TextA extends TextB

{
public abstract void bprint();
public static final int var=0;
}

That is an interface can used another interface using extends.

Another feature of interface is marker interface. In this interface no method or variable included for example Cloneable(). This interface send special information to JVM. Again @Override in one kind of interface.

Cloneable() interface

package java.lang;
public interface Cloneable{
}

Cloneable() is default interface. We can implement user defined empty interface too.

package java.lang;

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

@Target(value = (ElementType.METHOD))

@Retention(value =RetentionPolicy.SOURCE )

public @interface Override{

}

More other tutorials on JAVA in Draftsbook  Article series on :

  • JAVA : Dart is similar feature with JAVA  language. Java Classes and objects are the two main aspects of object-oriented programming. The Principles of OOP are:
    1. Encapsulation
    2. Inheritance
    3. Abstraction
    4. Polymorphism

 

Part 3: Overriding vs. Overload in JAVA with example.

Part 9: Nested(Inner) class in JAVA with EXAMPLES

Nested class in JAVA with EXAMPLES

Nested class if we declare one or more classes in a class then these classes are known as nested class.

Java Allows two category of nested class

  1. Static nested class
  2. Non Static nested class or inner class.

i) Member inner class: When declare any class inside a class but outside a method

ii) Anonymous inner class: this class uses for interface and abstract

iii) Local inner class: this class is more restricted class than others. This class used inside method.

Why use nested class? When we declare a private variable in a class others class can’t use this variable. So the purpose of nested class is hiding or encapsulate.

Inner class or non-static class can use outer class variable as global variable.

Member inner class example:

  1. public class Outerclass {
    private int data=30;
    class Inner{
    void printline(){
    System.out.println("inner class Printline");
    System.out.println("Outer class data variable value: "+data);
    }
    }
    void display(){
    Outerclass.Inner inner = new Outerclass.Inner();
    inner.printline();
    }
    public static void main(String args[]){
    Outerclass outer = new Outerclass();
    outer.display();
    }
    }

     

Following the program Inner class use as member inner class. To display any line in the Inner class and to print outer class variable, here need outer class object in main method.

Line 10 can be rewritten as Inner inner = new Inner(). Keep in mind we can’t create Inner class object in main method because main method is a static method. In outer class create display method where need to create inner class object to print inner class method.

When outer.display() called then inner class method printline() is run.

Output:

run:

inner class Printline

30

BUILD SUCCESSFUL (total time: 0 seconds)

Outer class variable value run as globally, In case of Inner class variable or method need to create inner class object that already used in the example.

Anonymous inner class: This type of inner class most important. This inner class used in interface and abstract. As interface and abstract can’t create any object so inner class is the best use for interface and inner class.

Interface consist of ‘able’ ,e.g default interface Cloneable()

Now example for anonymous inner class

  1. interface Enable{
    void screen();
    }
    public class TestClass{
    public static void main(String args[]){
    Enable e = new Enable() {
    @Override
    public void screen() {
    System.out.println("Nice and Simple");
    }
    };
    e.screen();
    }
    }
    
    
    

    Following the example Enable is an interface there contains a method void screen(). To implement the method there needs to be @Override following

  1. interface Enable{
    void screen();
    }
    public class TestClass implements Enable{
    public static void main(String args[]){
    }
    @Override
    public void screen() {
    System.out.println("Using implements Enable interface");
    }
    }

     

We can use Enable interface by using implements. When implements Enable interface this TestClass  show message for Override method of interface.

So anonymous inner class can be used as inner class or by implements interface.

As this class known as anonymous we can rewrite the inner class like

  1. new Enable() {
    @Override
    public void screen() {
    System.out.println("Using implements Enable interface");
    }
    }.screen();
    

    Here this inner class means obj.method() where object is anonymous. This anonymous inner class can be used for interface similarly for the abstract class. We know interface is implicitly public abstract where no method has any body part. For the abstract class user need to declare class and method as abstract. Example anonymous inner class for abstract class:

  1. abstract interface newinterface
    {
    abstract void eat();
    }
    public class NewClass {
    public static void main(String args[])
    {
    new newinterface(){
    @Override
    public void eat()
    {
    System.out.println("Sweet food");
    }
    }.eat();
    }
    }
    

    Here this inner class means obj.method() where object is anonymous and the class is abstract.

Local inner class:

A class that is created inside a method is known as local inner class. If you want to invoke the methods of local inner class, you must instantiate this class inside the method.

1)      Local inner class cannot be invoked from outside the method.

2)      Local inner class cannot access non-final variable.

Example of local inner class:

public class LocalInner {    
 private int data = 30;    
 void localmethod()    
 {     
class Inner   
  {    
 void message()  
   {    
     System.out.println("Local variable value: "+data);   
  }  
   }   
  Inner inner = new Inner(); 
    inner.message();   
  }     
public static void main(String args[])
{   
  LocalInner obj = new LocalInner();
     obj.localmethod();  
   }    
 }

 

In main method when obj.localmethod(); is called local inner method called to run the inner class method  that is  inner.message(); is called.

Another example

public class LocalInner {
 private int data = 30; 
void localmethod() { 
int x=2;
 class Inner { 
void message() {
 System.out.println("Local variable value: "+data +"   "  +x); 
} } 
Inner inner = new Inner(); 
inner.message(); } 
public static void main(String args[])
{ 
LocalInner obj = new LocalInner();
 obj.localmethod(); 
} }

 

Here we declare a local variable x (line 5) in the inner class and want to invoke message() method then need to declare x as final. Example

void localmethod() {
 final int x=2; class Inner{ 
void message() { 
System.out.println("Local variable value: "+data+"   " +x);
 } }

 

 

Above all if local variable does not final,local variable can’t invoke the local inner class.

Static Nested Class;

Example:

public class NewClass1 { 
    static int a =1;
     static class Inner{
     void message()
     {      
   System.out.println("Simple statements " +a);    
 }     static void display()     
{         System.out.println("Display statements");    
 }     }    
 public static void main(String args[])     { 
      Inner n = new Inner();      
 n.message(); // as message() is non-static so need to create object to call the method.    
   Inner.display(); //display() method is static so can be called by class ,no need create object here.           } }
Nested Interface: Interfaces can be nested, by default nested Interfaces are public and static (implicitely) Nested Interface program: public interface Displable {     void msg();     interface Eatable{         void eat();     } }

When we want to use eat() method in class the program would be:

class Exinterface implements Displable.Eatable{

@Override

public void eat() {

System.out.print(" Mango ");

}

}

public class NestedInterface{

public static void main(String args[]){

Exinterface ex = new Exinterface();

ex.eat();

}

}

 

In this program we implements outer interface class with inner interface to use eat() method.

When we want to use msg()  method in class the program would be:

class Exinterface implements Displable{

    @Override

    public void msg() {

        System.out.print(" This is a Nested Interface example ");

    }

}

public class NestedInterface{

public static void main(String args[]){

Exinterface ex = new Exinterface();

ex.msg();

}

}

 

Part 3: Overriding vs. Overload in JAVA with example.

Part 10: Java abstract class with Example.

Java abstract class with Example

When we declare any class with abstract then this class known as abstract class. Abstract class can method can be used as abstract and non-abstract. In Interface all methods implicitly public abstract and we know abstract methods can’t be any body part.

Example:

abstract class First
 {
 abstract void printA(); 
void printAB()
{ 
System.out.println("This is non-abstract method of abstract class  First");
 } 
}

Here printA() is an abstract method where printAB() is a non-abstract method. So, abstract class provide both abstract and non-abstract method.

Now, another class (e.g. class Second) wants to use this abstract class then that class need to extends the First class. The class Second can’t extends more than one class because the class maintain single inheritance. After extends class “First” need to override method printA() because this is an abstract method. printAB() method can be or can’t be override because this is optional and this method is not abstract. For the interface class, when another class need to use interface class then all the method interface mandatorily need to override.

Example:

class Second extends First
{
 @Override void printA() 
{
 System.out.println("This is override method abstract class  First");
 }
 }

In this part, printA() method is override as default same as class “First” printA() method. If class “First” is an interface then printA() method mendatorilly override as public.

Keep in mind abstract class has not any object references directly. If we want to create any abstract class object then we need to create inner class with override method of abstract class.

public class AbstractDemo {

public static void main(String args[]){

Second second = new Second();

second.printA();

second.printAB();

}

}

 

Here create object of second class to call the methods. As class”First” is abstract so we did not create any object of this class.

So abstract class main advantages are need not to mandatorily override all the methods in abstract class. Only which class are need in frequently we declare these methods as abstract latterly we use these methods as override and change methods body part as required.

Extra features of abstract class are given below using an program:

abstract class Initfirst
{ 
int dim1,dim2;
 Initfirst(int d1, int d2)

{ 
this.dim1 = d1; 
this.dim2 =d2;
 } 
abstract int sum(); 
abstract int mul(); 
} 
class Initsecond extends Initfirst
{
 public Initsecond(int d1, int d2) 
{ 
super(d1, d2); 
} 
@Override int sum() 
{ 
return (dim1 + dim2);
 } 
@Override int mul()
 { 
return (dim1 * dim2);
 } 
}
 class Inittheird extends Initfirst
{ 
public Inittheird(int d1, int d2)
 { 
super(d1, d2); 
} 
@Override int mul()
 {
 return (dim1 * dim2);
 }
 @Override int sum()
 { 
return (dim1 + dim2); 
} 
}
 public class ClassDemo
 { 
public static void main(String args[]) 
{ 
Initsecond initsecond = new Initsecond(2,1);
 Inittheird inittheird = new Inittheird(4,5);
 Initfirst initfirst; initfirst = initsecond; 
System.out.println("Sum is : "+initfirst.sum() + "   " +"Mul is : "+initfirst.mul());
 initfirst = inittheird; 
System.out.println("Sum is : "+initfirst.sum() + "   " +"Mul is : "+initfirst.mul()); 
} 
}

Following the program abstract class Initfirst contructor can not called directly like

Initfirst initfirst = new Initfirst(1,2); But Initfirst class subclass can be called directly by creating object.

Initsecond and Inittheird both extends Initfirst so Initfirst construct called by super() which is used both subclasses  in Initsecond and Inittheird.

In main method both subclasses object is created. As abstract class Initfirst can not be called directly so we can call it initialize as “ Initfirst  “any_name”; Follwing program Initfirst initfirst;

So we can make call by writing Initfirst

As, Initfirst = initsecond;

Initfirst = inittheird;

Timus Problem 1001 (Reverse Root) Solution

Timus Problem 1001 (Reverse Root) Solution

Timus Problem 1001(Reverse Root) :

Reverse Root is a mathematical problem.

To read details : http://acm.timus.ru/problem.aspx?space=1&num=1001

Proplem description: This is a straightforward problem.Just do square root.

Problem Solution:

#include <stdio.h>

#include <math.h>

int main()

{

double a[100],b[100];

int i,j,swap,n;

scanf("%d",&n);

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

scanf("%lf",&a[100]);

}

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

b[j]=sqrt(a[i]);

j++;}

for(i=0;i<j;i++){

    for(j=0;j<i;j++){

        if(b[j]==b[j+1]||b[j]>b[j+1]||b[j]<b[j+1]){

        swap=b[j];

        b[j]=b[j+1];

        b[j+1]=swap;}

    }

}

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

    printf("%.4lf\n",b[i]);

}

return 0;

}

Timus Problem 1001 (Reverse Root) Solution

Timus Problem 1005 (Stone pile) Solution

Timus Problem 1005 (Stone pile):

This problem is for beginner in timus online judge.

Problem details link : http://acm.timus.ru/problem.aspx?space=1&num=1005

Problem Description:

just find the minimal difference among a value range.

Usage Tips:

>> Right shift operator : deletes the number of bits what are given from right
if 24>>2 then
24 binary = 11000 ,let right shift we get 110 which is 6.

<< Left shift operator: adds zero  bits for the given number on the right.
if 24<<2 then
24 binary = 11000 ,let left shift we get 10000 which is 16.

Left and right shift operator                                 Left and right shift operator example

Problem Solution:

#include<stdio.h>

int main()

 {

 int n, x[22], sum, ans, possible_combine, i;



 scanf("%d",&n);

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

scanf("%d", &x[i]);

possible_combine = (1<<n);

 sum = 0;

 while(possible_combine)

 {

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

if(possible_combine & (1<<i))
{
        sum+=x[i];
}
else
{
sum-=x[i];
}
if(sum>=0 && ans>sum)

ans = sum;

sum = 0;

possible_combine--;

 }

 printf("%d\n", ans);

     return 0;

 }
Timus Problem 1001 (Reverse Root) Solution

Timus Problem 1025 (Democracy in Danger) Solution

Timus problem 1025 Democracy in Danger :

This problem is for beginner.

Problem Details: http://acm.timus.ru/problem.aspx?space=1&num=1025

Problem Description: This is a Straightforward problem.

Timus Problem 1025 Solution

#include<stdio.h>

int main()

{

int i,j,k,a,swap,n,sum=0,array[101];

scanf("%d\n",&n);

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

{

scanf("%d",&array[i]);

}

for(i=0;i<n-1;i++)

{

for(j=0;j<n-i-1;j++)

{

if(array[j]>array[j+1])

{



swap=array[j];

array[j]=array[j+1];

array[j+1]=swap;

}

}

}

k=i/2+1;

for(a=0;a<k;a++)

{

sum+=array[a]/2+1;

}

printf("\n%d",sum);

return 0;

}



 

Timus Problem 1001 (Reverse Root) Solution

Timus Problem 1068 Solution

Timus Problem 1068 Solution

#include <stdio.h>

#include <stdlib.h>

    long int sum[10000];

int a[10000];

int main()

{

    int i,n;

    sum[0]=1;

    scanf("%d",&n);

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

        scanf("%d",&a[i]);}

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

    sum[i]+=a[i];

    printf("%ld",sum[i]);}

}

 

Timus Problem 1001 (Reverse Root) Solution

Timus Problem 1083 Solution

Timus Problem 1083 Solution

#include <stdio.h>

int main()

{

    char ch;

    int n,k=0,f=1;

    scanf("%d %c",&n,&ch);

    if(ch=='!'){

        printf("%c",ch);

    k++;}

    else if(ch=='\n')

    printf("%c",ch);{

        break;}

    for(n=1;n<=10;n++)

    f*=f*(n-2k);

    if(f<=0){

        break;

    }

    printf("%d",f);

}

 

Timus Problem 1001 (Reverse Root) Solution

Timus Problem 1086 Solution

Timus Problem 1086 Solution

#include <iostream>

#include <stdio.h>

using namespace std;

bool flag[20000005];

int primes[20000005];

int cnt;



void sieve(int n)

{

cnt=0;

primes[cnt++] = 2;

for(int i=3; i<=n; i+=2)

{

if(flag[i] == 0)

{

primes[cnt++] = i;

if(i <= n/i)

{

for(int j=i*i; j<=n; j+=i*2)

flag[j] = 1;

}

}

}



return ;

}

int main()

{

    sieve(17000);

    int num,tc;

    scanf("%d",&tc);

    while(tc--)

    {

        scanf("%d",&num);

        printf("%d\n",primes[num-1]);

    }

    return 0;

}

 

Timus Problem 1001 (Reverse Root) Solution

Timus Problem 1100 Solution

#include <stdio.h>

int main()

{

    int i,swap,j,n,l,sum=0,k,ara[100][100];

    scanf("%d",&n);

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

        scanf("%d %d",&ara[i][j]);

    }

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

    {

        for(j=0;j<n-i;j++)

        {

            if(ara[j]<ara[j+1])

            {

                ara[i]=ara[i+1];

                ara[i+1]=ara[j];

                ara[j]=ara[i];

            }



            }

        } 

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

      {

      printf("%d %d\n",ara[i][j]);  

      }

 

Timus Problem 1001 (Reverse Root) Solution

Timus Problem 1104 Solution

#include <stdio.h>

 #include <ctype.h>

 int main()

 {

  long num=0,k,v,max=1;

  char ch;

  ch=getchar();

   while(ch!=EOF)

     {

      if(isdigit(ch)) v=ch-'0';

      else v=ch-'A'+10;

      num+=v;

      if(v>max) max=v;

      ch=getchar();

     }

   for(k=max;k<36;k++)

    if(num%k==0) break;

   if(k<36) printf("%ld",k+1);

   else printf("No solution.");

  return 0;

 }