Like others here, I am trying to make a program called greedy that will tell me the least amount of coins that I have to give someone as change for a given amount of money. I made this but it gives me the wrong amount of coins and I don't know why :(
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void)
{
float given_amount;
int cent_count;
int coin_count = 0;
do
{
printf("What is the amount of change I owe you?\n");
given_amount = GetFloat();
}
while (given_amount < 0);
cent_count = (int)round(given_amount * 100);
while (cent_count > 25)
{
coin_count++;
cent_count -= 25;
}
while (cent_count > 10)
{
coin_count++;
cent_count -= 10;
}
while (cent_count >= 1)
{
coin_count++;
cent_count -= 1;
}
printf("Take these %d coins\n", coin_count);
}
If I tell the program I need to give back 25 cents the program tells me I have to give the person 7 coins, but it should just tell me I have to give him one coin... a quarter.
Your first 2 loops don't take care of value 25 and 10.
In fact your algorithm is giving you 7, that is: 2 coins of 10 cent, and 5 coins of 1 cent.
You must check for them, so:
while (cent_count >= 25)
{
coin_count++;
cent_count -= 25;
}
while (cent_count >= 10)
{
coin_count++;
cent_count -= 10;
}
Here's my code:
#include <cs50.h>
#include <stdio.h>
#include <math.h>
int main(void)
{
float input = -1.00;
int n = 0;
int c = 0;
printf("O hai! How much change is owed?: ");
do
{
input = GetFloat();
if(input < 0)
printf("Positive number, please!: ");
}
while (input < 0);
c = roundf(input * 100);
while(c >= 25)
{
c = c - 25;
n++;
}
while (c >= 10)
{
c = c - 10;
n++;
}
while (c >= 5)
{
c = c - 5;
n++;
}
n = n + c;
printf("%i\n", n);
}
Related
I am learning C and I need a help here. I'm just working on a little program from my course's exercise, and I need to use my variable (that is inside of an for loop) outside of the loop. I know its a very dumb question, but I need your help. Here's the code I wrote, in the CS50 IDE:
#include <stdio.h>
#include <cs50.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
int main(void)
{
// Verifies if the card_number is between 13 and 16 digits
long card_number = 0;
for (int i = 0; i < 20; i++)
{
card_number = get_long("Insert the card's number: ");
int reach_zero = 0;
int digit_count = 0;
do
{
reach_zero = (card_number /= 10);
digit_count++;
}
while (reach_zero != 0);
if (digit_count >= 13 && digit_count <= 16)
{
break;
}
}
// Prints the card_number
printf("%li\n", card_number);
}
I just need to printf the card_number.
If I have understood the question then just write
card_number = get_long("Insert the card's number: ");
long reach_zero = card_number;
int digit_count = 0;
do
{
reach_zero /= 10;
digit_count++;
}
while (reach_zero != 0);
I'm trying to solve a problem (as the title already state). I've actually learned that I can do it with modulo operator (%). But the first code that I wrote is using the while-loop, so I'm trying to finish the code.
This is the code
int main()
{
char arr[1000000];
int i = 0;
int sum = 0;
printf("type the number = ");
scanf("%s", arr);
while(arr[i] != '\0'){
sum = arr[i] + sum;
i++;
}
printf("the total number is = %d", sum);
so the problem is it's actually printing out some huge amount of number.. I guess it's because of the array is in char, can someone help me how do I changed the value into int ?
You need to substract from the digit code the code of '0'.
Here you have the both versions (I have added some logic to accept the numbers with + & - at there beginning):
int sumdigitsStr(const char *num)
{
int sum = 0;
int first = 1;
while(*num)
{
if(isdigit(*num)) {sum += *num - '0'; first = 0;}
else
if(first && (*num == '-' || *num == '+'))
{
first = 0;
num++;
continue;
}
else
{
sum = -1; break;
} //error string contains non digits
num++;
}
return sum;
}
int sumdigits(long long num)
{
int sum = 0;
do
{
sum += abs((int)(num % 10));
}while((num = num / 10));
return sum;
}
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
int d, sum = 0;
while (n != 0)
{
d = n % 10;
sum = sum + d;
n = n / 10;
}
printf("sum of digits is : %d", sum);
return 0;
}
I'm a first year collage student and I'm working on an exercise with a while function.
my current objective is to identify the lowest and highest value digits in a given number.
this is my code so far:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
void main() {
printf("enter an integer\n");
int one,onemax,onemin, min=9, max=0;
scanf("%d", &one);
onemax = one;
onemin = one;
while (onemax >= 0) {
if (max <= onemax % 10) {
max = onemax % 10;
onemax=onemax / 10;
if (onemax == 0) {
break;
}
}
else {
onemax=onemax / 10;
}
}
while (onemin != 0) {
if (min >= onemin % 10) {
min = onemin % 10;
onemin=onemin / 10;
if (onemin == 0) {
break;
}
}
else {
onemin=onemin / 10;
}
}
printf("min = %d max = %d", min, max);
}
now I know I'm stuck in a loop here and I need help getting out.
when onemax reaches 0 you are done. You should change the >= condition to >:
while (onemax > 0) {
// Here ------^
I am writing a program to solve the problem of changed owed after a customer purchases goods.
The changes are meant to be mostly in floating variable like 0.50 for 50cents and that works fine but for situation where the change owed are in whole number like 10 and 9 the program will not carry out necessary operations
I have converted the input from float to input but to no avail
#include <cs50.h>
#include <stdio.h>
#include <math.h>
int change(void);
int main()
{
float num;
int y;
int counter = 0;
do
{
num = get_float("Change owed: ");
num = round(num * 100);
y = (int) num;
printf("%d\n", y);
}
while(num <= 0);
while(y>0)
{
if(y > 25)
{
y-=25;
counter++;
}
else if(y < 25 && y >= 10)
{
y-=10;
counter++;
}
else if(y < 10 && y >= 5)
{
y-=5;
counter++;
}
else if(y < 5 && y >= 1)
{
y-=1;
counter++;
}
else
{
return 0;
}
}
printf("%d \n", counter);
}
I want to be able to run the program for all numeric data types
I tried to printf all the narcissistic numbers of the number of digits entered by the user.
For example for input 3 the program should print: 153, 370, 371, 407. Now for some reason instead of printing the numbers it prints nothing and the program is stuck.
#include <stdio.h>
#include <math.h>
int main() {
int digit, a, c = 0;
unsigned long long int count, b, sum;
printf("Enter digits to check narcisistic: ");
scanf("%d", &digit);
count = pow(10, digit - 1);
if (digit > 2) {
while (count < pow(10, digit)) {
b = count;
sum = 0;
while (count >= 1) {
a = b % 10;
b /= 10;
sum += pow(a, digit);
}
if (sum == count) {
printf("\n Narcissistic found:\t%llu", count);
c++;
}
count++;
}
if (c == 0)
printf("No Narcissistic number for this digit.");
}
return 0;
}
What is the problem of this code?
while(count>=1){
a=b%10;
b/=10;
sum+=pow(a,digit);
}
count never changes in this loop, so it will loop forever.
As per #dcp's answer.
The inner while loop will never exit. What you are supposed to be looping over is the number of digits. For instance (after declaring digNum as an int earlier):
for(digNum = digit; digNum > 0; digNum--)
{
a = b%10;
b /= 10;
sum+=pow(a,digit);
}