Calculation of Annual Interest Rates

Asked 2 years ago, Updated 2 years ago, 32 views

I would like to do the following, but I get an error.

$./a.out
Principal (yen): 1000000
% annual interest rate: 0.001
Deposit period (year): 10
 Year 1: 1000010 Yen
 Year 2: 1000020 Yen
 Year 3: 1000030 Yen
 Year 4: 1000040 Yen
 Year 5: 1000050 Yen
 Year 6: 1000060 Yen
 Year 7: 1000070 Yen
 Year 8: 1000080 Yen
 Year 9: 1000090 Yen
Year 10: 1000100 Yen

I got an error when I programmed as follows.

int main (void)
{
    inti, m, n, y;

    printf("Principal: ");
    scanf("%d", & m);
    printf("Annual interest rate (%):");
    scanf("%d", & n);
    printf("Deposit Period (Year):");
    scanf("%d", & y);
    for(i=m;i<=y;i=i+n/100){
        printf("Year i: %d Yen\n", i );
    }

    return 0;
}
ex0304.c:In function 'main':
ex0304.c:8:24:warning:unknown conversion type character') 'in format [-Wformat=]
     printf("Annual interest rate (%):");
                        ^

c

2022-09-29 22:38

3 Answers

Do not use % alone in printf or in format strings in scanf.You must have at least one known character after you.Same as \ in the general string.

If you want the percentage character % to appear on the screen, it's printf("%d%%",100);.


2022-09-29 22:38

printf requires the % symbol followed by the type specifier (the "d" or "c" part in the example below).

Example:

printf("%d", num);## decimal
printf("%s",str);## string

However, where the warning appears, the % symbol is written as (%), which is considered to be an unknown type designation of %).

If you want to print the % symbol as a character in printf, write it like %%.

printf("%d%%,num);

Also, there are several wrong parts besides the warning, so I don't think it will work as expected even if only the printf part is corrected.


2022-09-29 22:38

"By the way, @cubick said ""there are several wrong parts besides the parts where warning is displayed""

  • Annual interest rate data is a value that cannot be expressed as an integer, but you are trying to enter it as an integer in a variable.
    (By the way, with VisualC++ on Windows, if you enter 0.001 for the annual interest rate, the next deposit period (year): will be skipped, and the for loop will not run and will end without display as shown below.)
  • forThe control portion of the loop is a mixture of loop count control and interest rate calculation, making it meaningless and never running.
    (Let's control the number of loops and calculate the interest rate application results independently)
  • Even if you can run it, the format string you specified in the results display and the variable you want to display are incorrect, so it will not result in the desired display result.
    (Think about what data is needed to display and how it can be incorporated into the format string.)

Let's think carefully.


2022-09-29 22:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.