See a problem in else if statement

Dangling else problem

Image
Hello, This problem comes when we use else if statement and do some mistake more describe is given below......... Dangling else problem: One of the major problem with nested if is dangling else problem. The problem arises when both outer if statement inner if statement might pair with common else statement. Thus the compiler is not able to judge to pair the else with which statement. The solution to this problem is given by the compiler that it pairs else with the recent if thus sometimes giving wrong result.                                           Hence the programmer should put early bracket wherever required. This post is may very short because my only aim was to describe this problem. Thank you, Keep supporting............. 

Restaurant Hackerrank solution in C and Filling Jar Hackerrank solution in C

 

Restaurant

Martha is interviewing at Subway. One of the rounds of the interview requires her to cut a bread of size  into smaller identical pieces such that each piece is a square having maximum possible side length with no left over piece of bread.

Input Format

The first line contains an integer  lines follow. Each line contains two space separated integers  and  which denote length and breadth of the bread.

Constraints

Output Format

 lines, each containing an integer that denotes the number of squares of maximum size, when the bread is cut as per the given condition.

Sample Input 0

2
2 2
6 9

Sample Output 0

1
6

Explanation 0

The 1st testcase has a bread whose original dimensions are , the bread is uncut and is a square. Hence the answer is 1.
The 2nd testcase has a bread of size . We can cut it into 54 squares of size , 6 of size . For other sizes we will have leftovers. Hence, the number of squares of maximum size that can be cut is 6.

My solution for this question in C:

#include <stdio.h>
int gcd(int a, int b)
{
    if(a == b)
        return a;
    if(a == 0)
        return b;
    return gcd(b%a,a);
}
int main()
{
    int t;
    scanf("%d",&t);

    while(t--)
    {
        int l,b,ans;
        scanf("%d%d",&l,&b);

        if(l<b)
            ans=gcd(l,b);
        else
            ans=gcd(b,l);

        printf("%d\n",(l*b)/(ans*ans));
    }
    return 0;

}


Filling Jars

Comments

Popular posts from this blog

Permutations of Strings hackerrank solution in C || Permutations of Strings

Input and output device

Pointing Devices