then subtract it by 100
nbalance = (nbalance * interest) + nbalance - 100;
though it defeats the purpose of using pay in your operation...
but hey as long as you get your desired output you should be happy
then subtract it by 100
nbalance = (nbalance * interest) + nbalance - 100;
though it defeats the purpose of using pay in your operation...
but hey as long as you get your desired output you should be happy
The solution, as in how the output is supposed to be?
Enter the monthly payment: 100
Month: 1 balance: 915.0 total payments: 100.0
Month: 2 balance: 828.73 total payments: 200.0
Month: 3 balance: 741.16 total payments: 300.0
Month: 4 balance: 652.27 total payments: 400.0
Month: 5 balance: 562.06 total payments: 500.0
Month: 6 balance: 470.49 total payments: 600.0
Month: 7 balance: 377.54 total payments: 700.0
Month: 8 balance: 283.21 total payments: 800.0
Month: 9 balance: 187.46 total payments: 900.0
Month: 10 balance: 90.27 total payments: 1000.0
Month: 11 balance: -8.38 total payments: 1100.0
What I meant to say is the solution on how did 915 become 828 then 741 then 652 and so on cause as you can see your current operation cannot do this
If you don't know the solution by hand then try not to code it yet
can you post the operation to get the output...not the code but the solution
cause It's hard for me to guess on my side
You still don't get it do you
915*.015 will be equal to....
Then add the value to nbalance
Don't just save the value to nbalance
the problem is not the while loop itself but rather the operation of the doubles inside the loop
nbalance = (nbalance * interest) - pay;
after this operation nbalance becomes less than 1
try this
while(nbalance>1 || nbalance==1)
while (nbalance>=1) {
System.out.println ("Month: "+ month + " Balance: " + nbalance + " Total Payments: " + pay);
month++;
nbalance = (nbalance * interest) - pay;
nbalance--;
pay = pay+100;
}
why should it have nbalance--?
also change the position of pay and nbalance operation
Ok, changed it to that, and also back to the while loop. So now it looks like this:
import java.util.Scanner; class CreditCardBill{ public static void main (String[] args){ Scanner input = new Scanner(System.in); int month = 1; System.out.println ("Enter the monthly payment: "); double pay = input.nextInt(); double left = 1000; double interest = 0.015; double balance = (left * interest) + left; double nbalance = (balance - pay); while (nbalance>=1) { System.out.println ("Month: "+ month + " Balance: " + nbalance + " Total Payments: " + pay); month++; nbalance = (nbalance * interest) - pay; nbalance--; pay = pay+100; } } }
So does it still have an error?
I wasn't sure how to change the while loop, and the for loop made a little more sense to me.
well suit yourself
nbalance-- subracts itself by 1 which makes the output wrong
Trust me its better to use a while loop
nbalance = (nbalance * interest) - 100;
if pay is user input then subtract pay to it
If you read my one of my previous post I told you that use the previous while loop in your code.. there's no need for a for loop or do want to make it complicated?
in your code your multiplying it to balance and not to nbalance directly...
try to change your operations that is suited for the program
can you post your current code
Try to trace your program and see the logic... your almost done
pay++ only adds 1 to your previous amount e.g 100...101...102
you don't need to change it to a for loop just add the previous operations on your while loop, make a few adjustments then you should be done
Okay I can see the whole picture now...
Include some of your previous operations on the loop and you should be done ;)
Yes, but when that break is removed, it becomes an infinite loop. The months accumulate, the nbalance and pay don't change at all, instead just keep repeating itself.
I edited my previous post and stated that you should have included your previous operation on the loop that will deduct nbalance to make it less than 1 to stop the loop
whoops sorry missed that... should have noted that you are using a break ... to stop the loop
instead you should have the previous operations on the loop that will deduct the money(to make nbalance less than 1)
Of course it will only print out that line...
you don't have a loop
then why not convert it say 1.5 percent will become .015 then add it or do any operation with it
If the output should be a double then why not declare balance and nbalance as a double?
and why not declare left as a double too?
and if you want left to be int then just convert it double then
A way to convert from double to int is to use Double.intValue()
I already showed the right way of printing it out in my first post
System.out.println("There are " + count + " student(s) in the class");
System.out.println("The total of their scores = " + total + "points");
I need to print out the scores and the grades when entered and i do not know how to print the scanner. im sure i am probably missing other things as well.
print the scanner?? or print the variables you used scanner at?? please make it more clear
also
System.out.printf("%s%d%s\n", "There are ",count, " student(s) in the class");
System.out.printf("%s%d%s\n", "The total of their scores = ",total,"points");
I'm not sure if this is the right way of doing this... I think it's more like this.
System.out.println("There are " + count + " student(s) in the class");
System.out.println("The total of their scores = " + total + "points");
Since your using a 512 digit number my suggestion would be to convert it to hexadecimal base or a higher base digit, do the operation then convert it back to string or decimal base
though I'm not sure if this is the best solution
...Maybe using a long int might help you more in storing the numbers
atoi1[0] = rev1[len1];
atoi2[0] = rev2[len2];
atoi1[1] = '\0';
atoi2[1] = '\0';
num1 = atoi(atoi1);
num2 = atoi(atoi2);
what are you trying to achieve here?
atoi just converts a string to an integer why put a null terminator at the end?
int i;
char Input [512];
printf ("Enter a number: ");
fgets ( Input, 512, stdin ); // or use a standard scanf
i = atoi (Input);
printf ("The value entered is %d",i);
Why use c in a loop condition.. why not use it as a counter?
Your not thinking from what I just said, the answer is already in front of you
for(int c=1; c<=b; c++){
if(b ==5)
println("loop terminated " + c);
}
Why use c in a loop condition.. why not use it as a counter?
*"I'm not gonna explicitly say the answer to your problem"
But why does b start at 30000 and something? Shouldn't b increment by 1?
What I meant to say was your using b as an increment while your using c in your initialization and your loop condition in your previous post
for( c=0; c <= b; b++){
println("b = " + b);
Since nothing happens to c it will never be equal to b which is why it loops forever
if(b ==5){
println("loop terminated " + c++);
}
Also since b = 6 it will never be equal to 5
What's this?.. another guess what's wrong with my code game... huh
Not only do you want us to do the work for you, but you did not state how your program works, it's purpose or the desired output.
oh , i only got codeblocks don't know if it is good , since it didn't point much errors for me
On another note, how long have you been studying programming? i have only been studying for a month..
2 years.... most of the errors I pointed out are basic errors which can be fixed just by looking at your code
The compiler is not as important as the programmer
oo which compiler do you use?o.o
linux gcc
Yes its possible to have a case within a case.
Did you compile your work cause I can see a lot of syntax error
scanf("%c", &option);
In lines 11,50,84,124,157,190,231,298,342,383
Use %s in receiving input
printf("Do you want to find the volume or surface area of the sphere?Indicate by entering the Initial Letter of the word\n");
scanf("%f", &option);
In line 268:
option is not a float
printf("\nThe surface area of the sphere is %.2f");
In line 285:
you forgot to put &surface_area
printf("%d = %d\n",j,freq[i-2]);
why are you using you using freq[35998]?
I think that integer i your using is supposed to be j.
int mark 1, mark 2, mark 3, mark 4, mark 5, The best 2 highest marks;
space is not used in variable name declaration
The best 2 highest marks = mark > 50 + mark > 50
This is not how you use an operation statement
if ( mark > 50 ){
printf (" mark > 50 ");
else {
printf (" the mark is not one of the best 2 highest marks ");
}
mark is not initialized
Did you compile your work cause you can see most of your errors there
Here's my example
String Str = "The cake is a lie";
char delimiter = ' '; //the space is the delimeter
// calculate the number of delimiter characters
int N = 0;
for (int i = 0; i < Str.length(); i++)
if (Str.charAt(i) == delimiter) N++;
String[] tokens = new String[N+1]; //we add 1 for the last word
int[] tokenlength = new int[N+1];
// parse N+1 tokens and store in an array
int a = 0, b = 0;
for (int i = 0; i < N; i++) {
while(Str.charAt(a) != delimiter)
a++;
tokens[i] = Str.substring(b, a); //saves the word in the array
tokenlength[i] = tokens[i].length(); //saves the length of the current word
a++;
b = a;
}
//For the last word
tokens[N] = Str.substring(a, Str.length());
tokenlength[N] = tokens[N].length();
// print results for testing
System.out.println(Str);
for (int i = 0; i < tokens.length; i++){
// print each tokens in reverse
for(int j = tokenlength[i]-1; j >= 0; j-- ){
System.out.print(tokens[i].charAt(j));
}
System.out.print(" ");
}
Also next time wrap your code in code tags
tokenize the sentence by using space as a delimiter, save each letter in an array then print out the result in reverse
Heres a more relevant example to your structure
struct emp{ //example structure
char name[20];
};
void add(struct emp *employee,int n){
int i;
for(i=1;i<=n;i++){
printf("Enter employee %i",i); //my example on receiving input
printf("\nEnter name:");
scanf("%s",employee[i].name);
}
}
main()
{
int num = 5; //defined number of employees
struct emp employee[1000];
add(employee, num);
}
We're supposed to have an array that stores every employee that is created, but i dont know how to create/'name' new employee's on the fly.
Does scanf on each index on the array doesn't solve your problem?
So my real question I guess is how do i set up my function call and definition?
Since I don't know what your structure looks like I'll make this example
struct x
{
int a;
int b;
};
main()
{
struct x z;
z.a = 5;
printf("The first member is %d \n", z.a);
}
could you post your code for more details?
You should have used your previous thread for clarifications dsoto
how? .. can you at least help me with the logic ?
If you're compiling in C99, you could #include <stdbool.h> and use bool, true and false. bool would be a macro for _Bool, which itself is a standard type guaranteed to hold 0 or 1, and true and false are macros for 1 and 0 respectively.
If not, you could just use 0 and 1 directly.But anyway you already got the idea
Why not use scanf like this?
printf("Enter two words: ");
scanf("%s",x1);
scanf("%s",x2);
Also why use a while condition when an if else condition can be enough
for(i = 0; i < c; i++)
{
if (x1[i] == x2[i]){
x3[i] = x1[i];
}
else break;
}
printf("%s\n",x3);
you can use a boolean to know if the number has been repeated instead of using printf
You could store the input numbers in an array and use a loop again to check if the current number has been entered before
Why are there so many worthless posts here? The answer is extremely simple:
There is no function (why would there be?)
Use = on 2 consecutive characters.
We don't give answers here. We help them fix their attempt.As for your code:
main(){
-- main is always an intscanf("%s",string);
-- see thisfor(i=0;i<strlen(string);i++)
-- why calculate the length of the string every time through the loop? That's a waste of resources. And why compare the last character with the \0 at the end?if(string[i]==string[i+1]){
-- indent properlyprintf("%c\n", string[i]);
-- indent properly
where's your requiredreturn
statement?
Sorry about that the problem seems so easy I just couldn't help myself do the code,
and yes I agree the using strlen is a waste of time but I just thought that a (any)string function a part of his requirement so I include it.
Here's the answer plus it has a string function
#include<stdio.h>
#include<string.h>
main(){
char string[20];
int i;
printf("Enter string:");
scanf("%s",string);
for(i=0;i<strlen(string);i++)
{
if(string[i]==string[i+1]){
printf("%c\n", string[i]);
}
}
}
Does it really need to be a String function?
Because you can use a simple for loop , if/else statement and a counter to output the consecutive character
Though if (any) String Function is required at all you can use strcmp on each index(a letter) and the index after that(next letter) to know if it is consecutive though it is inefficient.
Note that your going to find a way to get each letter according to its index and use a for loop and if else statement along with strcmp to find a consecutive letter.
Does it really need to be a String function?
Because you can use a simple for loop , if/else statement and a counter to output the consecutive character
Though if (any) String Function is required at all you can use strcmp on each index(a letter) and the index after that(next letter) to know if it is consecutive though it is inefficient.