Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
Program Details: Fisher Number
A Fisher number is an integer for which the product of its factors (including itself) is equal to the number's cube. For example, 12 is a Fisher number, because 123 = 2 x 3 x 4 x 6 x 12.
Example:
Input: 12
Output: true (12<sup>3</sup> = 2 x 3 x 4 x 6 x 12)
Input: 8
Output: false (8<sup>3</sup> != 2 x 4 x 8)
Objectives:
Write a program to check if the user input is a Fisher number or not.
Print all the Fisher numbers in a given range
Here is my code:
#include <stdio.h>
#include <conio.h>
int ch, product = 1, cube, num, i, j, min, max;
// Check Function
void check() {
int i;
printf("Enter the Number to check whether It is Fisher or Not\n");
scanf("%d", &num);
cube = num * num * num;
for(i = 2; i <= num; i++) {
if(num % i == 0) {
product = product * i;
}
}
if(cube == product) {
printf("It is a Fisher Number!\n");
}
else {
printf("It is not a Fisher Number\n");
}
}
// RANGE FUNCTION
void range() {
printf("Enter the Minimum and Maximum value of Range\n");
scanf("%d%d", &min, &max);
for(i = min; i <= max; i++) {
cube = i * i * i;
for(j = 1; j <= i; j++) {
if(i % j == 0) {
product = product * i;
}
}
if(cube == product) {
printf("%d\n", i);
}
}
}
void main() {
clrscr();
printf("Enter Your Choice \n");
printf("1 - Check Fisher Number \n");
printf("2 - Display Fisher Numbers in a Range \n");
scanf("%d", &ch);
switch(ch) {
case 1: check();
break;
case 2: range();
break;
default: printf("Enter Valid Choice \n");
}
getch();
}
Output picture and text:
Enter Your Choice
1 - Check Fisher Number
2 - Display Fisher Numbers in a Range
2
Enter the Minimum and Maximum value of Range
1 40
1
I am not getting the range output right. For example, if the range is set to 1 to 15, it only shows 1 which is wrong!
You should multiply by j here:
if(i%j == 0) {
product = product * i;
}
It would be more obvious if you used more meaningful variable names than i and j.
You also need to reset product variable before the inner loop (why is it a global variable anyway? Don't you know they are evil?)
cube = i * i * i;
product = 1;
for(j=1;j<=i;j++) { ...
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I'm just trying to understand how this minimum power of 2 algorithm works, in particular, how this piece of code, result *= 2, returns the minimum power of 2 from the number entered by the user. I cannot seem to figure it out, even when I write it out manually. Any help to clarify would be greatly appreciated!
int main()
{
int userInput, result;
do
{
printf("Enter a number (-1 to exit): ");
scanf("%d", &userInput);
if (userInput > 0)
{
result = 1;
while (result < userInput)
{
result *= 2;
}
printf("Minimum power of 2 greater than %d: %d\n", userInput, result);
}
} while (userInput != -1);
return EXIT_SUCCESS;
}
Output when 3 is entered: Min.....3: 4
Output when 5 is entered: Min.....5: 8
There are no problems with the implementation of this algorithm[1]. This algorithm returns the least power of 2, provided it is greater than or equal to the variable userInput:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int userInput, result;
do{
printf("Enter a number (-1 to exit): ");
scanf("%d", &userInput);
if (userInput > 0){
result = 1;
int counter = 0;
while (result < userInput){
result *= 2;
printf("Result: %d\t Counter: %d\n", result, counter++);
}
printf("Result: %d ^ %d = %d\n", 2, counter, (int) pow(2, counter));
printf("Minimum power of 2 greater than %d: %d\n", userInput, result);
}
} while (userInput != -1);
return EXIT_SUCCESS;
}
In the above application, if the user enters the value 11, the following result is produced:
Enter a number (-1 to exit): 11
Result: 2 Counter: 0
Result: 4 Counter: 1
Result: 8 Counter: 2
Result: 16 Counter: 3
Result: 2 ^ 4 = 16
Minimum power of 2 greater than 11: 16
1 - Smallest power of 2 greater than or equal to n
Change to this:
while (result < userInput)
{
printf("Before: %d\n");
result *= 2;
printf("After: %d\n\n");
}
And you'll see what's going on.
...
result = 1;
while (result < userInput)
{
result *= 2;
}
...
Every time result is tested and it's lesser than userInput, result is doubled.
In the case of 5 as userInput
iterations
result
userInput
at start
1
5 (>result so continue looping)
iteration 1
2
5 (>result so continue looping)
iteration 2
4
5 (>result so continue looping)
iteration 3
8
5 (<result so loop stops)
I have written a program to calculate compound interest.
Here is the code:
#include <stdio.h>
#include <math.h>
int main(void) {
float value, rate, years,r;
int column = 0, tmp;
printf("Enter money values: ");
scanf("%f",&value);
printf("Enter a interest rate: ");
scanf("%f",&rate);
printf("Enter number of years: ");
scanf("%f",&years);
printf("\nYears ");
tmp = rate + 4;
r = rate;
for (int a = rate; a <= tmp; a++) {
printf(" %d ", a);
column++;
}
for (int b = 1; b <= column; b++) {
printf("\n %d",b);
for (int i = 1; i<= column; i++) {
printf(" %.2f ", (float) pow ( (value)*(1.0+((r/100.0)/(1.0))) , (1.0*b))
r++;
}
r = rate;
printf("\n");
}
// I = P*R*T
// P= AMOUNT (value)
// R=RATE (r)
// T=YEARS (b)
return 0;
}
It asks the user for a value (money), interest rate, number of years and displays the interest rate like so:
Enter money values: 100
Enter a interest rate: 6
Enter number of years: 5
Years 6 7 8 9 10
1 106.00 107.00 108.00 109.00 110.00
2 11236.00 11449.00 11664.00 11881.00 12100.00
3 1191016.00 1225043.00 1259712.00 1295029.00 1331000.00
4 126247696.00 131079600.00 136048896.00 141158160.00 146410000.00
5 13382255616.00 14025517056.00 14693280768.00 15386240000.00 16105100288.00
But my problem is the floating point calculation.
As you may be able to tell the numbers in the output above and very long and have many trailing digits
which i am very confused about.
For example in the 2nd row the first output is 11236.00,
this is wrong since it should be outputting 112.36 but for some reason the decimal has moved
forward two spaces. Why is this? and how could i fix this problem and print the correct solution
with the decimal in the correct place.
You have the value inside the pow. So when you square for two years, you are squaring the amount. Move the (value)* to output the pow call.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am trying to figure out how to get the prime number n, and calculate the sum of cubes for that n (1^3 + 2^3 + ... + n^3). So far I can figure out how to get the primes. I just can't figure out how to get that same n to calculate its primes. This is what I have so far:
#include <stdio.h>
int main() {
int n, i, c = 0
printf("Enter any number n: ");
scanf("%d", &n);
for(i=2; i<=n/2; i++){
if(n%i == 0){
c=1;
break;
}
}
if (c==0)
printf(%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
printf("Enter that same number n: ");
scanf("%d", &num);
int num, cube, sum = 0, j=1;
while (j <= num) {
cube = j*j*j;
sum = sum + cube;
j++;
}
printf("sum of cubes of %d is %d\n", num, sum);
return 0:
}
I get an error on the second scanf because it says num is undeclared. What should I do to fix this situation.
Use scanf after declaring int num. Also you have used % in 2nd num. Use
int num, cube, sum = 0, j=1;
scanf("%d", &num);
instead of
scanf("%d", %num);
int num, cube, sum = 0, j=1;
Consider this starting piece
long double sumcubs(int n)
{
int i;
long double ret = 0;
for (i = 1; i <= n; ++i)
ret += (i * i * i);
return ret;
}
Once you have encapsulated this sum of cubes function, it should become easier to organize and write the rest of your code.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm not sure what i'm doing wrong but the for loop is not initializing
The code just goes immediately to displaying the printfs. That have no values in them since the for loop didn't activate
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define PAUSE system("Pause")
main() {
// INITALIZE VARIABLES
int number = 0;
int i = 0;
int odd = 0;
int even = 0;
int totalNum = 0;
int tempNum = 0;
int count;
printf("Enter a number between 2 and 25\n");
scanf("%i", &number);
do{
if (number < 2 || number > 25)
printf("That was an invalid number please try again\n");
scanf("%i", &number);
} while (number < 2 || number > 25);
printf("Enter how many numbers you want to input\n");
scanf("%i", &count);
for (i = 1; i == count; ++i){
printf("input numbers\n");
scanf("%i", &tempNum);
if (tempNum % 2 == 0)
even++;
else
odd++;
totalNum = totalNum + tempNum;
} // END FOR LOOP
// DISPLAY OUTPUT
printf("You entered %i numbers\n", count);
printf("The sum of the %i numbers is %i\n", count, totalNum);
printf("The average of the %i numbers is %i\n", count, totalNum / count);
printf("You entered %i odd numbers and %i even numbers\n", odd, even);
PAUSE;
} // END MAIN
Your loop will only execute at best once, when count == 1 as you initialize i to 1.
If you enter a 1 for count,
printf("Enter how many numbers you want to input\n");
scanf("%i", &count);
the loop will run exactly once, until i increments to 2
You probably want:
for (i = 1; i <= count; ++i){
do{
if (number < 2 || number > 25)
printf("That was an invalid number please try again\n");
scanf("%i", &number);
} while (number < 2 || number > 25);
it should be...
do{
if (number < 2 || number > 25){
printf("That was an invalid number please try again\n");
scanf("%i", &number);
}
} while (number < 2 || number > 25);
else it asks always another number
i = 1, so i == count; gives false therefore the loop is ignored.
A for loop in C works like this:
for ( variable initialization; condition; variable update ) {
/* Do something... */
}
The loop will execute for as long as condition is true. So, when you do:
for (i = 1; i == count; ++i)
The loop will execute for as long as i == count is true. So, unless count holds 1 when this line is executed, the loop will never run.
As others pointed out, you probably want this:
for (i = 1; i <= count; ++i)
So your loop will run for all values of i, until it reaches count.
As a side note i should point out that the usual way to write for loops in C is something like this:
for (i = 0; i < count; i++)
We start with i = 0 because C arrays are zero-based, so the Nth element of an array has index n-1
You were so close. In addition to fixing your loop test clause for (i = 1; i <= count; i++), I would suggest using " %d" for your format specifier. Your do loop need only be a while loop to avoid printing your invalid number message every time.
Additionally, While not an error, the standard coding style for C avoids caMelCase variables in favor of all lower-case. See e.g. NASA - C Style Guide, 1994.
With those changes, (and changing your odd/even check to a simple &) you could write your code as follows.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
// #define PAUSE system("Pause")
int main (void)
{
int number, i, odd, even, totalnum, tempnum, count;
number = i = odd = even = totalnum = tempnum = count = 0;
printf ("enter a number between 2 and 25: ");
scanf (" %d", &number);
while (number < 2 || number > 25) {
printf ("invalid number, again (between 2 and 25): ");
scanf (" %d", &number);
}
printf ("numbers to input: ");
scanf (" %d", &count);
for (i = 1; i <= count; i++) {
printf ("input number %2d: ", i);
scanf (" %d", &tempnum);
if ((tempnum & 1) == 0)
even++;
else
odd++;
totalnum = totalnum + tempnum;
}
printf ("You entered %d numbers\n", count);
printf ("The sum of the %d numbers is %d\n",
count, totalnum);
printf ("The average of the %d numbers is %d\n",
count, totalnum / count);
printf ("You entered %d odd numbers and %d even numbers\n",
odd, even);
// PAUSE;
return 0;
}
note: main is type int (e.g. int main (int argc, char **argv) or simply int main (void) to indicate no arguments taken). Since it is type int it will return a value to the shell. While historic implementations may have allowed void main that is no longer the case for portable code.
Example Use/Output
$ /bin/forskipped
enter a number between 2 and 25: 4
numbers to input: 4
input number 1: 1
input number 2: 2
input number 3: 3
input number 4: 4
You entered 4 numbers
The sum of the 4 numbers is 10
The average of the 4 numbers is 2
You entered 2 odd numbers and 2 even numbers
Look it over and let me know if you have any questions.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I try to make this program when I enter 3 5 2 5 5 5 0
=>
Enter numbers: 3 5 2 5 5 5 0
The largest number is 5
The occurrence count of the largest number is 4
int main()
{
int a[10];
int i,max, count;
printf("Enter numbers: ");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
max=a[0];
count=1;
for(i=1;i<10;i++)
{
if(max<a[i])
{
count = 1;
max = a[i];
}
else if(max==a[i])
{
count++;
}
}
printf("The largest number is %d\n",max);
printf("The occurrence count of the largest number is %d",count);
return 0;
}
It is my code, but it is totally wrong..
I don't know what should I do
please help me
You've got several errors.
In your first loop, you want to read into a[i], not a[10].
Your code corrently assumes you always type in 10 numbers. It looks like you wanted a value of 0 to end the list. Me, I'd use end-of-file to end the list. To check for 0 you need an extra line if(a[i] == 0) break; in the loop. To check for EOF (or other non-numeric input which will cause problems) you could check to see that scanf returns 1.
In case you enter less than 10 numbers, you'll need a new variable that knows how many variables you actually entered. I set nn = i after the first loop.
Then you just need to change the second loop to run from 1 to nn, not 10.
Putting this all together, we have:
#include <stdio.h>
int main()
{
int a[10];
int i,max, count, nn;
printf("Enter numbers: ");
for(i=0;i<10;i++)
{
if(scanf("%d",&a[i]) != 1) break;
if(a[i] == 0) break;
}
nn = i;
max=a[0];
count=1;
for(i=1;i<nn;i++)
{
if(max<a[i])
{
count = 1;
max = a[i];
}
else if(max==a[i])
{
count++;
}
}
printf("The largest number is %d\n",max);
printf("The occurrence count of the largest number is %d\n",count);
return 0;
}
This seems to work.
Code is OK other than the user needs to enter 10 numbers.
There is no need to store previous numbers, so code can simply examine each new number until '\n' is encountered. Just keep look for the end-of-line before looking for the number.
#include <limits.h>
#include <stdio.h>
#include <ctype.h>
int main(void) {
int count = 0;
int max = INT_MIN;
printf("Enter numbers: ");
for (;;) {
int ch;
int num;
while (isspace(ch = fgetc(stdin)) && ch != '\n') {
;
}
if (ch == '\n' || ch == EOF) break;
ungetc(ch, stdin);
if (scanf("%d", &num) != 1) break;
if (num >= max) {
count++;
if (num > max) {
max = num;
count = 1;
}
}
}
printf("The largest number is %d\n", max);
printf("The occurrence count of the largest number is %d", count);
return 0;
}