Sunday 2 March 2014

Difference between “for” loop and “while” loop?

Topic
For
While
Main margin:
Generally runs till a predetermined iteration.
Loops until a condition meets.

Boolean substitution:
Not possible in general sense.
One of the best feature of while is Boolean (true/false) checking condition. e.g:

while ( x--){}


Accessory
variables:
Is a must creating 1 new variable.
No needed most of the time.

Avatars:
for has 2 types.

1. Regular for loop
for( initialization;  condition; increments/decrements )
{    statements; 
}

2. Alternative notation in Java 5
for( element type name :  collection )
{    statements;
// loop will execute each element 
}

Has while and do-while.
Speed:
Probably for loop, may be because it looks out for 1 specific item through statement body.

while loop has to check conditions for each run until the condition satisfy(ies).




Thursday 13 February 2014

UVA sloved: problem number: 10579 - Fibonacci Numbers

10579 - Fibonacci Numbers
Time limit: 3.000 seconds

Problem E: Fibonacci Numbers

A Fibonacci sequence is calculated by adding the previous two members of the sequence, with the first two members being both 1.
f (1) = 1, f (2) = 1, f (n > 2) = f (n - 1) + f (n - 2)

Input and Output 

Your task is to take numbers as input (one per line), and print the corresponding Fibonacci number.

Sample Input 

3
100

Sample Output 

2
354224848179261915075

Note: No generated fibonacci number in excess of 1000 digits will be in the test data, i.e. f (20) = 6765 has 4 digits..
Local_UVa'2003
_____________________________________________________________________

solution: 

import java.math.BigInteger;
import java.util.Scanner;

public class Main

{
    public static void main(String[] args)
    {
      Scanner input= new Scanner(System.in);
      int x;
      
      while(input.hasNext())
      {
          x=input.nextInt();
          BigInteger h=BigInteger.ONE;
          BigInteger m=BigInteger.ONE;
          BigInteger sum=BigInteger.ZERO;
          while(x>2)
          {
              sum=h.add(m);
              m=h;
              h=sum;
              x--;
          }
       System.out.println(sum);
      }
    }
}


/* had a little help from a big pal.*/

Wednesday 12 February 2014

UVA sloved: problem number: 11448 - Who said crisis?

11448 - Who said crisis?
Time limit: 10.000 seconds 
 B. Who Said Crisis? 

Context

The company Assistance for Critical Moments (ACM) is helping other companies to overcome the current economical crisis. As experts in computing machinery, their job is to calculate the cost/benefit balance of the other companies. They receive two numbers, indicating the total amount of benefits and costs, and they have to compute the final balance.

The Problem

You have to solve the complex business problem of computing balances. You are given two positive integer numbers, corresponding to the benefits and the costs. You have to obtain the total balance, i.e., the difference between benefits and costs.

The Input

The first line of the input contains an integer indicating the number of test cases.
For each test case, there is a line with two positive integer numbers, A and B, corresponding to the benefits and the costs, respectively. Both numbers are between 0 and a googol (10100) to the power of a hundred.

The Output

For each test case, the output should consist of a line indicating the balance: A-B.

Sample Input

4
10 3
4 9
0 8
5 2

Sample Output

7
-5
-8
3

_________________________________________________________________


solution: 
import java.util.Scanner;
import java.math.BigInteger;

public class Main
{
public static void main(String[] args)
{
Scanner input= new Scanner(System.in);
BigInteger x,y;
int c,d;

c= input.nextInt();
d= 0;

while(c>d)
{
x= input.nextBigInteger();
y= input.nextBigInteger();
System.out.println(x.subtract(y));
d++;
}
}
}


Saturday 8 February 2014

UVA sloved: problem number: 10110 - Light, more light

10110 - Light, more light

Time limit: 3.000 seconds
Light, more light

The Problem

There is man named "mabu" for switching on-off light in our University. He switches on-off the lights in a corridor. Every bulb has its own toggle switch. That is, if it is pressed then the bulb turns on. Another press will turn it off. To save power consumption (or may be he is mad or something else) he does a peculiar thing. If in a corridor there is `n' bulbs, he walks along the corridor back and forth `n' times and in i'th walk he toggles only the switches whose serial is divisable by i. He does not press any switch when coming back to his initial position. A i'th walk is defined as going down the corridor (while doing the peculiar thing) and coming back again. Now you have to determine what is the final condition of the last bulb. Is it on or off?
 

The Input

The input will be an integer indicating the n'th bulb in a corridor. Which is less then or equals 2^32-1. A zero indicates the end of input. You should not process this input.

The Output

Output "yes" if the light is on otherwise "no" , in a single line.

Sample Input

3
6241
8191
0

Sample Output

no
yes
no



by-
Sadi Khan
Suman Mahbub
01-04-2001

_______________________________________________

solution:

#include<stdio.h>
#include<math.h>

int main()
{
long n, a, b;
while(scanf("%lld", &n)==1)
{
if(n==0)
break;
a= sqrt(n);
b=a*a;

if(b==n)
printf("yes\n");
else
printf("no\n");
};
return 0;
}


Sunday 26 January 2014

UVA sloved: problem number: 11479 - Is this the easiest problem?

11479 - Is this the easiest problem?

A triangle is a geometric shape with three positive sides. However, any given three sides won’t necessarily form a triangle. The three sides must form a closed region. Triangles are categorized depending on the values of the sides of a valid triangle. In this problem you are required to determine the type of a triangle.

 
  Input    
  The first line of input will contain a positive integer T<20, where T denotes the number of test cases. Each of the next T lines will contain three 32 bit signed integer.  
     
  Output  
  For each case of  input there will be one line of output. It will be formatted as:
Case {x}: {triangle type}. Where x denotes the case number being processed and {triangle type} is the type of the triangle..{triangle type} will be one of the following, depending on the values of the three sides:
    • Invalid - The three sides can not form a triangle
    • Equilateral  - All three sides of valid triangle are equal
    • Isosceles  - Exactly two of the sides of a valid triangle are equal.
    • Scalene - No pair of sides are equal in a valid triangle.
 
     
  Sample Input Sample Output    
  4
1 2 5
1 1 1
4 4 2
3 4 5
Case 1: Invalid
Case 2: Equilateral
Case 3: Isosceles
Case 4: Scalene
 
 


 

__________________________________________________________

#include<stdio.h>
int main()
{
    long int t,a,b,c,i;
    while(scanf("%ld",&t)==1)
    {
        i=1;
        while(i<=t)
        {
            scanf("%ld%ld%ld",&a,&b,&c);
           
            if((a+b)<=c || (b+c)<=a || (c+a)<=b)
                printf("Case %ld: Invalid\n",i);
           
            else if(a<=0 || b<=0 || c<=0)
                printf("Case %ld: Invalid\n",i);
           
            else if(a==b && b==c)
                printf("Case %ld: Equilateral\n",i);
           
            else if(a==b || b==c || c==a)
                printf("Case %ld: Isosceles\n",i);
           
            else
                printf("Case %ld: Scalene\n",i);
            i++;
        }
    }
    return 0;
}



Thursday 9 January 2014

Determine whether the given number is a multiple of 3 or not.

Input

First line contains the number of test cases T (T <= 10), then T lines follow, each contains an integer N (0 <= N <= 1000).

Output

For each test case, if N is a multiple of 3, first print N, then print the string “ is a multiple of 3”, otherwise, first print N, then print “ is not a multiple of 3” without the quotes. See sample input output for more details.

Example

Input:
3
1
6
10

Output:

1 is not a multiple of 3
6 is a multiple of 3
10 is not a multiple of 3

______________________________

#include <cstdio>

int main()
{
    int n, t;
    scanf("%d",&t);
    while(t>0){
        scanf("%d",&n);
       
        if ((n % 3) == 0)
            printf("%d is a multiple of 3\n",n);
        else
            printf("%d is not a multiple of 3\n",n);

        t--;
    }
}

Saturday 4 January 2014

Difference between “while” loop and “do while” loop?

do while: starts executing contents of the loop before looking at the condition. therefore, execution shall run at least for once even if condition doesn't math.

e.g:

int  x=11;
        do{
            System.out.println("human being VS being human");
            x++;
        }while(x<10)
this one will execute 1 time before it goes down to look for the condition, after that it will stop.

and 

while: looks for the condition at the very first before executing the content.

e.g:
int  x=11;
        while(x<10){
            System.out.println("human being VS being human");
            x++;
        };

this one won't go inside the loop since the condition doesn't match.


skeleton code: real number comperation using Conditional Operator in Java ( ? : ) [ simple, user friendly ]


import java.util.Scanner;

class grps{
    public static void main(String[] args){
        Scanner input= new Scanner(System.in);
       
        System.out.print("enter the first input: ");
        double a=input.nextDouble();
       
        System.out.print("enter the nextinput: ");
        double b=input.nextDouble();
       
        System.out.println(a>b ? "First one is the bigger.":"Second one is the bigger."); //( ? : )
    }
}



raw code: Compound Interest calculation in Java [ simple, user friendly]

Q: lets say, you to find out what will be the number of viewers in my blog in next 30 days. given that, for instance i have 10,000 views now, increasing rate is 1% each day. you have to calculate it for next 30 days and show the result in every other 10th day.
formula is: amount= pri(1+rate)^time

=>
import java.util.Scanner;

class grps{
    public static void main(String[] agrs){
        Scanner input= new Scanner(System.in);
        int y=0;
       
        System.out.print("enter the sum you have already: ");
        double principal=input.nextDouble(); //=10000
       
        System.out.print("how much is the growth rate: ");
        double rate=input.nextDouble();    //=.01
       
        System.out.print("for how many days you want to calculate: ");
        double day=input.nextDouble();    //=30
       
        System.out.print("how often you want to see the result [e.g: 1 being every day, 2 being every other day]: ");
        double show=input.nextDouble();    //=10
       
        for(int x=1; x<=day; x+=show){
            y+=(int)(principal*Math.pow(1+rate, day));
            System.out.println("In "+x+" days there will be "+y+" followers!");
        }
    }
}