C Program To Find Armstrong Numbers Between 1 and 500 using Function

Lets write a C program to find Armstrong number or Narcissistic number from 1 to 500 using function.

Problem Statement
Write a C program to print out all Armstrong numbers or Narcissistic number between 1 and 500. If sum of cubes of each digit of the number is equal to the number itself, then the number is called an Armstrong number. Hint: Use function / method.

An Armstrong number or Narcissistic number is an n-digit base b number such that the sum of its (base b) digits raised to the power n is the number itself.

Example 1:
If number = 153
It has 3 digits: 1, 5 and 3. So n = 3.
result = 13 + 53 + 33 = 1 + 125 + 27 = 153.
So the original number 153 is equal to the result. So it’s an Armstrong Number.

Example 2:
If number = 1634
It has 4 digits: 1, 6, 3 and 4. So n = 4.
result = 14 + 64 + 34 + 44 = 1 + 1296 + 81 + 256 = 1634.
So the original number 1634 is equal to the result. So it’s an Armstrong Number.

Related Read:
C Program to print Armstrong Numbers between 1 and 500

Video Tutorial: C Program To Find Armstrong Numbers From 1 To 500 using Function


[youtube https://www.youtube.com/watch?v=V0Ws8aS4GwI]

YouTube Link: https://www.youtube.com/watch?v=V0Ws8aS4GwI [Watch the Video In Full Screen.]


To count the digits in a number

    int n = 0, temp;

    temp = num;

    while(temp)
    {
        temp = temp / 10;
        n++;
    }

To Iterate through the digits in the number

    while(num)
    {
        rem = num % 10;
        sum = sum + pow(rem, n);
        num = num / 10;
    }

To know above code logic, please visit and watch the video tutorial present at C Program to Check Armstrong Number.

Full Source Code: C Program To Find Armstrong Numbers Between 1 and 500 using Function

#include<stdio.h>
#include<math.h>

float armstrong(int);

int main()
{
    int count;

    for(count = 1; count <= 500; count++)
    {
        if(count == armstrong(count))
        {
            printf("%d is a Armstrong number\n", count);
        }
    }

    return 0;
}

float armstrong(int num)
{
    int rem, n = 0, temp;
    float sum = 0.0;

    temp = num;

    while(temp)
    {
        temp = temp / 10;
        n++;
    }

    while(num)
    {
        rem = num % 10;
        sum = sum + pow(rem, n);
        num = num / 10;
    }

    return(sum);
}

Output:
1 is a Armstrong number
2 is a Armstrong number
3 is a Armstrong number
4 is a Armstrong number
5 is a Armstrong number
6 is a Armstrong number
7 is a Armstrong number
8 is a Armstrong number
9 is a Armstrong number
153 is a Armstrong number
370 is a Armstrong number
371 is a Armstrong number
407 is a Armstrong number

#include<stdio.h>
#include<math.h>

float armstrong(int);

int main()
{
    int count;

    for(count = 1; count <= 500; count++)
    {
        if(count == armstrong(count))
        {
            printf("%d is a Armstrong number\n", count);
        }
    }

    return 0;
}

float armstrong(int num)
{
    int rem;
    float sum = 0.0;

    while(num)
    {
        rem = num % 10;
        sum = sum + (rem * rem * rem);
        num = num / 10;
    }

    return(sum);
}

Output:
1 is a Armstrong number
153 is a Armstrong number
370 is a Armstrong number
371 is a Armstrong number
407 is a Armstrong number

Logic To Find Armstrong Numbers Between 1 and 500 using Function

Using for loop(inside main method) we pass value(from 1 to 500) to armstrong function one by one. The value is present in variable count.

Function armstrong checks the number of digits present in the number(we call it as n) and then separates individual digits of the number and multiplies all the individual digits n times and adds them all and finally returns the sum.

If the armstrong function returned number and the value present in count are same then the value present in count is an Armstrong number, else we pass on to check the next number selected by for loop.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find Biggest of Three Numbers using Function

In this video tutorial you’ll learn how to find biggest of three integer numbers using function.

Related Read:
Nested if else Statement In C
Biggest of 3 Numbers: C
Biggest of 3 Numbers Using Ternary Operator: C

Video Tutorial: C Program To Find Biggest of Three Numbers using Function


[youtube https://www.youtube.com/watch?v=0fxG98WizAo]

YouTube Link: https://www.youtube.com/watch?v=0fxG98WizAo [Watch the Video In Full Screen.]

Source Code: C Program To Find Biggest of Three Numbers using Function

#include<stdio.h>

int biggest(int, int, int); // function prototype

int main()
{
    int a, b, c;

    printf("Enter 3 integer numbers\n");
    scanf("%d%d%d", &a, &b, &c);

    //function call biggest(a, b, c)
    printf("Biggest of %d, %d and %d is %d\n", a, b, c, biggest(a, b, c));

    return 0;
}

// function definition
int biggest(int x, int y, int z)
{
    if(x > y && x > z)
    {
       return x;
    }
    else
    {
       if(y > z)
          return y;
       else
          return z;
    }
}

Output
Enter 3 integer numbers
50
40
60
Biggest of 50, 40 and 60 is 60

Source Code: C Program To Find Biggest of Three Numbers using Function, Using Ternary Operator

#include<stdio.h>

int biggest(int, int, int); // function prototype

int main()
{
    int a, b, c;

    printf("Enter 3 integer numbers\n");
    scanf("%d%d%d", &a, &b, &c);

    //function call biggest(a, b, c)
    printf("Biggest of %d, %d and %d is %d\n", a, b, c, biggest(a, b, c));

    return 0;
}

// function definition
int biggest(int x, int y, int z)
{
   return( (x>y && x>z)?x:(y>z)?y:z );
}

Logic To Find Biggest of 3 Numbers using Function

We ask the user to enter 3 integer numbers. We pass those 3 integer numbers to user defined function biggest. Inside function biggest we use ternary operator to determine the biggest of those 3 numbers. Function biggest returns the biggest of the 3 numbers back to the calling method/function – in above progam its main() method.

Note: Function biggest returns integer type data. And it takes 3 arguments of type integer. We’re calling function biggest from inside printf() function.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find Biggest of Two Numbers using Function

In this video tutorial you’ll learn how to find biggest of two integer numbers using function.

Related Read:
Biggest of Two Numbers: C
Biggest of Two Numbers Using Ternary Operator: C

Video Tutorial: C Program To Find Biggest of Two Numbers using Function


[youtube https://www.youtube.com/watch?v=PF7sGUN_U_M]

YouTube Link: https://www.youtube.com/watch?v=PF7sGUN_U_M [Watch the Video In Full Screen.]

Source Code: C Program To Find Biggest of Two Numbers using Function

#include<stdio.h>

int biggest(int, int); //  function prototype

int main()
{
    int a, b;

    printf("Enter 2 integer numbers\n");
    scanf("%d%d", &a, &b);

    // function call biggest(a, b)
    printf("Biggest of %d and %d is %d\n", a, b, biggest(a, b));

    return 0;
}

//function definition
int biggest(int x, int y)
{
    return( x>y?x:y );
}

Output 1
Enter 2 integer numbers
50
25
Biggest of 50 and 25 is 50

Output 2
Enter 2 integer numbers
-5
-10
Biggest of -5 and -10 is -5

Logic To Find Biggest of 2 Numbers using Function

We ask the user to enter 2 integer numbers. We pass those 2 integer numbers to user defined function biggest. Inside function biggest we use ternary operator to determine the biggest number. Function biggest returns the biggest of the 2 numbers.

x>y?x:y

Here if x is greater than y, x will be returned else y will be returned.

Note: Function biggest returns integer type data. And it takes 2 arguments of type integer. We’re calling function biggest from inside printf() function.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Check Leap Year or Not using Function

Any year is entered through the keyboard. Write a function to determine whether the year is a leap year or not.

Related Read:
C Program To Check Leap Year

Video Tutorial: C Program To Check Leap Year or Not using Function


[youtube https://www.youtube.com/watch?v=bFYhQJHXWlU]

YouTube Link: https://www.youtube.com/watch?v=bFYhQJHXWlU [Watch the Video In Full Screen.]

Source Code: C Program To Check Leap Year or Not using Function

#include<stdio.h>
#include<stdbool.h>

bool leap(int); // function prototype

int main()
{
    int year;

    printf("Enter a year to find leap year or not\n");
    scanf("%d", &year);

    //function call leap(year);
    if( leap(year) )
    {
        printf("%d is leap year\n", year);
    }
    else
    {
        printf("%d is not leap year\n", year);
    }

    return 0;
}

//function definition
bool leap(int year)
{
    if(year % 100 == 0)
    {
        if(year % 400 == 0)
            return true;
        else
            return false;
    }
    else
    {
        if(year % 4 == 0)
            return true;
        else
            return false;
    }
}

Output 1
Enter a year to find leap year or not
2020
2020 is leap year

Output 2
Enter a year to find leap year or not
2021
2021 is not leap year

Logic To Find Leap Year or Not: Using Function

If user entered year is a century year(year ending with 00) and the year is perfectly divisible by 400, then it’s a leap year.

If the user entered year is not a century year and the year is perfectly divisible by 4, then its a leap year orelse it’s not a leap year.

In our function, we return true if the user entered year is leap year and return false if the user entered year is not leap year.

Note: User defined function leap has a return type of bool i.e., it returns either true or false. For our C program to support bool type we are including stdbool.h header file.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Addition of 2 Numbers using Function: C Program

In this video tutorial lets learn how to add two integer numbers using functions in C programming language.

Related Read:
Function / Methods In C Programming Language
Addition of 2 Numbers: C

Video Tutorial: Addition of 2 Numbers using Function: C Program


[youtube https://www.youtube.com/watch?v=1fmOsKbnTxQ]

YouTube Link: https://www.youtube.com/watch?v=1fmOsKbnTxQ [Watch the Video In Full Screen.]

Source Code: Addition of 2 Numbers using Function: C Program

#include<stdio.h>

int add(int, int); // function prototype

int main()
{
    int a, b;

    printf("Enter 2 integer numbers\n");
    scanf("%d%d", &a, &b);

    //function call add(a, b);
    printf("%d + %d = %d\n", a, b, add(a, b));

    return 0;
}

//function definition
int add(int x, int y)
{
    return(x+y);
}

Output 1
Enter 2 integer numbers
25
25
25 + 25 = 50

Output 2
Enter 2 integer numbers
50
5
50 + 5 = 55

Here we call user defined function add from within printf() function. We pass 2 integer variables as argument to add method. The values of variable a and b are copied to variable x and y. Note that values of variable x and y are local to function add and main function do not know values of x and y. x and y are separate copy and altering which do not alter the value of variables a and b.

a and b are called actual arguments.
x and y are called formal arguments.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert