Thread: how to print correct number of monthly sales more than 1000

  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    1

    how to print correct number of monthly sales more than 1000

    Code:
    #include<stdio.h>
    
    int main() {
        int month;
        int monthPeriod;
        int more1000;
        float largestSales;
        float lowestSales;
        float sum;
        float sales;
        printf("Enter number of months in the financial period: ");
        scanf("%d", &monthPeriod);
        sum = 0;
        month = 1;
        while (month <= monthPeriod) {
            printf("Enter the sales of month %d ($): ", month);
            scanf("%f", &sales);
            sum += sales;
            if (month == 1 || sales > largestSales)
                largestSales = sales;
            if (month == 1 || sales < lowestSales)
                lowestSales = sales;
            if (month == 1 || sales > 1000)
                more1000++;
            month++;
        }
        printf("Total sales: $%f\n", sum);
        printf("Average monthly sales: $%f\n", sum / monthPeriod);
        printf("Largest monthly sales: $%f\n", largestSales);
        printf("Lowest monthly sales: $%f\n", lowestSales);
        printf("Number of monthly sales more than 1000: $%d\n", more1000);
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,763
    You should initialise more1000 to 0 to begin with.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-10-2015, 01:05 AM
  2. Replies: 5
    Last Post: 05-17-2012, 03:18 PM
  3. Replies: 1
    Last Post: 11-04-2011, 01:16 PM
  4. Replies: 11
    Last Post: 06-23-2010, 01:36 AM
  5. Replies: 0
    Last Post: 03-28-2003, 08:20 AM

Tags for this Thread