Printing only one combination using nested loops - c

I have to print combination of different currency notes a man should have to pay to the cashier. Program first ask total amount to be paid and then notes of different denominations that he possesses. The problem is, I have to print only one combination of currency notes, but I am getting all combinations (as usual) by the following code.
#include <stdio.h>
int main(void)
{
int hundred,fifty,ten,total,hund,fif,t;
printf ("Enter amount to be paid:");
scanf ("%d",&total);
printf ("\n\nHow many currency notes do you have?\nEnter 100,50 and 10 Rupee Notes Respectively:\n");
scanf ("%d %d %d",&hund,&fif,&t);
printf ("\t\t\tPossible combination for Rs=%d/- \n",total);
for (hundred=0; hundred<=hund; hundred++)
{
for (fifty=0; fifty<=fif; fifty++)
{
for (ten=0; ten<=t; ten++)
{
if ((hundred*100+fifty*50+ten*10)==total)
{
printf("\n\n Hundred rupees notes=%d, 50 rupees notes=%d, 10 rupees notes=%d",hundred,fifty,ten);
}
}
}
}
getch();
return 0;
}

Add a getch(); and return 0; just after the printf inside the nested loops.
Another way is to use goto. Type
goto exit;
Just after the printf inside the nested loops and type
exit:
Just before the getch();.

Related

C program \n and while loop

guys I have a C program question. My goal is to make a change operation working for taking a price and giving out the current amount remaining. I code in java, so this is my first time in C, Im not looking to have my entire code done for me. I can do the code, however im having trouble getting it to execute the way I want. My problem here is that when I use \n my code seems to work, but my output is really weird, i have to add constant spaces and repeat my lines. not sure why this is happening, Also my while loop does not seem to execute, which is making no sense to me. If anyone could help , I would apprectiate it. Thank you for reading, ps I reply isntantly
/******************************************************************************
Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
/* Header comment that describes the purpose of the program
* Name Karanvir Dhillon
* Date Jan 12
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main (void) {
double tendered;
double changeDue;
double price;
int hundred=0;
int twenty=0;
int ten=0;
int five=0;
int toonoe=0;
int loonie=0;
int quarter=0;
int dime=0;
int nickle=0;
int penny=0;
/* Statements to be executed */
printf("Total purchase price and tendered amount\n");
scanf("%lf %lf ", &price, &tendered);
printf(" %lf and %lf is \n", tendered,price);
changeDue=tendered-price;
printf("%lf \n", changeDue);
if(tendered<price){
printf("Not enough money recieved as payment \n");
}
if(tendered==price){
printf("Exact payment, no change given \n");
}
if(tendered>price){
printf("%lf Amount to be paid is \n", changeDue);
}
while(changeDue!=0.00){
if(changeDue<=100.00){
changeDue=changeDue-100.00;
hundred=hundred+1;
}
if(changeDue<=20.00){
changeDue=changeDue-20.00;
twenty=twenty+1;
}
if(changeDue<=10){
changeDue=changeDue-10.00;
ten=ten+1;
}
if(changeDue<=5){
changeDue=changeDue-5.00;
five=five+1;
}
if(changeDue<=2){
changeDue=changeDue-2.00;
toonoe=toonoe+1;
}
if(changeDue<=1){
changeDue=changeDue-1.00;
loonie=loonie+1;
}
if(changeDue>1){
for(int i=0;i<changeDue;i++){
if(i==0.25&&changeDue>=0.25){
changeDue=changeDue-0.25;
quarter=quarter+1;
}
if(i==0.10&&changeDue>=0.10){
changeDue=changeDue-0.10;
dime=dime+1;
}
if(i==0.05&&changeDue>=0.05){
changeDue=changeDue-0.05;
nickle=nickle+1;
}
if(i==0.01&&changeDue<0.05){
changeDue=changeDue-0.01;
penny=penny+1;
}
}
}
}
if(hundred!=0){
printf("%d hundred$ bills given as change \n",hundred);
}
if(twenty!=0){
printf("%d twenty$ bills given as change \n",twenty);
}
if(ten!=0){
printf("%d ten$ bills given as change \n",ten);
}
if(five!=0){
printf("%d five$ bills given as change \n",five);
}
if(toonoe!=0){
printf("%d toonie coins given as change \n",toonoe);
}
if(loonie!=0){
printf("%d loonie coins given as change \n",loonie);
}
if(quarter!=0){
printf("%d quarter coins given as change \n",quarter);
}
if(dime!=0){
printf("%d dime coins given as change \n",dime);
}
if(nickle!=0){
printf("%d nicke coins given as change \n",nickle);
}
if(penny!=0){
printf("%d penny coins given as change \n",penny);
}
return 0;
}
In most C compilers, including ours, the newline escape sequence '\n' yields an ASCII line feed character. The C escape sequence for a carriage return is '\r'.
Include a '\r' along with (or perhaps instead of) the '\n' in output strings. For example:
printf("Hello world\r\n");

Why my c program only produce zero answer while using for loop?

I tried to use for loop calculate the number of books keyed in and sum up their total price, but at the end i only get zero price in C program. What is my problem ? How to solve it?
#include<stdio.h>
int main(void)
{
int booknum;
float bookfee,bookgst,nogst,totfee,newfee,newfee_nogst;
bookgst=0.0;
nogst=0.0;
int cnt;
char code;
printf("Key in total books purchased >> ");
scanf("%d",&booknum);
for(cnt=1;cnt<=booknum;cnt++)
{
printf("\n\n");
printf("Key in price of the book >> ");
scanf("%f",&bookfee);
printf("Key in type( S=standard gst,Z=zero gst) \n>> ");
scanf("%c",&code);
getchar();
if(code=='S')
{
newfee=bookfee+0.6;
}
else if(code=='Z')
{
newfee_nogst=bookfee;
}
bookgst=bookgst+newfee;
nogst=nogst+newfee_nogst;
printf("\n");
}
totfee=bookgst+nogst;
printf("Book purchased with GST : RM %.2f\n",bookgst);
printf("Book purchased without GST : RM %.2f\n",nogst);
printf("Total payment : RM %.2f\n",totfee);
return 0;
}
There are a few problems with this code, but you're almost there!
First code reading needs to eat the previous \n (see this), otherwise the code is neither Z not S (it's a newline), and that's why the fees are never added.
(Search also for "fgets vs scanf" to see how to use the safer fgets).
scanf(" %c",&code);
then these lines
bookgst=bookgst+newfee;
nogst=nogst+newfee_nogst;
add the newfee / newfee_nogst ; these variables are set to 0 before the loop, but at the next occurence, they're still set to the value of the previous occurrence, thus either set them to 0 at the beginning of the loop, or, add the value directly in the if (see below). And since we're here, print an error if the code is wrong (and maybe subtract one to cnt to do one more loop with a correct code, in this case).
Also, the GST calculation is probably wrong, 6% of x is 0.06 * x, and if you want GST added to the value that's x * 1.06
if(code=='S')
{
bookgst = bookgst + bookfee*1.06; // or bookgst += bookfee*1.06
}
else if(code=='Z')
{
nogst = nogst + bookfee; // or nogst += bookfee
}
else {
printf("Code not understood\n");
}

Why can't I get the input value?

I cannot assign the input value to bj.
For example,when I input 756,what I expect to get is UTC at the same time: 2356,however the computer gives me UTC at the same time: 1600.
#include <stdio.h>
int main()
{
int bj;
scanf("Input a BJT: %d", &bj);
if(bj>=0&&bj<=2359&&bj%100<60)
{
if(bj<800)
bj+=1600;
else
bj-=800;
printf("UTC at the same time: %d",bj);
}
else
;
return 0;
}
scanf("Input a BJT: %d", &bj);
You've mixed up your printf and scanf functions somewhat. scanf specifies what the input should be so you don't want the output stuff in there as well as it'll end up expecting that in your input.
Split them out like this.
printf("Input a BJT:\n");
scanf("%d", &bj);
scanf("Input a BJT: %d", &bj);
This line is messed up. Use it like:
printf("Input a BJT: ");
scanf("%d", &bj);
check this link
There are two solutions:
1.
printf("Input a BJT:\n");
scanf("%d", &bj);
2.
When you input, you should input Input a BJT: before you input the value of bj .For example,I want bj equal 756, so I input Input a BJT: 756.

Simple Multichoice, Multivariable, Calculator Query

I recently programmed this code (C) for a pretty simple calculator in Xcode.
It works mostly but does not display the answer for the sum/s.
The code is as follows:
#include <stdio.h>
int main()
{
//int's & chars-----------------
char SumMethod;
int firstnumber, secondnumber;
//int's & chars------------------
puts("Calculator v0.6");
printf("Please input first number: "); //Prompts 'firstnumber' input
scanf("%d", &firstnumber); //Scan's 'firstnumber' input and saves to '&firstnumber'
printf("Please input second number: "); //Prompts 'secondnumber' input
scanf("%d", &secondnumber); //Scan's 'secondnumber' input and saves to '&secondnumber
printf("d" "Please select Method: +(a), -(b), *(c), /(d): ");
scanf("%c", &SumMethod); //Scan's 'SumMethod' input
if(SumMethod=='a') {
printf ("%d",firstnumber + secondnumber); //This section detects the SumMethod and outputs the corrisponding sum
}
else if (SumMethod=='b'){
printf("%d",firstnumber-secondnumber);
}
else if (SumMethod=='c') {
printf("%d",firstnumber * secondnumber);
}
else if (SumMethod=='d') {
printf("%d",firstnumber / secondnumber);
}
}
For the solution i'm looking for a response that doesn't drastically change the code as how it is now is what I understand and because i'm relatively new to coding in general however if there is an obvious fix using useful common tools that would be much appreciated.
Thanks, Xenon
Your problem is that your comparisons are wrong. You need to compare your input to the LETTER a and not the (undefined and essentially random) values of the variables a,b,c,d.
so it is
if(SumMethod=='a')
instead of
if(SumMethod==a)
You don't need your variables a,b,c,d at all.

program running through my if else after function call

I have a class assignment in C to make a simple calculator that performs three calculations. I haven't completed all of the functions yet but I am having a problem with my calcMenu function. When the function is called the program runs through all of the if else statements and unknown to me, performs only the else statement which is error checking. Than the function is run again which is intended but this time it does not run through all of the if else statements and allows the user to make a choice. I know I have done something really stupid but have been racking my brain for the last hour. If anyone has any pitty for me, than please point me in the right direction. I know all the system calls will Irk some but this is a basic class and our instructor has told us to use them.
Thanks in advance,
Mike
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#define pause system ("pause")
#define cls system ("cls")
//Prototype calculate functions here
void evenOrOdd(int userNumber);
void squareNum(int userNumber);
void cubeNum(int userNumber);
void calcMenu(int userNumber);
void main() {
//Declare local variables here
int userNumber = 0;
printf("\t\t\tThe amazing three function caluculator\n\n\n");
printf("Please enter a whole number that you would like to calculate\n");
scanf("%d", &userNumber);
calcMenu(userNumber);
}
void calcMenu(int userNumber)
{
char calculateOption;
printf("\nWhat calculation would you like to perform with your number?\n\n");
printf("Press A.) to check if your number is even or odd.\n\n");
printf("Press B.) to calculate the square of your number.\n\n");
printf("Press C.) to calculate the cube of your number.\n\n");
printf("press D.) to exit the program.\n");
scanf("%c", &calculateOption);
calculateOption = toupper (calculateOption);
if (calculateOption == 'A')
{
evenOrOdd(userNumber);
}
else if (calculateOption == 'B')
{
squareNum(userNumber);
}
else if (calculateOption == 'C')
{
cubeNum(userNumber);
}
else if (calculateOption == 'D')
{
system("cls");
printf("Thank You for using my amazing calculator.\n\n");
system ("pause");
system ("exit");
}
else
{
printf("Please enter a valid choice");
calcMenu(userNumber);
}
}
void evenOrOdd(int userNumber) {
userNumber = userNumber %2;
if (userNumber == 0)
{
printf("Your number is even. \n");
}
else
{
printf("Your number is odd. \n");
}
}
void squareNum(int userNumber) {
}
void cubeNum(int userNumber){
}
When you read input with scanf you have to press the Enter key to make the program continue. Your scanf call reads the single character from the input, but leaves the Enter key still in the input buffer, to be read next time you call scanf.
There is a very simple trick to solve that: Place a space in the scanf format string before or after the "%c". This will make scanf skip whitespace.
scanf("%c ", &calculateOption);
If you stepped through the code with a debugger you would have easily seen that calculateOption would have been the newline character.
First of all, You can condense all those printf statements into one function to save the extra calls.
Next, you should probably indent your functions, I can't tell where one begins and another ends at a glance.
Third, don't use system("pause"), use getchar().
Fourth, this is optional, you might want to turn those if statements into a switch statement.
Now, on to your question. First of all, instead of using scanf("%c", &calculateOption), just use getchar() here too. In this case, I would write calcMenu() as this:
int calcMenu(int userNumber){
printf("\nWhat calculation would you like to perform with your number?\n\n\
Press A.) to check if your number is even or odd.\n\n\
Press B.) to calculate the square of your number.\n\n\
Press C.) to calculate the cube of your number.\n\n\
Press D.) to exit the program.\n");
switch(toupper(getchar())){
case 'A':
evenOrOdd(userNumber);
break;
case 'B':
squareNum(userNumber);
break;
case 'C':
cubeNum(userNumber);
break;
case 'D':
system("cls"); //this is bad, really.
printf("Thank You for using my amazing calculator.\n\n");
getchar();
return 0;
default:
printf("Please enter a valid choice: ");
calcMenu(userNumber);
break;
}
}
Also, main should always return a value. void main is bad practice.
Disclaimer: The code isn't tested, you shouldn't copy/paste it anyways. I also don't know if you're being forced to use some things or not...

Resources