In this Chef and Chocolate problem, A man wants to gift C Chocolates to His son on his birthday. However, he has only X chocolates with him. The cost of 1 chocolate is Y rupees. So we need to find the minimum money in rupees Man needs to spend so that he can gift C Chocolates to his son.
let's say a man has to gift a total of 9 chocolates out of which he has 7 chocolates. Thus, man needs to buy 2 more chocolates, which costs him 10 rupees.
Problem solution in C programming.
#include <stdio.h>
int main(void)
{
int T, c, x, y;
scanf("%d", &T);
while(T--)
{
scanf("%d %d %d", &c, &x, &y);
printf("%d\n", (c-x)*y);
}
return 0;
}
Here in the above code we first declare four integers variables and read an integer value that's the number of test cases given by the user and store it in the variable T. after that we run a while loop to read each and every test case. after that in each test case, we read all the three inputs given by the user and then print the amount of money that the man needs to buy these chocolates.
The simple logic that we use here is that we take the maximum number of chocolates that man needs and minus with the number of chocolates he has. so we got the number of chocolates that man needs to buy. after that we multiply by y because each chocolate has y price.
Problem solution in C++ programming.
#include <iostream>
using namespace std;
int main() {
int t;
cin>>t;
while(t>0){
int c,x,y;
cin>>c>>x>>y;
cout<<(c-x)*y<<endl;
t--;
}
return 0;
}
Problem solution in Java programming.
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0){
int c = sc.nextInt();
int x = sc.nextInt();
int y = sc.nextInt();
System.out.println((c - x) * y);
}
sc.close();
}
}
Problem solution in Python programming.
# cook your dish here t = int(input()) for _ in range(t): c,x,y= map(int,input().split()) print((c-x)*y)
Here in the above code we first read input and convert it into the integer because python reads all the inputs in string format. after that, we run the for loop to run the check for each and every test case. here t is the number of test cases that the user will enter. after that, we read three integer inputs c, x, and y using the map and split function. after that, we use the same logic as we use in the above c program. and print the amount of money that man needs to buy the chocolates for his son.
0 Comments