Lets write a C program to check whether two positive numbers entered by the user are Co-Prime numbers / Relative Prime Numbers or not using function / method.
Co-Prime numbers / Relative Prime Numbers: Two numbers are said to be co-prime or relative prime numbers if they do not have a common factor other than 1.
OR
Two numbers whose Greatest Common Divisor(GCD) is 1 are known as Co-Prime or Relative Prime Numbers.
Factors of a number: All the numbers which perfectly divide a given number are called as Factors of that number.
Note: User entered numbers(to check for co-prime) do not require to be prime numbers.
Source Code: C Program To Find If Two Numbers are Co-Prime or Not using Function
#include
int coprime(int num1, int num2)
{
int min, count, flag = 1;
min = num1 < num2 ? num1 : num2;
for(count = 2; count <= min; count++)
{
if( num1 % count == 0 && num2 % count == 0 )
{
flag = 0;
break;
}
}
return(flag);
}
int main()
{
int n1, n2;
printf("Enter 2 positive numbers\n");
scanf("%d%d", &n1, &n2);
if( coprime(n1, n2) )
{
printf("%d and %d are co-prime numbers.\n", n1, n2);
}
else
{
printf("%d and %d are not co-prime numbers.\n", n1, n2);
}
return 0;
}
Output 1: Enter 2 positive numbers 8 15 8 and 15 are co-prime numbers.
Output 2: Enter 2 positive numbers 12 15 12 and 15 are not co-prime numbers.
Note: coprime() method returns 1 or 0 value. If it returns 1, then the code inside if block gets executed. If it returns 0, then the code inside else block gets executed.
Lets write a C program to check whether two positive numbers entered by the user are Co-Prime numbers / Relative Prime Numbers or not.
Co-Prime numbers / Relative Prime Numbers: Two numbers are said to be co-prime or relative prime numbers if they do not have a common factor other than 1.
OR
Two numbers whose Greatest Common Divisor(GCD) is 1 are known as Co-Prime or Relative Prime Numbers.
Note: User entered numbers(to check for co-prime) do not require to be prime numbers.
Exit For Loop Since count = 8 and min = 8, control exits for loop.
We’ve checked the complete iteration of for loop and we can see that no common number perfectly divides both user entered numbers n1 and n2 i.e., 8 and 15. So (8, 15) are co-prime numbers or relative prime numbers.
Example for non co-prime number
If n1 = 12 and n2 = 15
Number 3 perfectly divides both n1 and n2 i.e., 12 and 15. So 12 and 15 have 2 common factors i.e., 1 and 3, hence (12, 15) are not co-prime or relative prime numbers.
Video Tutorial: C Program To Check If Two Numbers are Co-Prime or Not
Source Code: C Program To Find Two Numbers are Co-Prime or Not
#include
int main()
{
int n1, n2, min, count, flag = 1;
printf("Enter 2 positive numbers\n");
scanf("%d%d", &n1, &n2);
min = n1 < n2 ? n1 : n2;
for(count = 2; count <= min; count++)
{
if( n1 % count == 0 && n2 % count == 0 )
{
flag = 0;
break;
}
}
if(flag)
{
printf("%d and %d are co-prime\n", n1, n2);
}
else
{
printf("%d and %d are not co-prime\n", n1, n2);
}
return 0;
}
Output 1: Enter 2 positive numbers 8 15 8 and 15 are co-prime
Output 2: Enter 2 positive numbers 12 15 12 and 15 are not co-prime
Logic To Check If Two Numbers are Co-Prime or Not
User enters 2 positive numbers, we store those numbers in variables n1 and n2 respectively. We find the smallest number in it and store it inside variable min.
We initialize loop counter variable count to 2(as all the numbers are perfectly divisible by 1, so we skip number 1 and start with number 2). We iterate through the for loop until count value is less than or equal to min. We increment the value of count by one for each iteration of for loop.
Inside for loop we check if there are any common factors for n1 and n2. n1 % count == 0 && n2 % count == 0 If there are any common factors, then those numbers entered by the user are not co-prime / relative prime numbers. If there are no common factors for those two numbers, then they are co-prime or relative prime numbers.
Source Code: C Program To Express A Number as Sum of Two Prime Numbers
#include
#include
int nxtPrime(int);
int isPrime(int);
int main()
{
int num, count, flag = 0;
printf("Enter a positive number\n");
scanf("%d", &num);
for(count = 2; count <=(num-count); count = nxtPrime(count))
{
if(isPrime(num-count))
{
flag = 1;
printf("%d + %d = %d\n", count, num-count, num);
}
}
if(flag == 0)
{
printf("%d cannot be expressed as the sum of 2 prime numbers.\n", num);
}
return 0;
}
int nxtPrime(int num)
{
do
{
num++;
}while(!isPrime(num));
return(num);
}
int isPrime(int num)
{
int count, inum, prime = 1;
inum = sqrt(num);
for(count = 2; count <= inum; count++)
{
if(num % count == 0)
{
prime = 0;
break;
}
}
return(prime);
}
Output 1: Enter a positive number 14 3 + 11 = 14 7 + 7 = 14
Output 2: Enter a positive number 5 2 + 3 = 5
Output 3: Enter a positive number 24 5 + 19 = 24 7 + 17 = 24 11 + 13 = 24
Output 4: Enter a positive number 34 3 + 31 = 34 5 + 29 = 34 11 + 23 = 34 17 + 17 = 34
Output 5: Enter a positive number 41 41 cannot be expressed as the sum of 2 prime numbers.
Note: Function nxtPrime() and isPrime() returns integer type data so its return type is int. And both these methods / functions accepts 1 integer type argument.
Logic To Express A Number as Sum of Two Prime Numbers
1. We ask the user to enter a positive number and store it inside variable num. Now we write the for loop. We initialize the loop counter variable count to 2 – because 2 is the first number in prime number series. We iterate through the for loop until count is less than or equal to (num-count). For each iteration of for loop we change the value of count to next prime number in the prime number series.
We use following simple expression to find and printout the result: count + (num – count) = num;
We already know that value of count is prime number. Next inside for loop we check the second part of above expression i.e., (num-count) for prime number or not. If both count and (num-count) are prime numbers, then we output the values, if not, then we change the value of count to next prime number and check if (num-count) is prime or not. We continue doing this until count is less than or equal to (num-count);
2. We need to get next prime number for loop count variable count. So we need to define (write logic for) nxtPrime() method.
int nxtPrime(int num)
{
do
{
num++;
}while(!isPrime(num));
return(num);
}
value of count is passed to nxtPrime() method, which is copied to local variable num. First we increment the value of num inside do block and then check the condition in while. If the number is prime then we exit the do-while loop and return the number orelse we keep incrementing the value of num and check if the new number is prime or not, by passing it to the function isPrime().
int isPrime(int num)
{
int count, inum, prime = 1;
inum = sqrt(num);
for(count = 2; count <= inum; count++)
{
if(num % count == 0)
{
prime = 0;
break;
}
}
return(prime);
}
This is the beauty of function. We call the function isPrime() 2 times. Once from main() function and once from nxtPrime() function. This avoids repeating our code. We write the code once in our function and call it any number of times in the program. This is called DRY concept in programming i.e., Do Not Repeat Yourself.
Factors of a Number: All the numbers which perfectly divide a given number are called as Factors of that number.
Prime Number: Any natural number which is greater than 1 and has only two factors i.e., 1 and the number itself is called a prime number.
Note: The user entered number need not be a prime number. But the factors of the number must be prime number.
For example, 24 and 35 are not prime numbers. But the prime factors of 24 are 2, 2, 2, and 3. Here both 2 and 3 are prime numbers. Similarly, prime factors of 35 are 5 and 7. Both 5 and 7 are prime numbers.
Video Tutorial: C Program To Find Prime Factors of a Number using Function
Source Code: C Program To Find Prime Factors of a Number using Function
#include
void primefactors(int num)
{
int count;
printf("\nPrime Factors of %d are ..\n", num);
for(count = 2; num > 1; count++)
{
while(num % count == 0)
{
printf("%d ", count);
num = num / count;
}
}
printf("\n");
}
int main()
{
int num;
printf("Enter a positive integer\n");
scanf("%d", &num);
primefactors(num);
return 0;
}
Output 1: Enter a positive integer 24
Prime Factors of 24 are .. 2 2 2 3
Output 2: Enter a positive integer 35
Prime Factors of 35 are .. 5 7
Output 3: Enter a positive integer 315
Prime Factors of 315 are .. 3 3 5 7
Output 4: Enter a positive integer 24024
Prime Factors of 24024 are .. 2 2 2 3 7 11 13
Output 5: Enter a positive integer 510
Prime Factors of 510 are .. 2 3 5 17
Logic To Find Prime Factors of a Number, using Function
We ask the user to enter a positive integer number and store it inside variable num. We pass this value to a function primefactors().
Inside primefactors() function we write a for loop. We initialize the loop counter to 2 – which is the smallest prime number. We exit the for loop when num is less than or equal to 1.
Inside for loop we write a while loop to check if the number is perfectly divisible by the value of count. If yes, then we display the value of count and divide the num by count and store it back into variable num.
Since we continuously modulo divide and divide the number by 2, other numbers(which are multiples of 2) can not divide the number. Similarly, we divide the number by 3, so other numbers(which are multiples of 3) can not divide the number. Next we divide the number by 5, 7, 11, etc.
Note: Function primefactors() doesn’t return anything so its return type is void. It accepts 1 integer type argument.
Write a function power( a, b ), to calculate the value of a raised to b.
Note: In today’s video tutorial lets see 2 methods of calculating value of a raised to b. 1. In first method lets write the entire logic ourselves. 2. In second method lets use the built in method pow() which is present in math.h library file.
Problem Statement
To clarify, the problem statement is: User inputs two numbers through the keyboard. Write a C program to find the value of one number raised to the power of another, using function / method.
Source Code: C Program To Calculate One Number Raised To Another using Function
#include
int power(int base, int exp)
{
int count, result = 1;
for(count = 1; count <= exp; count++)
{
result = result * base;
}
return(result);
}
int main()
{
int a, b;
printf("Enter integer values for base and exponent\n");
scanf("%d%d", &a, &b);
printf("%d to the power of %d is %d\n", a, b, power(a, b));
return 0;
}
Output 1: Enter integer values for base and exponent 2 4 2 to the power of 4 is 16
Output 2: Enter integer values for base and exponent 3 2 3 to the power of 2 is 9
Output 3: Enter integer values for base and exponent 5 2 5 to the power of 2 is 25
Output 4: Enter integer values for base and exponent 5 3 5 to the power of 3 is 125
Logic
In above code, we ask the user to enter base and exponent values. We pass these 2 values to function power(). Inside power() function/method, we use for loop to multiply the value of base exponent number of times. This would give us the result and we return the result.
Example: In above code, if the user inputs 2 as base and 4 as exponent, then: for loop executes or iterates exponent number of times i.e., 4 times in our case. So 2 is multiplied 4 times.
2 x 2 x 2 x 2 = 16.
Source Code: C Program To Calculate One Number Raised To Another using Function and builtin method pow()
#include
#include
float power(int base, int exp)
{
return( pow(base, exp) );
}
int main()
{
int a, b;
printf("Enter integer values for base and exponent\n");
scanf("%d%d", &a, &b);
printf("%d to the power of %d is %0.2f\n", a, b, power(a, b));
return 0;
}
Output 1: Enter integer values for base and exponent 2 4 2 to the power of 4 is 16
Output 2: Enter integer values for base and exponent 3 2 3 to the power of 2 is 9
Output 3: Enter integer values for base and exponent 5 2 5 to the power of 2 is 25
Output 4: Enter integer values for base and exponent 5 3 5 to the power of 3 is 125
Logic
Here we ask the user to enter the value of base and exponent. We pass the values of base and exponent to power() method. Inside power() method we use pow() builtin method which is present in math.h header file. We pass base and exponent value to pow() function and it returns the result, which we return to calling function i.e., main method.
Note: pow() method is present in math.h library, so we include it at the top of our c source code.
Important Note: Note that we are using float as return type in above program(second method), since built in method pow() returns floating point or double type data. If we use integer type data as return type then it would give wrong results for certain inputs.
For example, if you have below code for power() function
int power(int base, int exp)
{
return( pow(base, exp) );
}
If would return 24 for 5 raise to 2. And 124 as result for 5 raise to 3. Both these results are wrong.
Important Note: As you might have observed already we are not using function prototype in above programs. That is because we are writing function definition at the top before main function from where we are calling power() function. So main method already knows that there is a method called power() in our program.
If we want to write more than 1 function and we need to arrange it properly, we can write function prototype at the top – so that we can know about the functions present in our program in one glance, in one place. And then we can write the function definition anywhere in our program, in any order. Function prototype and function definition orders do not matter.
Also note that, writing function prototype is optional if you write function definition before main.