A person’s body mass index is a simple calculation based on height and weight that classifies the person as underweight, overweight, or normal. The formula for metric unit is,
BMI = weight in kilograms / (height in meters)2
You are given a function,
Int GetBMICategory(int weight, float height);
The function accepts the weight (an integer number) and height (a floating point number) of a person as its arguments. Implement the function such that it calculations the BMI of the person and returns an integer, the person’s BMI category as per the given rules:
Note:
Example : Input:
42
1.54
Output:
0
Explanation:
The person’s weight is 42Kg and height is 1.54 metres BMI = weight in kilograms / (height in meters)2 = 42/(1.54 * 1.54) = 17.70
Since, BMI < 18, as per given rules, the output is 0.
Sample Input:
62
1.52
Sample Output:
2
Python Solution:
#BMI Problem statement
def GetBMICategory(weight, height):
BMI = weight/(height**2)
if BMI<18:
return 0
elif BMI>=18 and BMI<25:
return 1
elif BMI>=25 and BMI<30:
return 2
elif BMI>=30 and BMI<40:
return 3
else:
return 4
w = 62
h = 1.52
print(GetBMICategory(w,h))
Java Solution:
import java.util.Scanner;
class Main{
public int GetBMICategory(int weight, double height){
double bmi = weight/(height*height);
if(bmi<18){
return 0;
}
else if(bmi>=18 && bmi<25){
return 1;
}
else if(bmi>=25 && bmi<30){
return 2;
}
else if(bmi>=30 && bmi<40){
return 3;
}
else{
return 4;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Main obj = new Main();
int weight = sc.nextInt();
double height = sc.nextDouble();
int res = obj.GetBMICategory(weight, height);
System.out.println(res);
sc.close();
}
}
C Solution:
// BMI Problem statement
#include<stdio.h>
int bmi(int weight, float height){
float ans=weight/(height*height);
if(ans<18)
return 0;
else if(ans>=18 && ans<25)
return 1;
else if(ans>=25 && ans<30)
return 2;
else if(ans>=30 && ans<40)
return 3;
else
return 4;
}
int main(){
int weight;
float height;
scanf("%d %f",&weight,&height);
printf("%d",bmi(weight,height));
}
CPP Solution:
// BMI Problem statement
#include<iostream>
using namespace std;
int bmi(int weight, float height){
float ans=weight/(height*height);
if(ans<18)
return 0;
else if(ans>=18 && ans<25)
return 1;
else if(ans>=25 && ans<30)
return 2;
else if(ans>=30 && ans<40)
return 3;
else
return 4;
}
int main(){
int weight;
float height;
cin>>weight>>height;
cout<<bmi(weight,height);
}
Balance Fruits
Problem statement
Implement the following function:
int BalanceFruits(int a, int m, int rs);
You have a basket full of apples and mangoes, your job is to make the numer of apples and given a function that accepts three integers 'a', 'm' and 'rs' as its argument where 'a' and a basket respectively and 'rs' is the rupees that you have. Implement the function to balance the basket.
If 'a' > 'm', then buy (a - m) mangoes at the rate of Rs 1 per mango.
If 'a' < 'm', then sell (m - a) mangoes at the rate of Rs 1 per mango.
Return the total rupees left with you after balancing the fruits.
Assumption:
a > = 0, m > = 0 and rs > = 0
rs > = (a - m)
Note: If a = m, return rs unchanged
Example:
Sample Input:
a:8
m:4
rs:6
Sample Output:
2
Explanation:
Since a > m, (a - m) mangoes are bought at Rs 1 per mango, so rs = 6 - 4 = 2.
Thus, output is 2.
CPP Solution:
// Balanced Fruits Problem
#include<iostream>
using namespace std;
int fruit(int a, int m, int rs){
if(a>m)
return rs-(a-m);
else if(a==m)
return rs;
else
return rs+(m-a);
}
int main(){
int a,m,rs;
cin>>a>>m>>rs;
cout<<fruit(a,m,rs);
}
Java Solution:
import java.util.Scanner;
public class Main{
public static int balanceFruit(int a, int m, int rs){
if(a == m){
return rs;
}
else if(a > m){
return (rs - (a - m));
}
else{
return (rs + (m - a));
}
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int m = sc.nextInt();
int rs = sc.nextInt();
int res = balanceFruit(a, m, rs);
System.out.println(res);
}
}
Python Solution:
def Balancefruits(a,m,rs):
if a>m:
b=a-m
total=rs-b
return total
else:
c=m-a
total=rs-c
return total
x=Balancefruits(int(input()),int(input()),int(input()))
print(x)
Alternative Solution Python:
a = int(input())
m = int(input())
rs = int(input())
def BalanceFruits(a,m,rs):
if(a==m):
print(rs)
return
if(a>m):
rs = rs - (a-m)
print(rs)
else:
rs = rs + (m-a)
print(rs)
BalanceFruits(a,m,rs)
Count Specific Numbers Problem
Statement:
You are required to implement the following function:
int CountSpecificNumbers(int m, int n);
The function accepts two arguments m and n which are integers. You are required to calculate the count of numbers having only 1, 4 and 9 as their digits between the numbers lying in the range m and n both inclusive, and return the same. Return -1 if m>n.
Sample Input:
100
200
Sample Output:
9
Explanation:
The numbers between 100 and 200, both inclusive having only 1,4 and 9 as their digits are 111, 114, 119, 141, 144, 149, 191, 194, 199. The count is 9 hence 9 is returned.
Python Solution:
def countSpecificNumbers(m,n):
if m>n:
return -1
count = 0
for i in range(m,n+1):
num = i
flag = True
while num>0:
digit = num%10 #digit seperation logic
num //= 10
if digit==1 or digit==4 or digit==9:
continue
else:
flag = False
break
if flag:
count += 1
return count
m = int(input())
n = int(input())
print(countSpecificNumbers(m,n))
CPP Solution:
// Count specific number problem
#include<bits/stdc++.h>
using namespace std;
int countspecificnumber(int m, int n){
if(m<=n)
{
int count=0;
for(int i=m;i<=n;i++)
{
int num=i,flag=1;
while(num)
{
int n=num%10;
num=num/10;
if(n==1 || n==4 || n==9)
continue;
else
{
flag=0;
break;
}
}
if(flag==1)
count++;
}
return count;
}
return -1;
}
int main(){
int m,n;
cin>>m>>n;
cout<<countspecificnumber(m,n);
}
Count Digit Occurrences:
Problem statement:
You are required to implement the following function:
int CountDigitOccurrences(int l, int u, int x);
The function accepts 3 positive integers ‘l’, ‘u’ and ‘ x’ as its argument. you are required to calculate the number of occurrences of a digit ‘x’ in the digit of number lying in the range between ‘l’ and ‘u’ both inclusive, and return the same.
Note:
l<=u
0<=x<=9
Example:
Input:
l: 2
u: 13
x:3
Output:
2
Explanation:
The number of occurrences of digit 3 in the digits of the number lying in the range [2,13] both inclusive is 2, i.e{3,13}, hence 2 is returned.
Sample Input:
l: 1
u: 30
x: 2
Sample Output:
13
Python Solution:
def CountDigOC(l,u,x):
for num in range(l,u+1):
temp=num
count=0
while(temp>0):
x=num%10
count +=1
temp=temp//10
return count
y=CountDigOC(int(input()),int(input()),int(input()))
print(y)
JAVA Solution:
import java.util.Scanner;
public class Main{
public static int countDigitOccurences(int l, int u, int x){
int count = 0;
if(l > u){
return -1;
}
else{
for(int i=l; i<=u; i++){
int number=i;
boolean flag = true;
while(number != 0){
int num = number % 10;
number /= 10;
if(num == x){
count++;
}
}
}
}
return count;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int l = sc.nextInt();
int u = sc.nextInt();
int x = sc.nextInt();
int res = countDigitOccurences(l, u, x);
System.out.println(res);
}
}
CPP Solution:
// Count Digit Occurence Problem
#include<iostream>
using namespace std;
int countdigit(int l, int u, int x){
int n, count=0;
for(int i=l; i<=u;i++)
{
n=i;
while(n!=0)
{
if(n%10==x)
count++;
n=n/10;
}
}
return count;
}
int main(){
int l,u,x;
cin>>l>>u>>x;
cout<<countdigit(l,u,x);
}