12 hour clock Multiplication
Last Updated :
11 Nov, 2022
Given two positive integers num1 and num2, the task is to find the product of the two numbers on a 12-hour clock rather than a number line.
Note: Assume the Clock starts from 0 hours to 11 hours.
Examples:
Input: Num1 = 3, Num2 = 7
Output: 9
Explanation: 3*7 = 21. The time in a 12 hour clock is 9.
Input: Num1 = 3, Num2 = 4
Output: 0
Approach: Follow the steps to solve this problem:
- Calculate Product as Num1*Num2 and store it in a variable Prod.
- If Prod = 12, return 0.
- Else If Prod ? 0 and Prod ? 11, return Prod.
- Else return, Prod % 12.
Note: You can skip all the steps and return (Num1*Num2) % 12 also, it also works fine.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int multiClock( int Num1, int Num2)
{
return (Num1 * Num2) % 12;
}
int main()
{
int num1 = 3, num2 = 7;
cout << multiClock(num1, num2) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
public static void main (String[] args)
{
int num1 = 3 , num2 = 7 ;
System.out.println(multiClock(num1, num2));
return ;
}
static int multiClock( int Num1, int Num2) {
return (Num1 * Num2) % 12 ;
}
}
|
Python3
def multiClock(Num1,Num2) - > int :
return (Num1 * Num2) % 12
if __name__ = = '__main__' :
num1 = 3
num2 = 7
print (multiClock(num1,num2))
|
C#
using System;
class GFG {
static int multiClock( int Num1, int Num2)
{
return (Num1 * Num2) % 12;
}
public static void Main( string [] args)
{
int num1 = 3, num2 = 7;
Console.Write(multiClock(num1, num2));
}
}
|
Javascript
<script>
function multiClock(Num1, Num2) {
return (Num1 * Num2) % 12;
}
let num1 = 3, num2 = 7;
document.write(multiClock(num1, num2));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...