Page 1 of 1

vat/vatback - work with percentages

Posted: Fri Jan 29, 2016 9:20 am
by machinebacon
The first little program asks for the percentage (e.g. for VAT) and then accepts input to calculate the total gross amount in a loop:

Code: Select all

#include<stdio.h>

float n,p,ps,s;

int main()
{
    printf("Enter percentage number for all operations: \n%% ");
    scanf("%f",&p);
    printf("Enter net value, press Ctrl-c to terminate: \n");

    for( ; ; ) {
        printf("> ");
        scanf("%f",&n);
        ps = (n * p)/100;
	s = n+ps;
        printf("%.2f + %.2f = %.2f\n", n, ps, s);
        }
    return 0;
}
And this one does the calculation backwards, from gross to net substracting percentage.

Code: Select all

#include<stdio.h>

float n,p,ps,s;

int main()
{
    printf("Enter percentage number to substract for all operations: \n%% ");
    scanf("%f",&p);
    printf("Enter total gross amount, press Ctrl-c to terminate: \n");

    for( ; ; ) {
        printf("> ");
        scanf("%f",&n);
        ps = (n * p)/(100 + p);
	s = n-ps;
        printf("%.2f - %.2f = %.2f\n", n, ps, s);
        }
    return 0;
}
Nothing fancy, but still quicker than finding the calculator and typing the whole shit in (again and again for batch operations).

Re: vat/vatback - work with percentages

Posted: Fri Jan 29, 2016 9:36 am
by Snap
Tnaks for this, bacon. Really helpful for my invoices.

Re: vat/vatback - work with percentages

Posted: Fri Jan 29, 2016 10:06 am
by franksinistra
useful stuff... thanks

Re: vat/vatback - work with percentages

Posted: Fri Jan 29, 2016 10:11 am
by simgin
Brilliant julius , thanks!