How to use function with no arguments and no return values in C Language

Program to use function with arguments and no return values in C Language


#include <stdio.h>
void displaymenu(void);
int main()
{
    int choice;
    displaymenu();
    printf("Enter your choice");
    scanf("%d", &choice);
    return 0;
}
void displaymenu(void)
{
    printf("1.Create database\n");
    printf("2.Insert new record\n");
    printf("3.Modify a record\n");
    printf("4.Delete a record\n");
    printf("5.Display all records\n");
    printf("6.Exit\n");
}

Output


1.Create database
2.Insert new record
3.Modify a record
4.Delete a record
5.Display all records
6.Exit
Enter your choice

How to use factorial of a number in C language

Program to use factorial of a number in C language


#include <stdio.h>
long int factorial(int n);
int main()
{
    int num;
    printf("enter a number :");
    scanf("%d", &num);
    if (num < 0)
    {
        printf("no factorial of negative number\n");
    }

    else
    {
        printf("factorial of %d is %ld\n", num, factorial(num));
    }

    return 0;
}
long int factorial(int n)
{
    int i;
    long int fact = 1;
    if (n == 0)
        return 1;
    for (i = n; i > 1; i--)
    {

        fact *= i;
        printf("i %d\t fact %ld\n", i, fact);
    }
    return fact;
}

Output


i 5 fact 5
i 4 fact 20
i 3 fact 60
i 2 fact 120
factorial of 5 is 120

How to use function find the larger number in C Language

Program to use function find the larger number in C Language


#include <stdio.h>
int larger(int x, int y);
int main(void)
{
    int x, y;
    printf("Enter two number :");
    scanf("%d %d", &x, &y);
    printf("Larger number=%d\n", larger(x, y));
    return 0;
}
int larger(int x, int y)
{
    return x > y ? x : y;
}

Output


Enter two number :15
9
Larger number=15

How to use function parameters and arguments in C language

program to use function parameters and arguments in C language


#include <stdio.h>
void func(int a,int b);
int main()
{
    int x=10,y=20;
    func(x,y);
    printf("x=%d,y=%d\n",x,y);
    return 0;
}
void func(int a,int b)
{
    a++;
    b--;
    printf("a=%d, b=%d\n",a,b);
}

output


a=11, b=19
x=10,y=20

How to use function understand the return statement in C Language

Program to use function understand the return statement in C Language


#include lt&;stdio.h>
void selection(int age, float ht);
int main(void)
{
    int age;
    float ht;
    printf("Enter age and height: ");
    scanf("%d%f", &age, &ht);
    selection(age, ht);
    return 0;
}
void selection(int age, float ht)
{

    if (age > 25)
    {
        printf("age should be less than 25\n");
        return;
    }
    if (ht < 5)
    {
        printf("height should be more than 5\n");
        return;
    }
    printf("Selected\n");
}

Output


Enter age and height:19
Selected

How to use function find the sum of two numbers

Program to use function find the sum of two numbers


#include <stdio.h>
int sum(int x, int y); 
int main(void)
{
    int a = 10, b = 20, k;
    k = sum(a, b); /*Function call*/
    printf("%d\n", k);
    k = sum(4, 5); /*Function call*/
    printf("%d\n", k);
    k = sum(a + b, b * 2); /*function call*/
    printf("%d\n", k);
    return 0;
}
int sum(int x, int y)
{
    int s;
    s = x + y;
    return s;
}

Output


30
9
70

How to use find the sum of two numbers in C Language

Program to use find the sum of two numbers in C Language


#include <stdio.h>
int sum(int x, int y); /*Function declaration*/
int main()
{
    int a, b, s;
    printf("enter values for a and b :");
    scanf("%d %d", &a, &b);
    s = sum(a, b); /*Function call*/
    printf("sum of %d and %d is %d\n", a, b, s);
    return 0;
}
int sum(int x, int y) /*Function definition*/
{
    int s;
    s = x + y;
    return s;
}

Output


enter values for a and b :5
4
sum of 5 and 4 is 9