Skip to main content

C Program to find the Average of numbers

Here we will write two C programs to find the average of two numbers(entered by user).

Example 1: Program to find the average of two numbers

#include <stdio.h>
int main()
{
    int num1, num2;
    float avg;

    printf("Enter first number: ");
    scanf("%d",&num1);
    printf("Enter second number: ");
    scanf("%d",&num2);

    avg= (float)(num1+num2)/2;

    //%.2f is used for displaying output upto two decimal places
    printf("Average of %d and %d is: %.2f",num1,num2,avg);

    return 0;
}
Output:
Enter first number: 12
Enter second number: 13
Average of 12 and 13 is: 12.50

Example 2: Program to find the average using function

In this program, we have created a user defined function average() for the calculation of average. The numbers entered by user are passed to this function during function call.
#include <stdio.h>
float average(int a, int b){
    return (float)(a+b)/2;
}
int main()
{
    int num1, num2;
    float avg;

    printf("Enter first number: ");
    scanf("%d",&num1);
    printf("Enter second number: ");
    scanf("%d",&num2);

    avg = average(num1, num2);

    //%.2f is used for displaying output upto two decimal places
    printf("Average of %d and %d is: %.2f",num1,num2,avg);

    return 0;
}
Output:
Enter first number: 20
Enter second number: 13
Average of 20 and 13 is: 16.50

Comments

Popular posts from this blog

Recursive program to insert a star between pair of identical characters

Given a string with repeated characters, we have to insert a star i.e. ” * “  between pair of adjacent identical characters using recursion. Examples: Input : aabb Output : a*ab*b Input : xxxy Output : x*x*xy Recommended: Please try your approach on  {IDE}  first, before moving on to the solution. Approach: If there is an empty string then simply return. This forms our  base condition . Else we do the following- Check if the first two characters are identical. If yes, then insert ” * ” between them. As we have now checked for identical characters at first two positions of the string so we now make a recursive call  without the first character of the string . The above approach has been implemented below: C filter_none edit play_arrow brightness_4 // Recursive CPP program to insert * between // two consecutive same characters. #include <iostream> using namespace std;     // Funct...

Count the number of objects using Static member function

Write a program to design a class having static member function named showcount() which has the property of displaying the number of objects created of the class. Explanation:  In this program we are simply explaining the approach of static member function. We can define class members and member functions as static using static keyword. Before understanding static member function, we must understand static member. When we declare a member of a class as static it means no matter how many objects of the class are created, there is  only one copy of the static member . Important points about Static : A static member is shared by all objects of the class, all static data is initialized to zero when the first object is created, if no other initialization is present. A static member function can only access static data member, other static member functions and any other functions from outside the class. By declaring a function member as static, we make it independent of any ...

Send and Receive JSON over sockets in server and client application in C

I want to send the data in JSON over sockets in a server-client application written in C. I am using json-c / libjson library for handling JSON data in C application. By working on some of the tutorials I am able to create JSON object and able to parse it successfully. Now I want to use the JSON data format for the communication of server-client. Here is part of my server and client code server.c int main () { int listenfd = 0 , connfd = 0 ; //related with the server struct sockaddr_in serv_addr ; //json_object * jobj; uint8_t buf [ 158 ], i ; memset (& buf , '0' , sizeof ( buf )); listenfd = socket ( AF_INET , SOCK_STREAM , 0 ); serv_addr . sin_family = AF_INET ; serv_addr . sin_addr . s_addr = htonl ( INADDR_ANY ); serv_addr . sin_port = htons ( 8888 ); bind ( listenfd , ( struct sockaddr *)& serv_addr , sizeof ( serv_addr )); printf ( "binding\n" ); listen ( listenf...