In output, new_dollars always displays the number of dollars, so I don't know how to solve the problem. Please help me figure out what the problem is.
int main(void)
{
int dollars, cents, count, new_dollars;
for ( count = 1; count <= 10; ++count){
printf ("Enter dollars: ");
scanf ("%i", &dollars);
printf ("Enter cents: ");
scanf ("%i", ¢s);
if ( cents >= 100 ){
cents = cents % 100;
new_dollars = dollars + cents / 100;
printf ("%i\n", new_dollars);
printf ("$%i.%2i\n\n", new_dollars, cents);
}
else {
printf ("$%i.%2i\n\n", dollars, cents);
}
}
return 0;
}
The commenters are right about integer division. Try this, it has the order two lines that do the calculation swapped, so cents will still have a useful value when you need it to:
int main(void)
{
int dollars, cents, count, new_dollars;
for ( count = 1; count <= 10; ++count){
printf ("Enter dollars: ");
scanf ("%i", &dollars);
printf ("Enter cents: ");
scanf ("%i", ¢s);
if ( cents >= 100 ){
new_dollars = dollars + cents / 100;
cents = cents % 100;
printf ("%i\n", new_dollars);
printf ("$%i.%2i\n\n", new_dollars, cents);
}
else {
printf ("$%i.%2i\n\n", dollars, cents);
}
}
return 0;
}
convert "int new_dollars" into "float new_dollars";
new_dollars = (float) dollars + (float) cents / 100;
printf ("$%.2f\n", new_dollars);
Related
#include <stdio.h>
#include <cs50.h>
int cents;
int main(void)
{
do
{
// Prompt the user for input
cents = get_int("Change owed: ");
}
while (cents <= 0);
return cents;
int count_coins();
}
int count_coins() // count the amount of coins
{
int coins;
for(coins = 0; cents == 0; coins++)
{
// count quarters
if (cents >= 25)
{
cents = cents - 25;
return coins;
}
// count dimes
else
if (cents >= 10)
{
cents = cents - 10;
return coins;
}
// count nickels
else
if (cents >= 5)
{
cents = cents - 5;
return coins;
}
// count pennies
else
if (cents >= 1)
{
cents = cents - 1;
return coins;
}
while (cents == 0)
{
// print total amount of coins
printf("%i" , coins);
}
}
return cents;
}
I'm trying to do the cs50 pset 1 problem cash. When I run this code, it asks for an input and successfully keeps asking for input until I give it a positive number, but after that, it doesn't print the number of coins. This is my first-time programming anything, so any constructive criticism is more than welcome.
This is the full code, the dig variables are user inputs
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
int dig1=0;
int dig2=0;
int dig3=0;
num=dig1,dig2,dig3;
fflush(stdin);
printf("Please enter the first digit of your three digit number:");
scanf("%d", &dig1);
fflush(stdin);
printf("Please enter the second digit of your three digit number:");
scanf("%d", &dig2);
fflush(stdin);
printf("Please enter the third digit of your three digit number:");
scanf("%d", &dig3);
if (num==(dig1*dig1*dig1)+(dig2*dig2*dig2)+(dig3*dig3*dig3))
{
printf("Your number is an Armstrong number!\n");
}
else
{
printf("Your number is not an Armstrong number!\n");
}
system("pause");
return 0;
}
How could I make the variable "num" equal to all of the inputs for "dig1", "dig2", and "dig3". As in if dig 1 was 2 and dig 2 was 4 and dig 3 was 6, num would be 246. Please help!
Your problem can be solved this way:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int num;
int dig[3];
int i,j = 100;
int result = 0;
for(i = 0;i<3;i++){
fflush(stdin);
printf("Please enter the %d digit of your three digit number:",i+1);
scanf("%d", &dig[i]);
}
for(i = 0;i<3;i++){
result += pow(dig[i],3);
num += dig[i] * j;
j/=10;
printf("%d * %d = %d\n",dig[i],j,num);
}
if (num == result)
{
printf("Your number is an Armstrong number!\n");
}
else
{
printf("Your number is not an Armstrong number!\n");
}
system("pause");
return 0;
}
You could multiply these numbers according to their significant value:
int num = dig1 + dig2 * 10 + dig3 * 100;
I have to create a recursive function that tells you the number of ways a number of cents can be made into change. (Using quarters, dimes nickels, and pennies).
So far, I have a recursive function that does that, however it counts the same combination more than once, so the number is too big. How do I remove the duplicate combinations?
Code:
#include <stdio.h>
//Prototypes
int coins(int);
int main(void){
//Declarations
int num;
//Get user input
printf("Enter an amount of change in cents: ");
scanf("%d", &num); //Change to fgets
//Call function
printf("There are %d ways to make change for %d cents.\n", (coins(num)), num);
}
int coins(int amt){
//Declarations
int ways=0;
//Base Case
if(amt == 0){
return 1;
}
//int ways=0; More efficient after base case.
if(amt >= 1){
ways+=coins(amt-1);
}
if(amt >= 5){
ways+=coins(amt-5);
}
if(amt >= 10){
ways+=coins(amt-10);
}
if(amt >= 25){
ways+=coins(amt-25);
}
return ways;
}
Example:
Input: 17 (cents)
Output: 80 ways
**Output should be 6
#include <stdio.h>
int coins(int, int);
int main(void){
int num;
printf("Enter an amount of change in cents: ");
scanf("%d", &num);
printf("There are %d ways to make change for %d cents.\n", coins(num, 0), num);
return 0;
}
int coins(int amt, int kind){
static int kinds[4] = {25, 10, 5, 1};
int ways=0, i, n;
if(kinds[kind] == 1)//always divisible
return 1;
n = amt / kinds[kind];
for(i = 0; i <= n; ++i)
ways+=coins(amt-kinds[kind]*i, kind + 1);
return ways;
}
So I'm working on this assignment. I tried running this code, and it ceases to work after reading in the second number. Here is a synopsis of it:
Write a C program that prompts the user to enter two positive integers. Your program should then display the number of carry operations that result from adding the two numbers and print the result of the addition. The input continues until the user enters 0 for the first number.
For instance, if the input is 123 and 456, there are 0 carry operations. If the input is 666 and 777, there are 3 carry operations.
Here's my code (nothing happens once it gets to the while loop):
#include <stdio.h>
#include <string.h>
// ------------------------
char dummy;
char response = 'y';
int userNum1, userNum2, sum;
int carry = 0;
int factor = 1;
int digit1 = 1;
int digit2 = 1;
int main(void)
{
while (response == 'y' || response == 'Y')
{
printf ("Enter the first number: ");
scanf ("%d", &userNum1);
printf ("Enter the second number: ");
scanf ("%c", &dummy);
scanf ("%d", &userNum2);
sum = userNum1 + userNum2;
while (userNum1 > 0 && userNum2 > 0)
{
while (digit1 > 0 && digit2 > 0)
{
digit1 = getNum (userNum1);
digit2 = getNum (userNum2);
factor = factor * 10;
carryTheOne (digit1, digit2);
}
}
printf ("The sum is %d and there were %d carry operations", sum, carry);
printf("\n");
printf ("Do you want to do another one?");
scanf ("%c", &dummy);
scanf ("%c", &response);
scanf ("%c", &dummy);
}
return 0;
}
int getNum(int num)
{
num = ((num % (factor * 10)) / factor);
return num;
}
int carryTheOne (int num1, int num2)
{
if ((digit1 + digit2) > 9)
carry++;
return carry;
}
Can someone help me here and let me know what I'm doing wrong? It just prints a blank line after reading the second number and never does anything else (doesn't terminate, just doesn't do anything).
EDIT: I changed the while loop to this:
while (userNum1 > 0 && userNum2 > 0)
{
while (digit1 > 0 && digit2 > 0)
{
digit1 = getNum (userNum1);
digit2 = getNum (userNum2);
factor = factor * 10;
carryTheOne (digit1, digit2);
userNum1 = (userNum1 - digit1) / 10;
userNum2 = (userNum2 - digit2) / 10;
}
}
It worked for 123 and 456, but didn't work for 666 and 777. Any reason why?
Remove that factor variable. I have made for edits:
1.Comment out
int factor = 1;
2.comment out
factor = factor * 10;
3.change
userNum1 = (userNum1 - digit1) / 10;
userNum2 = (userNum2 - digit2) / 10;
to
userNum1 = userNum1 / 10;
userNum2 = userNum2 / 10;
4.change:
int getNum(int num)
{
num = ((num % (factor * 10)) / factor);
return num;
}
to
int getNum(int num)
{
num = num % 10;
return num;
}
I have to create a recursive function that tells you the number of ways a number of cents can be made into change. (Using quarters, dimes nickels, and pennies).
So far, I have a recursive function that does that, however it counts the same combination more than once, so the number is too big. How do I remove the duplicate combinations?
Code:
#include <stdio.h>
//Prototypes
int coins(int);
int main(void){
//Declarations
int num;
//Get user input
printf("Enter an amount of change in cents: ");
scanf("%d", &num); //Change to fgets
//Call function
printf("There are %d ways to make change for %d cents.\n", (coins(num)), num);
}
int coins(int amt){
//Declarations
int ways=0;
//Base Case
if(amt == 0){
return 1;
}
//int ways=0; More efficient after base case.
if(amt >= 1){
ways+=coins(amt-1);
}
if(amt >= 5){
ways+=coins(amt-5);
}
if(amt >= 10){
ways+=coins(amt-10);
}
if(amt >= 25){
ways+=coins(amt-25);
}
return ways;
}
Example:
Input: 17 (cents)
Output: 80 ways
**Output should be 6
#include <stdio.h>
int coins(int, int);
int main(void){
int num;
printf("Enter an amount of change in cents: ");
scanf("%d", &num);
printf("There are %d ways to make change for %d cents.\n", coins(num, 0), num);
return 0;
}
int coins(int amt, int kind){
static int kinds[4] = {25, 10, 5, 1};
int ways=0, i, n;
if(kinds[kind] == 1)//always divisible
return 1;
n = amt / kinds[kind];
for(i = 0; i <= n; ++i)
ways+=coins(amt-kinds[kind]*i, kind + 1);
return ways;
}