Recursion, possible error in algo - c

i am doing one of the simple programin C, sum of digits of 5 digit number.Though i had done it using a simple function but i need to do it with recursion also.I had read many solution on net regarding this problem using recursion and had implemented one of mine.But that is giving error and i cant figure out what mesh i am doing in my algo.
#include<stdio.h>
int sum5(int x); //function for sum of digits of 5 digit number
int main()
{
int x;
int result;
printf("Enter a 5 digit number : ");
scanf("%d",&x);
printf("Number entered by you is %d",x);
result = sum5(x);
printf("Sum of digits of 5 digit number is = %d",&result);
return 0;
}
int sum5(int x)
{
int r;
int sum=0;
if(x!=0){
r=x%10;
sum=sum+r;
x=x-r; //doing this so that 0 come in the last and on diving it by 10, one digit will be removed.
sum5(x/10);
}
return sum;
}
but after its execution i am getting wrong result.It is dumping some anonymous value on the output.

Also, your sum5 function is incorrect. You have to add the value of sum5 to the sum variable of the caller function.
int sum5(int x)
{
int r;
int sum = 0;
if (x != 0) {
r = x % 10;
sum = r;
//x = x - r; - this isn't required. integer division will floor x
sum += sum5(x / 10);
}
return sum;
}

This is incorrect as it is printing the address of result and not its value:
printf("Sum of digits of 5 digit number is = %d",&result);
Change to:
printf("Sum of digits of 5 digit number is = %d", result);
Always check the result of scanf() to ensure a valid value was read:
/* Returns number of assignments made. */
if (scanf("%d", &x) == 1 && x > 9999 && x < 100000)
{
}
Plus the error in the implementation of sum5() as pointed out by Osiris
.

Related

C program for first seven terms in natural logarithm. I am not getting the right answer, can anyone review the following code

C program for first seven terms in natural logarithm. I am not getting the right answer, can anyone review the following code.
#include<stdio.h>
int main(){
float x,i,sum,result=0;
printf("Enter value of x:");
scanf("%f",&x);
for(i=2;i<=7;i++)
{
sum = (x - 1)/x;
result = (sum + (0.5 * pow(sum,i)));
}
printf("Sum of series of Natural Logarithm is: %0.2f",result);
return 0;
}
You haven't implemented the series correctly.
The iteration should begin at 1
The 0.5 is only correct for the second term
You overwrite result from each term instead of summing it.
Here is the corrected code. I also changed float to double, and i to int.
#include <stdio.h>
#include <math.h>
int main(void)
{
double x, sum, result = 0;
int i;
printf("Enter value of x: ");
int res = scanf("%lf", &x);
if(res != 1 || x <= 0.5) // validate
return 1;
for(i = 1; i <= 7; i++)
{
sum = (x - 1) / x;
result = result + pow(sum,i) / i;
}
printf("Sum of series of Natural Logarithm is: %f\n", result);
printf("The library function log() calculates: %f\n", log(x));
return 0;
}
Program session
Enter value of x: 0.75
Sum of series of Natural Logarithm is: -0.287697
The library function log() calculates: -0.287682

Program that prints prime numbers

Program that prints the 2 largest prime numbers in a given range from the user using recursion.
I want to get the 2 largest prime numbers in a range that is given by the user, but somehow it just prints the consecutive numbers in the range. The prime checker in my code is working when I built a program that asks the user for the end number, checks if it is a prime number and prints all prime numbers from 1 to n. But when I plugged it in this code, its not working well. I tried researching for other codes to be my reference but I cant find anything that prints the 2 largest prime numbers. It doesn't matter if the users input are prime numbers or not, the code will just check the two largest prime numbers in between. The end number or y can also be printed if its a prime number. I'm also trying to not use arrays to practice my parameter passing.
In this code, I'm trying to get the 2 largest prime number and print it. You can see the num1, and num2 are unused, they are supposed to be the variables for the 2 largest prime numbers in the given range.
#include <stdio.h>
void usersInput(int *x, int *y);
void swapping(int *x, int *y);
int primeCheck(int i, int j);
int main() {
int x, y, i, j, num1, num2;
usersInput(&x, &y);
if (x > y) {
swapping(&x, &y);
}
printf("The value of x is %d, and the value of y is %d.\n", x, y);
printf("\nThe two largest prime numbers from %d to %d are : ", x, y);
for (i = x; i <= y; i++)
if (primeCheck(i, y) == 0)
printf("%d ", i);
return 0;
}
void usersInput(int *x, int *y) {
printf("Enter the value of x: ");
scanf("%d", x);
printf("Enter the value of y: ");
scanf("%d", y);
}
void swapping(int *x, int *y) {
int temp;
temp = *x;
*x = *y;
*y = temp;
}
int primeCheck(int i, int j) {
if (j == i) {
return 0;
} else if (j % i == 0) {
return 1;
} else {
return primeCheck(i + 1, j);
}
}
As you can see, I don't have any functions yet for getting the 2 largest prime numbers. I don't really have idea on how to, so please help me
Here's the actual output:
Enter the value of x: 10
Enter the value of y: 3
The value of x is 3, and the value of y is 10.
The two largest prime numbers from 3 to 10 are: 6 7 8 9 10
Here's the expected output
Enter the value of x: 10
Enter the value of y: 3
The value of x is 3, and the value of y is 10.
The two largest prime numbers from 3 to 10 are: 5 7
The sub-statement of this if statement that is the call of printf:
if (primeCheck(i,y)==1)
printf("%d ", i);
gets the control when the function primeCheck returns 1.
But according to the function definition it returns 1 when y is divisible by any number in the range [i, y] and it is not necessary that this number is i:
} else if (j%i==0) {
return 1;
In all other case the function returns 0.
As the function calls itself recursively then it returns 1 when at least for one number in the range [x, y] y is divisible by that number.
For example for your input the function always will return 1 for each number if the range [3, 5] because 10 is divisible by 5.
Actually your function does not make a sense and has nothing common with prime numbers.
Also pay attention to that to find two biggest prime numbers in a range you should start the loop from the biggest number down to the smallest number of the range.
I think you understand the assignment incorrectly. It seems you need to write non-recursive function that determines whether a give number is a prime number and write a recursive function instead of the for loop that returns two greatest prime numbers.
That is the function that recursively finds two greatest prime numbers can look something like shown in the demonstration program below:
#include <stdio.h>
struct TwoPrimes
{
unsigned int first_prime;
unsigned int second_prime;
};
int is_prime( unsigned int n );
struct TwoPrimes find_two_primes( unsigned int first, unsigned int last )
{
if (first == last)
{
struct TwoPrimes two_primes = { ( is_prime( first ) ? last : 0 ), 0 };
return two_primes;
}
else
{
struct TwoPrimes two_primes = find_two_primes( first + 1, last );
if (two_primes.first_prime == 0 || two_primes.second_prime == 0)
{
if (is_prime( first ))
{
if (two_primes.first_prime == 0)
{
two_primes.first_prime = first;
}
else
{
two_primes.second_prime = first;
}
}
}
return two_primes;
}
}
int main( void )
{
struct TwoPrimes two_primes = find_two_primes( 3, 10 );
if (two_primes.first_prime != 0)
{
printf( "%u\n", two_primes.first_prime );
if (two_primes.second_prime != 0)
{
printf( "%u\n", two_primes.second_prime );
}
}
}
What you need to do yourself is to define the function is_prime.
There are multiple problems in your code:
the test for primality if (primeCheck(i, y) == 0) is incorrect: you should pass 2 as the first argument and i as the second.
the loop should start at i = y and run while i >= x, decrementing i to find the largest primes in the range first.
you should store the primes and stop when 2 have been found.
the primality test function should be modified to return 1 if i > j to prevent infinite recursion on some inputs, such as primeCheck(2, 1).
testing divisors up to and including sqrt(j) is sufficient in primeCheck(). You can stop the recursion when j < i * i, which can be tested without potential overflow as j / i < i.
Here is a modified version of your code:
#include <stdio.h>
int usersInput(int *x, int *y);
void swapping(int *x, int *y);
int primeCheck(int i, int j);
int main() {
int x, y, i, num1, num2;
if (!usersInput(&x, &y)) {
return 1;
}
if (x > y) {
swapping(&x, &y);
}
printf("The value of x is %d, and the value of y is %d.\n", x, y);
for (i = y, num1 = num2 = 0; i >= x; i--) {
if (primeCheck(2, i) == 0) {
if (num2 == 0) {
num2 = i;
} else {
num1 = i;
break;
}
}
}
printf("The two largest prime numbers from %d to %d are: %.0d %.0d\n",
x, y, num1, num2);
return 0;
}
// read user input return zero on failure
int usersInput(int *x, int *y) {
printf("Enter the value of x: ");
if (scanf("%d", x) != 1)
return 0;
printf("Enter the value of y: ");
return scanf("%d", y) == 1;
}
void swapping(int *x, int *y) {
int temp = *x;
*x = *y;
*y = temp;
}
int primeCheck(int i, int j) {
if (j < 2) {
return 1; // negative, 0 or 1: not prime
} else if (j / i < i) {
return 0; // all factors tested up to sqrt(j): j is prime
} else if (j % i == 0) {
return 1; // composite, not prime
} else {
return primeCheck(i + 1, j);
}
}
Output:
Enter the value of x: 10
Enter the value of y: 3
The value of x is 3, and the value of y is 10.
The two largest prime numbers from 3 to 10 are: 5 7

Finding numbers with unique digits in C

I have to write a program that finds every number (except 0) which can be factored by numbers from 2-9.
For example first such a number would be number 2520 as it can be divided by every single number from 2 to 9.
It also has to be a number that contains only 1 type of digit of its own (no multiple digits in a number). So for example 2520 will not meet this requirement since there are two same digits (2). The example of a number that meets both requirements is number 7560. That is the point I don't how to do it. I was thinking about converting value in an array to string, and then putting this string in another array so every digit would be represented by one array entry.
#include <stdio.h>
#include <math.h>
int main() {
int i, n, x, flag, y = 0;
scanf("%d", &n);
double z = pow(10, n) - 1;
int array[(int)z];
for (i = 0; i <= z; i++) {
flag = 0;
array[i] = i;
if (i > 0) {
for (x = 2; x <= 9; x++) {
if (array[i] % x != 0) {
flag = 1;
}
}
if (flag == 0) {
y = 1;
printf("%d\n", array[i]);
}
}
}
if (y == 0) {
printf("not exist");
}
return 0;
}
This should give you a base:
#include <stdio.h>
#include <string.h>
int main()
{
char snumber[20];
int number = 11235;
printf("Number = %d\n\n", number);
sprintf(snumber, "%d", number);
int histogram[10] = { 0 };
int len = strlen(snumber);
for (int i = 0; i < len; i++)
{
histogram[snumber[i] - '0']++;
}
for (int i = 0; i < 10; i++)
{
if (histogram[i] != 0)
printf("%d occurs %d times\n", i, histogram[i]);
}
}
Output:
Number = 11235
1 occurs 2 times
2 occurs 1 times
3 occurs 1 times
5 occurs 1 times
That code is a mess. Let's bin it.
Theorem: Any number that divides all numbers in the range 2 to 9 is a
multiple of 2520.
Therefore your algorithm takes the form
for (long i = 2520; i <= 9876543210 /*Beyond this there must be a duplicate*/; i += 2520){
// ToDo - reject if `i` contains one or more of the same digit.
}
For the ToDo part, see How to write a code to detect duplicate digits of any given number in C++?. Granted, it's C++, but the accepted answer ports verbatim.
If i understand correctly, your problem is that you need to identify whether a number is consisted of multiple digits.
Following your proposed approach, to convert the number into a string and use an array to represent digits, i can suggest the following solution for a function that implements it. The main function is used to test the has_repeated_digits function. It just shows a way to do it.
You can alter it and use it in your code.
#include <stdio.h>
#define MAX_DIGITS_IN_NUM 20
//returns 1 when there are repeated digits, 0 otherwise
int has_repeated_digits(int num){
// in array, array[0] represents how many times the '0' is found
// array[1], how many times '1' is found etc...
int array[10] = {0,0,0,0,0,0,0,0,0,0};
char num_string[MAX_DIGITS_IN_NUM];
//converts the number to string and stores it in num_string
sprintf(num_string, "%d", num);
int i = 0;
while (num_string[i] != '\0'){
//if a digit is found more than one time, return 1.
if (++array[num_string[i] - '0'] >= 2){
return 1; //found repeated digit
}
i++;
}
return 0; //no repeated digits found
}
// test tha function
int main()
{
int x=0;
while (scanf("%d", &x) != EOF){
if (has_repeated_digits(x))
printf("repeated digits found!\n");
else
printf("no repeated digits\n");
}
return 0;
}
You can simplify your problem from these remarks:
the least common multiple of 2, 3, 4, 5, 6, 7, 8 and 9 is 2520.
numbers larger than 9876543210 must have at least twice the same digit in their base 10 representation.
checking for duplicate digits can be done by counting the remainders of successive divisions by 10.
A simple approach is therefore to enumerate multiples of 2520 up to 9876543210 and select the numbers that have no duplicate digits.
Type unsigned long long is guaranteed to be large enough to represent all values to enumerate, but neither int nor long are.
Here is the code:
#include <stdio.h>
int main(void) {
unsigned long long i, n;
for (n = 2520; n <= 9876543210; n += 2520) {
int digits[10] = { 0 };
for (i = n; i != 0; i /= 10) {
if (digits[i % 10]++)
break;
}
if (i == 0)
printf("%llu\n", n);
}
return 0;
}
This program produces 13818 numbers in 0.076 seconds. The first one is 7560 and the last one is 9876351240.
The number 0 technically does match your constraints: it is evenly divisible by all non zero integers and it has no duplicate digits. But you excluded it explicitly.

Why is my to_base_n Program not working?

I need to write a C program which will read a number (in base 10) from user input and output it in any base which is a power of 2. The calculations have to be performed in one function, to_base_n, which takes the parameters num and base and prints the number in the respective base. As a validation check, the program also checks if the base is a power of two with the isPowerofTwo function.
The way the conversion is carried out is by means of long division which carries out the logic in the pseudocode below:
void to_base_n(int x, int n){
int r, i = 0
int digits[16]
while (x ≠ 0){
r = x mod n
x = x / n
digits[i] = r
i++
}
for (i = 0, i < 15, i++)
print digits[i]
}
Which I believe is arithmetically sound. But when I try to, for example, convert 82000 to base 4, I get the following output:
The large digits appearing are even bigger than num itself, so I figured the modulus cannot be entering the array properly (because ∀{x,n}; x mod n < x). I can't seem to find what's wrong with it. The full code is listed below.
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
bool isPowerofTwo(int);
void to_base_n(int, int);
int main(){
//Variables
int num, base;
//Prompt
printf("Please enter a number in base 10: ");
scanf("%d", &num);
printf("Please enter a base (2^n) to convert it to: ");
scanf("%d", &base);
//Precaution
while(!isPowerofTwo(base)){
printf("That number is not a power of 2. Please try again: ");;
scanf("%d", &base);
}
if(isPowerofTwo(base)){
//Output
printf("The number %d (base 10) is equivalent to ", num);
to_base_n(num, base);
printf(" (base %d).", base);
}
//Return Statement
return 0;
}
//Checks if Base is a Power of Two
bool isPowerofTwo(int base){
while((base % 2 == 0) && base > 1){
base = base / 2;
if(base == 1){
return true;
break;
}
}
return false;
}
//to_base_n
void to_base_n(int x, int n){
int r, i = 0;
int digits[16];
while(x != 0){
r = x % n;
x = x / n;
digits[i] = r;
i++;
}
for(i = 0; i < 15; i++)
printf("%d|",digits[i]);
}
Can anyone help explain what's wrong with it?
The number 82000 in base 4 would be:
110001100
Which is exacly what you get. Your mistake is that:
They are printed backwards.
You are printing more digits than you should, so you print garbage.
You ignore the number of digits extracted with your pseudo code, so you print uninitialised elements of the array.
for (i = 0, i < 15, i++)
print digits[i]
And they are printed in reverse order. I suggest changing it to this
for (i = i - 1, i >= 0, i--)
print digits[i]
and as C code in your function
for(i = i - 1; i >= 0; i--)
printf("%d|",digits[i]);

How to check whether a no is factorial or not?

I have a problem, then given some input number n, we have to check whether the no is factorial of some other no or not.
INPUT 24, OUTPUT true
INPUT 25, OUTPUT false
I have written the following program for it:-
int factorial(int num1)
{
if(num1 > 1)
{
return num1* factorial(num1-1) ;
}
else
{
return 1 ;
}
}
int is_factorial(int num2)
{
int fact = 0 ;
int i = 0 ;
while(fact < num2)
{
fact = factorial(i) ;
i++ ;
}
if(fact == num2)
{
return 0 ;
}
else
{
return -1;
}
}
Both these functions, seem to work correctly.
When we supply them for large inputs repeatedly, then the is_factorial will be repeatedly calling factorial which will be really a waste of time.
I have also tried maintaining a table for factorials
So, my question, is there some more efficient way to check whether a number is factorial or not?
It is wasteful calculating factorials continuously like that since you're duplicating the work done in x! when you do (x+1)!, (x+2)! and so on.
One approach is to maintain a list of factorials within a given range (such as all 64-bit unsigned factorials) and just compare it with that. Given how fast factorials increase in value, that list won't be very big. In fact, here's a C meta-program that actually generates the function for you:
#include <stdio.h>
int main (void) {
unsigned long long last = 1ULL, current = 2ULL, mult = 2ULL;
size_t szOut;
puts ("int isFactorial (unsigned long long num) {");
puts (" static const unsigned long long arr[] = {");
szOut = printf (" %lluULL,", last);
while (current / mult == last) {
if (szOut > 50)
szOut = printf ("\n ") - 1;
szOut += printf (" %lluULL,", current);
last = current;
current *= ++mult;
}
puts ("\n };");
puts (" static const size_t len = sizeof (arr) / sizeof (*arr);");
puts (" for (size_t idx = 0; idx < len; idx++)");
puts (" if (arr[idx] == num)");
puts (" return 1;");
puts (" return 0;");
puts ("}");
return 0;
}
When you run that, you get the function:
int isFactorial (unsigned long long num) {
static const unsigned long long arr[] = {
1ULL, 2ULL, 6ULL, 24ULL, 120ULL, 720ULL, 5040ULL,
40320ULL, 362880ULL, 3628800ULL, 39916800ULL,
479001600ULL, 6227020800ULL, 87178291200ULL,
1307674368000ULL, 20922789888000ULL, 355687428096000ULL,
6402373705728000ULL, 121645100408832000ULL,
2432902008176640000ULL,
};
static const size_t len = sizeof (arr) / sizeof (*arr);
for (size_t idx = 0; idx < len; idx++)
if (arr[idx] == num)
return 1;
return 0;
}
which is quite short and efficient, even for the 64-bit factorials.
If you're after a purely programmatic method (with no lookup tables), you can use the property that a factorial number is:
1 x 2 x 3 x 4 x ... x (n-1) x n
for some value of n.
Hence you can simply start dividing your test number by 2, then 3 then 4 and so on. One of two things will happen.
First, you may get a non-integral result in which case it wasn't a factorial.
Second, you may end up with 1 from the division, in which case it was a factorial.
Assuming your divisions are integral, the following code would be a good starting point:
int isFactorial (unsigned long long num) {
unsigned long long currDiv = 2ULL;
while (num != 1ULL) {
if ((num % currDiv) != 0)
return 0;
num /= currDiv;
currDiv++;
}
return 1;
}
However, for efficiency, the best option is probably the first one. Move the cost of calculation to the build phase rather than at runtime. This is a standard trick in cases where the cost of calculation is significant compared to a table lookup.
You could even make it even mode efficient by using a binary search of the lookup table but that's possibly not necessary given there are only twenty elements in it.
If the number is a factorial, then its factors are 1..n for some n.
Assuming n is an integer variable, we can do the following :
int findFactNum(int test){
for(int i=1, int sum=1; sum <= test; i++){
sum *= i; //Increment factorial number
if(sum == test)
return i; //Factorial of i
}
return 0; // factorial not found
}
now pass the number 24 to this function block and it should work. This function returns the number whose factorial you just passed.
You can speed up at least half of the cases by making a simple check if the number is odd or even (use %2). No odd number (barring 1) can be the factorial of any other number
#include<stdio.h>
main()
{
float i,a;
scanf("%f",&a);
for(i=2;a>1;i++)
a/=i;
if(a==1)
printf("it is a factorial");
else
printf("not a factorial");
}
You can create an array which contains factorial list:
like in the code below I created an array containing factorials up to 20.
now you just have to input the number and check whether it is there in the array or not..
#include <stdio.h>
int main()
{
int b[19];
int i, j = 0;
int k, l;
/*writing factorials*/
for (i = 0; i <= 19; i++) {
k = i + 1;
b[i] = factorial(k);
}
printf("enter a number\n");
scanf("%d", &l);
for (j = 0; j <= 19; j++) {
if (l == b[j]) {
printf("given number is a factorial of %d\n", j + 1);
}
if (j == 19 && l != b[j]) {
printf("given number is not a factorial number\n");
}
}
}
int factorial(int a)
{
int i;
int facto = 1;
for (i = 1; i <= a; i++) {
facto = facto * i;
}
return facto;
}
public long generateFactorial(int num){
if(num==0 || num==1){
return 1;
} else{
return num*generateFactorial(num-1);
}
}
public int getOriginalNum(long num){
List<Integer> factors=new LinkedList<>(); //This is list of all factors of num
List<Integer> factors2=new LinkedList<>(); //List of all Factorial factors for eg: (1,2,3,4,5) for 120 (=5!)
int origin=1; //number representing the root of Factorial value ( for eg origin=5 if num=120)
for(int i=1;i<=num;i++){
if(num%i==0){
factors.add(i); //it will add all factors of num including 1 and num
}
}
/*
* amoong "factors" we need to find "Factorial factors for eg: (1,2,3,4,5) for 120"
* for that create new list factors2
* */
for (int i=1;i<factors.size();i++) {
if((factors.get(i))-(factors.get(i-1))==1){
/*
* 120 = 5! =5*4*3*2*1*1 (1!=1 and 0!=1 ..hence 2 times 1)
* 720 = 6! =6*5*4*3*2*1*1
* 5040 = 7! = 7*6*5*4*3*2*1*1
* 3628800 = 10! =10*9*8*7*6*5*4*3*2*1*1
* ... and so on
*
* in all cases any 2 succeding factors inf list having diff=1
* for eg: for 5 : (5-4=1)(4-3=1)(3-2=1)(2-1=1)(1-0=1) Hence difference=1 in each case
* */
factors2.add(i); //in such case add factors from 1st list " factors " to " factors2"
} else break;
//else if(this diff>1) it is not factorial number hence break
//Now last element in the list is largest num and ROOT of Factorial
}
for(Integer integer:factors2){
System.out.print(" "+integer);
}
System.out.println();
if(generateFactorial(factors2.get(factors2.size()-1))==num){ //last element is at "factors2.size()-1"
origin=factors2.get(factors2.size()-1);
}
return origin;
/*
* Above logic works only for 5! but not other numbers ??
* */
}

Resources