Tuesday, March 31, 2015

Calculate average for many numbers

Script

#include <stdio.h>

main()
{
        int number_of_numbers, count = 1;
        float takeinput, average, total = 0;

        printf("\n\nHow many numbers will you type in ?   :  ");
        scanf("%d", &number_of_numbers);

        do
        {
                printf("Enter a number:                             ");
                scanf("%f", &takeinput);
                total = total + takeinput;
                count = count + 1;
        }
                while (count <= number_of_numbers);

        average = total/number_of_numbers;
        printf("\nThe average of all these numbers is:  %.2f", average);
        printf("\n\n");
}


Execution

How many numbers will you type in ?   :  10
Enter a number:                             59
Enter a number:                             21
Enter a number:                             69
Enter a number:                             93
Enter a number:                             27
Enter a number:                             83
Enter a number:                             54
Enter a number:                             59
Enter a number:                             71
Enter a number:                             82

The average of all these numbers is:  61.80

Calculate the series average for a player

Script

#include <stdio.h>

main()
{
        char player[20];
        float firstODI, secondODI, thirdODI, fourthODI, fifthODI, sixthODI, seventhODI;
        float average;

        printf("\n");
        printf("Please enter the name of the player:     ");
        scanf("%s", &player);

        printf("\n");
        printf("Please enter his score in the first ODI:   ");
        scanf("%f", &firstODI);

        printf("\n");
        printf("Please enter his score in the second ODI:  ");
        scanf("%f", &secondODI);

        printf("\n");
        printf("Please enter his score in the third ODI:   ");
        scanf("%f", &thirdODI);

        printf("\n");
        printf("Please enter his score in the fourth ODI:  ");
        scanf("%f", &fourthODI);

        printf("\n");
        printf("Please enter his score in the fifth ODI:   ");
        scanf("%f", &fifthODI);

        printf("\n");
        printf("Please enter his score in the sixth ODI:   ");
        scanf("%f", &sixthODI);

        printf("\n");
        printf("Please enter his score in the seventh ODI: ");
        scanf("%f", &seventhODI);

        printf("\n");
        average = (firstODI + secondODI + thirdODI + fourthODI + fifthODI + sixthODI + seventhODI)/7;

        printf("\nPlayer:                                  %s", player);
        printf("\nFirst ODI:                                 %0.0f", firstODI);
        printf("\nSecond ODI:                                %0.0f", secondODI);
        printf("\nThird ODI:                                 %0.0f", thirdODI);
        printf("\nFourth ODI:                                %0.0f", fourthODI);
        printf("\nFifth ODI:                                 %0.0f", fifthODI);
        printf("\nSixth ODI:                                 %0.0f", sixthODI);
        printf("\nSeventh ODI:                               %0.0f", seventhODI);
        printf("\n                                        ==========");
        printf("\nAverage for the series for %s:     %.2f", player, average);
        printf("\n                                        ==========");
        printf("\n\n\n");
}


Execution


Please enter the name of the player:     Tendulkar

Please enter his score in the first ODI:   59

Please enter his score in the second ODI:  45

Please enter his score in the third ODI:   11

Please enter his score in the fourth ODI:  29

Please enter his score in the fifth ODI:   147

Please enter his score in the sixth ODI:   54

Please enter his score in the seventh ODI: 59


Player:                                  Tendulkar
First ODI:                                 59
Second ODI:                                45
Third ODI:                                 11
Fourth ODI:                                29
Fifth ODI:                                 147
Sixth ODI:                                 54
Seventh ODI:                               59
                                        ==========
Average for the series for Tendulkar:     57.71
                                        ==========



Sunday, March 22, 2015

Read and print a line of text

Script

#include <stdio.h>

main()
{
        char text[100];
        printf("\n");
        printf("Enter a line of text:            ");
        scanf(" %[^\n]", text);
        printf("\n");
        printf("You entered the following text:  ");
        printf("%s", text);
        printf("\n\n");
}


Execution


Enter a line of text:                     Gundappa Viswanath played 91 Test matches for India and scored 6080 runs.

You entered the following text:  Gundappa Viswanath played 91 Test matches for India and scored 6080 runs.





Enter a line of text:                     There's an aura about Sachin Tendulkar in India and around the cricketing globe that is unmatched by anyone else.

You entered the following text:  There's an aura about Sachin Tendulkar in India and around the cricketing globe that is unmatched by anyone else.

Monday, March 2, 2015

Fahrenheit to Celsius conversion

Script

#include <stdio.h>

main()
{
        int fahrenheit, celsius;
        int lower, upper, step;

        lower = 0;
        upper = 200;
        step  = 5;

        fahrenheit = lower;

        printf("\n");
        printf("Fahrenheit \t Celsius \n");
        printf("---------- \t ------- \n");

        while (fahrenheit <= upper)
        {
                celsius = 5 * (fahrenheit - 32)/9;
                printf("  %d \t\t   %d \n", fahrenheit, celsius);
                fahrenheit = fahrenheit + step;
        }
}


Execution

Fahrenheit       Celsius
----------       -------
  0                -17
  5                -15
  10               -12
  15               -9
  20               -6
  25               -3
  30               -1
  35               1
  40               4
  45               7
  50               10
  55               12
  60               15
  65               18
  70               21
  75               23
  80               26
  85               29
  90               32
  95               35
  100              37
  105              40
  110              43
  115              46
  120              48
  125              51
  130              54
  135              57
  140              60
  145              62
  150              65
  155              68
  160              71
  165              73
  170              76
  175              79
  180              82
  185              85
  190              87
  195              90
  200              93

Celsius to Fahrenheit conversion

Script

#include <stdio.h>

main()
{
        int fahrenheit, celsius;
        int lower, upper, step;

        lower = 0;
        upper = 200;
        step  = 5;

        celsius = lower;

        printf("\n");
        printf("Celsius \t Fahrenheit \n");
        printf("------- \t ---------- \n");

        while (celsius <= upper)
        {
                fahrenheit = (celsius * 9/5) +  32;
                printf("  %d \t\t    %d \n", celsius, fahrenheit);
                celsius = celsius + step;
        }
}


Execution

Celsius          Fahrenheit
-------          ----------
  0                 32
  5                 41
  10                50
  15                59
  20                68
  25                77
  30                86
  35                95
  40                104
  45                113
  50                122
  55                131
  60                140
  65                149
  70                158
  75                167
  80                176
  85                185
  90                194
  95                203
  100               212
  105               221
  110               230
  115               239
  120               248
  125               257
  130               266
  135               275
  140               284
  145               293
  150               302
  155               311
  160               320
  165               329
  170               338
  175               347
  180               356
  185               365
  190               374
  195               383
  200               392

Hello World

Script

#include <stdio.h>

main()
{
printf("Hello, World!!! \n");
}


Execution

Hello, World!!!