I have this program to generate armstrong numbers upto a given range. But the problem is that the range variable (n in this case) is somehow acting like a const. I cannot assign a value nor increment it... There are errors during compilation with gcc. The power function works fine (Same issue with pow() defind in math.h). I would like to know why this is happening to this code and the possible fix(es). Thank you
#include <stdio.h>
//#include <math.h>
int power(int a, int b) {
int c = 1;
for(int i = 0; i < b; i++) {
c *= a;
}
return c;
}
void main(void) {
int sum, y, temp;
printf("temp, y, sum, n\n");
for(int n = 1; n < 100; n++) {
temp = n;
printf("%d ", temp);
y = 0; // y to hold the number of digits of n
while (n > 0) {
n = n / 10;
y++;
}
printf("%d ", y);
n = temp;
sum = 0;
while(n > 0) {
sum += power((n % 10), y);
n = n / 10;
}
if (temp == sum) {
printf("%d ", sum);
}
printf("%d\n", n);
}
}
Output:
temp, y, sum, n
1 1 1 0
1 1 1 0
1 1 1 0
1 1 1 0
1 1 1 0
1 1 1 0
1 1 1 0
.
.
.
Are you not constantly dividing n by 10?
As n is an integer that starts as 1, and not a float, it would constantly set to 0.
Your second while(n > 0) loop effectively sets n=0 within
the outer for loop.
Did you want to use a second n = temp?
before the last printf statement add: n = temp;
try you code like this:
#include <stdio.h>
//#include <math.h>
int power(int a, int b) {
int c = 1;
for(int i = 0; i < b; i++) {
c *= a;
}
return c;
}
void main(void) {
int sum, y, temp;
printf("temp, y, sum, n\n");
for(int n = 1; n < 100; n++) {
temp = n;
printf("%d ", temp);
y = 0; // y to hold the number of digits of n
while (n > 0) {
n = n / 10;
y++;
}
printf("%d ", y);
n = temp;
sum = 0;
while(n > 0) {
sum += power((n % 10), y);
n = n / 10;
}
if (temp == sum) {
printf("%d ", sum);
}
n = temp;
printf("%d\n", n);
}
}
It is because the value of n is set to 0 outside the loop. Try the below code.
#include <stdio.h>
//#include <math.h>
int power(int a, int b) {
int c = 1;
for(int i = 0; i < b; i++) {
c *= a;
}
return c;
}
void main(void) {
int sum, y, temp;
printf("temp, y, sum, n\n");
for(int n = 1; n < 100; n++) {
temp = n;
printf("%d ", temp);
y = 0; // y to hold the number of digits of n
while (n > 0) {
n = n / 10;
y++;
}
printf("%d ", y);
n = temp;
sum = 0;
while(n > 0) {
sum += power((n % 10), y);
n = n / 10;
}
if (temp == sum) {
printf("%d ", sum);
}
n = temp;
printf("%d\n", n);
}
}
Related
Given some natural numbers n and k, my goal is to write a C program that outputs a number formed by every k-th digit of n. I wrote a program as follows:
#include <stdio.h>
#include <math.h>
#define MAX 100
void printDigit(int n, int k)
{
int arr[MAX];
int i = 0;
int j, r;
while (n != 0) {
r = n % (int)pow(10,k);
arr[i] = r;
i++;
n = n / pow(10,k);
}
for (j = i - 1; j > -1; j--) {
printf("%d ", arr[j]);
}
}
int main()
{
int n = 12345678;
int k = 2;
printDigit(n,k);
return 0;
}
My code outputs the same number but partitioned into substrings of length k. Why is that and how can I fix it so that I get the number I wanted?
Your logic is too complicated, but if you want to stick to it:
int intpow(int x, int y)
{
int result = 1;
while(y--) result *= x;
return result;
}
void printDigit(int n, int k)
{
int arr[MAX];
int i = 0;
int j, r, powv;
while (n != 0) {
powv = intpow(10,k -1);
n /= powv;
if(!n) break;
r = n % 10;
arr[i] = r;
i++;
n = n / 10;
}
for (j = i - 1; j > -1; j--) {
printf("%d ", arr[j]);
}
}
n % (int)pow(10,k) is the low-order k digits of n. If you just want one digit, use n % 10.
Since pow(10, k) returns a double, it might not be an exact integer. You should round it to the nearest integer to do proper division.
void printDigit(int n, int k)
{
int arr[MAX];
int i = 0;
int j, r;
int divisor = lround(pow(10, k));
while (n != 0) {
r = n % 10;
arr[i] = r;
i++;
n = n / divisor;
}
for (j = i - 1; j > -1; j--) {
printf("%d ", arr[j]);
}
}
I am writing a program in order to count all achilles numbers without the math.h library. In this programm first I calculate powerful numbers after that I calculate GCD of powerful number. If GCD is 1 then the current number is achilles.
My problem is that my program works with scanf() but not with for loop counter! What i am doing wrong?
Thank you very much!
#include <stdio.h>
#define MAX 1000
int main(void)
{
int n;
int i, j, a;
int counter2 = 0;
int large;
int small;
int rem, gcd, max = 1, min = 1;
int achilles;
for (a = 1; a <= MAX; a++) //for loop for counter
{
n=a;
for (i = 1; i <= n; i++)
{
int count = 0;
for (j = 1; j <= i; j++)
{
if (i % j == 0)
{
count++;
}
}
int l = 0;
if (count == 2)
{
while (n % i == 0) // calculate factor and his exponent
{
l++;
n = n / i;
}
if (l > max) // calculates min and max in order to find GCD
{
max = l;
}
if (l < min)
{
min = l;
}
}
large = max;
small = min;
while (small) { // While small is not 0
// Calculates GCD
rem = large % small;
large = small;
small = rem;
}
gcd = large;
// printf("GCD(%d,%d)= %d", large, small, gcd);
}
if (gcd == 1) {
achilles = n;
// printf("%d\n", achilles);
}
// printf("GCD(%d,%d)= %d", max, min, gcd);
printf("%d\n", achilles);
}
}
The programm before editing with the for loop is the following!
#include <stdio.h>
#define MAX 1000
int main(void)
{
int n;
int i, j, a;
int counter2 = 0;
int large;
int small;
int rem, gcd, max = 1, min = 1;
int achilles;
scanf("%d", &n);
printf("%d = ",n);
for (i = 1; i <= n; i++)
{
int count = 0;
for (j = 1; j <= i; j++)
{
if (i % j == 0)
{
count++;
}
}
int l = 0;
if (count == 2)
{
while (n % i == 0) // calculate factor and his exponent
{
l++;
n = n / i;
}
if (l > max) // calculates min and max in order to find GCD
{
max = l;
}
if (l < min)
{
min = l;
}
}
large = max;
small = min;
while (small) { // While small is not 0
// Calculates GCD
rem = large % small;
large = small;
small = rem;
}
gcd = large;
// printf("GCD(%d,%d)= %d", large, small, gcd);
}
/*if (gcd == 1) {
achilles = n;
// printf("%d\n", achilles);
}*/
printf("GCD(%d,%d)= %d", max, min, gcd);
//printf("%d\n", achilles);
}
I am trying to show the sum of the prime factors of a given number and
I'm having difficulties displaying the prime factors in my output.
Sample Output:
Input number: 6
Factors are: 1 2 3
Sum of its factor: 1 +2 +3 =6
I am able to show the sum but I want to show the 1+2+3=6 like in the sample above where the factors are 1 2 3.
Can you help me correct my syntax to achieve this? Thanks in advance.
Here's my code:
#include <stdio.h>
int main() {
int i, j, num, isPrime, sum;
printf("Input number: ");
scanf("%d", &num);
printf("Factors are: ", num);
for (i = 1; i <= num; i++) {
if (num % i == 0) {
isPrime = 1;
for (j = 2; j <= i / 2; j++) {
if (i % j == 0) {
isPrime = 0;
break;
}
}
if (isPrime == 1) {
printf("%d ", i);
sum += i;
}
}
}
printf("\nSum of its factor : %d", sum);
return 0;
}
Your code actually has undefined behavior because sum is not initialized to 0. It produces the correct sum only by chance.
You can store the factors in an array, or even construct the expression as you go with sprintf. The maximum length of the expression is not very large as there can be at most 9 different prime factors (29!! > 232)
Here is a modified version:
#include <stdio.h>
int main() {
char expr[9 * 11 + 1];
int i, j, pos, num, isPrime, sum;
printf("Input number: ");
if (scanf("%d", &num) != 1)
return 1;
printf("Factors are: 1"); // always include 1
pos = 0;
expr[pos] = '\0';
sum = 1;
for (i = 2; i <= num; i++) {
if (num % i == 0) {
isPrime = 1;
for (j = 2; j * j <= i; j++) {
if (i % j == 0) {
isPrime = 0;
break;
}
}
if (isPrime == 1) {
pos += sprintf(expr + pos, "+%d", i);
printf(" %d", i);
sum += i;
}
}
}
printf("\nSum of its factors: 1%s = %d\n", expr, sum);
return 0;
}
Output:
Input number: 6
Factors are: 1 2 3
Sum of its factors: 1+2+3 = 6
Here is a more robust and much faster version that does not have undefined behavior for very large values of num:
#include <stdio.h>
int main() {
char expr[9 * 11 + 1];
int i, pos, num;
unsigned sum;
printf("Input number: ");
if (scanf("%i", &num) != 1)
return 1;
printf("Factors are: 1"); // always include 1
pos = 0;
expr[pos] = '\0';
sum = 1;
for (i = 2; num / i >= i; i++) {
if (num % i == 0) {
pos += sprintf(expr + pos, "+%d", i);
printf(" %d", i);
sum += i;
do { num /= i; } while (num % i == 0);
}
}
if (num != 1) {
pos += sprintf(expr + pos, "+%d", num);
printf(" %d", num);
sum += num;
}
printf("\nSum of its factors: 1%s = %u\n", expr, sum);
return 0;
}
Test:
Input number: 0x7fffffff
Factors are: 1 2147483647
Sum of its factors: 1+2147483647 = 2147483648
Since you want to print all the prime factors twice, you should do that in a way so that you can avoid duplicated code. Here is an idea:
#include <stdio.h>
/* Return the smallest prime that is smaller than or equal to n */
/* Assumes that the argument is greater than 1 */
int getFirst(int n)
{
int i;
for(i = 2; i <= n; i++)
if(n % i == 0)
return i;
}
int main()
{
int num, x, tmp, sum=0;
scanf("%d", &num);
tmp = num;
printf("Factors are: ");
while(1) {
x = getFirst(tmp);
printf("%d ", x);
if (x == tmp) /* If we are at the last prime */
break;
tmp /= x;
}
printf("\n");
printf("Sum of factors is: ");
tmp = num;
while(1) {
x = getFirst(tmp);
printf("%d ", x);
sum += x;
if(x == tmp) /* If we are at the last prime */
break;
printf("+ ");
tmp /= x;
}
printf("= %d\n", sum);
}
But as has been pointed out in the comments. 1 is not a prime, and that's why I excluded it.
The function "strike" has to return the number of times that the user's input are equivalent.
Assume, the random number is 1234.
if the user's input has one of the random's numbers, then strike1++.
e.g., if my input is 5152 then strike1 will be 2.
if my input is, 1112, then strike will be again 2.
I'm getting wrong output in strike1. Any ideas how to solve it?
(I don't want to solve it with arrays)
Solution:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
int hit(int num);
int strike(int num);
int rndNum(int num);
void main()
{
int num = 0;
int chosenNum;
int saveHits, saveStrikes;
srand(time(NULL));
printf("The Random number: %d", chosenNum = rndNum(num));
printf("\nPlease enter a 4 digit number: ");
scanf("%d", &num);
saveHits = hit(num, chosenNum);
saveStrikes = strike(num, chosenNum);
printf("\nThe number of hits: %d", saveHits);
printf("\nThe number of strikes: %d", saveStrikes);
getch();
}
int rndNum(int num)
{
int rndNum = rand() % 9000 + 1000;
return rndNum;
}
int hit(int num1, int chosenNum1)
{
int i, hit1 = 0;
for (i = 0; i < 4; i++)
{
if (num1 % 10 == chosenNum1 % 10)
hit1++;
num1 /= 10;
chosenNum1 /= 10;
}
return hit1;
}
int strike(int num1, int chosenNum1)
{
int i, strike1 = 0, n = 1;
int temp = num1, temp2 = chosenNum1;
for (i = 0;i < 4;i++)
{
while (n > 0)
{
if (temp > 0)
{
if ((temp % 10 == temp2 % 10))
{
strike1++; n--;
}
else
temp /= 10;
}
else
n--;
}
temp2 /= 10;
n = 1;
temp = num1;
}
return strike1;
}
int strike(int num1, int chosenNum1)
{
int i, strike1 = 0, n = 1;
int temp = num1, temp2 = chosenNum1;
for (i = 0;i < 4;i++)
{
while (n > 0)
{
if (temp > 0)
{
if ((temp % 10 == temp2 % 10))
{
strike1++; n--;
}
else
temp /= 10;
}
else
n--;
}
temp2 /= 10;
n = 1;
temp = num1;
}
return strike1;
}
int a,b,n;
printf("Input Natural Number n (n<2,100,000,000) : ");
scanf("%d",&n);
for(a=1;a<=100;a++)
for(b=1;b<=100;b++)
if(a<b && a*a + b*b == n*n)
{
printf("(%d, %d, %d)\n",a,b,n);
}
/*else
{
printf("impossible \n");
}
*/
return 0;
if I delete 'else' the program runs correctly. But I want to make another function which can check the number has pythagorean numbers or not by using 'else' paragraph. But when I put 'else' paragraph in that code, the result is dizzy.... plz help me!!
Put braces around the nested code blocks.
int a, b, n;
int impossible = 1;
printf("Input Natural Number n (n<2,100,000,000) : ");
scanf("%d", &n);
for (a = 1; a <= 100; a++) {
for (b = 1; b <= 100; b++) {
if (a < b && a * a + b * b == n * n) {
printf("(%d, %d, %d)\n", a, b, n);
impossible = 0;
}
}
}
if (impossible == 1) printf("impossible \n");
return 0;
Here is a possible answer
#include <stdio.h>
int power(int base, int power);
int main(){
int N;
printf("INput the Num: ");
scanf("%d", &N);
int a, b, c;
for(a = 0; a < N ; a++) {
for(b = 0; b< N; b++) {
if ((a < b) && (b < N - a - b)) {
if (power(a, 2) + power(b, 2) == power(N - a - b, 2)) {
printf("%d^2 + %d^2 = %d^2 \n", a, b, N-a-b);
}
}
}
}
}
int power(int base, int power) {
int result = 1;
for(int i = 0; i < power ; i++) {
result *= base;
}
return result;
}