C Program To Find Armstrong Numbers Between Range using Function

In today’s video tutorial lets find all the Armstrong numbers or Narcissistic numbers between user entered range, using 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 = 370
It has 3 digits: 3, 7 and 0. So n = 3.
result = 33 + 73 + 03 = 27 + 343 + 0 = 370.
So the original number 370 is equal to the result. So it’s an Armstrong Number.

Example 2:
If number = 8208
It has 4 digits: 8, 2, 0 and 8. So n = 4.
result = 84 + 24 + 04 + 84 = ‭4096‬ + 16 + 0 + ‭4096‬ = 8208.
So the original number 8208 is equal to the result. So it’s an Armstrong Number.

Related Read:
Swap 2 Numbers Using a Temporary Variable: C
Function / Methods In C Programming Language
C Program to Check Armstrong Number
C Program to print Armstrong Numbers Between Two Integers

Video Tutorial: C Program To Find Armstrong Numbers Between Range using Function


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

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


To count number of 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 Range using Function

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

float armstrong(int);

int main()
{
    int count, start, end, temp;

    printf("Enter start and end values\n");
    scanf("%d%d", &start, &end);

    if(start > end)
    {
        temp = start;
        start= end;
        end  = temp;
    }

    printf("Armstrong numbers between %d and %d are\n", start, end);

    for(count = start; count <= end; count++)
    {
        if(count == armstrong(count))
        {
            printf("%d is an 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:
Enter start and end values
1
999
Armstrong numbers between 1 and 999 are
1 is an Armstrong number
2 is an Armstrong number
3 is an Armstrong number
4 is an Armstrong number
5 is an Armstrong number
6 is an Armstrong number
7 is an Armstrong number
8 is an Armstrong number
9 is an Armstrong number
153 is an Armstrong number
370 is an Armstrong number
371 is an Armstrong number
407 is an Armstrong number

Output 2:
Enter start and end values
500
99999
Armstrong numbers between 500 and 99999 are
1634 is an Armstrong number
8208 is an Armstrong number
9474 is an Armstrong number
54748 is an Armstrong number
92727 is an Armstrong number
93084 is an Armstrong number

Output 3:
Enter start and end values
1
99999
Armstrong numbers between 1 and 99999 are
1 is an Armstrong number
2 is an Armstrong number
3 is an Armstrong number
4 is an Armstrong number
5 is an Armstrong number
6 is an Armstrong number
7 is an Armstrong number
8 is an Armstrong number
9 is an Armstrong number
153 is an Armstrong number
370 is an Armstrong number
371 is an Armstrong number
407 is an Armstrong number
1634 is an Armstrong number
8208 is an Armstrong number
9474 is an Armstrong number
54748 is an Armstrong number
92727 is an Armstrong number
93084 is an Armstrong number

Output 4:
Enter start and end values
1
500
Armstrong numbers between 1 and 500 are
1 is an Armstrong number
2 is an Armstrong number
3 is an Armstrong number
4 is an Armstrong number
5 is an Armstrong number
6 is an Armstrong number
7 is an Armstrong number
8 is an Armstrong number
9 is an Armstrong number
153 is an Armstrong number
370 is an Armstrong number
371 is an Armstrong number
407 is an Armstrong number

Logic To Find Armstrong Numbers Between Range using Function

We ask the user to enter start and end value i.e., the range. Using for loop we iterate through all the numbers between start and end. For each iteration of for loop, variable count holds the number which we need to check if its Armstrong number or not.

Value of count is passed to function armstrong. Function armstrong counts the number of digits present in the integer number(we store it inside variable n) and then multiplies all the individual digits of the number by n and adds them. The sum is returned back to the calling function.

If value present in count and the value returned by armstrong method are equal, then the value present in count is an Armstrong number and will be displayed on the console window.

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 print Armstrong Numbers Between Two Integers

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 Reverse a Number
Basic Arithmetic Operations In C
while loop in C programming
if else statement in C
Calculate Sum of Digits: C Program
C Program to Check Armstrong Number
C Program to print Armstrong Numbers between 1 and 500

C Program to print Armstrong Numbers Between Two User Entered Integers


[youtube https://www.youtube.com/watch?v=lDc4Lx5-Ltc]

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


To count the digits in a number

        num = temp = count;
        n   = sum  = 0;

        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 print Armstrong Numbers Between Two User Entered Integers

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

int main()
{
    int count, limit, rem, temp, num, n;
    float sum;

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

    printf("\nArmstrong numbers between %d and %d\n", count, limit);

    while(count <= limit)
    {
        num = temp = count;
        n   = sum  = 0;

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

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

        if(count == sum)
        {
            printf("%d is Armstrong number\n", count);
        }

        count++;
    }

    return 0;
}

Output:
Enter 2 integer numbers
1
99999

Armstrong numbers between 1 and 99999
1 is Armstrong number
2 is Armstrong number
3 is Armstrong number
4 is Armstrong number
5 is Armstrong number
6 is Armstrong number
7 is Armstrong number
8 is Armstrong number
9 is Armstrong number
153 is Armstrong number
370 is Armstrong number
371 is Armstrong number
407 is Armstrong number
1634 is Armstrong number
8208 is Armstrong number
9474 is Armstrong number
54748 is Armstrong number
92727 is Armstrong number
93084 is Armstrong number

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

Insertion of Records into Database using Forms: PHP & MySQL

Video tutorial illustrates INSERTION of records into database using simple HTML form and minimal PHP scripting.

First look at these short videos:
Connect to the database
Simple / basic insertion operation

Form and PHP script, both in one file
index.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
< ?php
include_once('db.php');
 
if($_POST['name'])
{
  $name = $_POST['name'];
 
  if(mysql_query("INSERT INTO apple VALUES('','$name')"))
echo "Successful Insertion!";
  else
echo "Please try again";
}
?>
 
<form action="." method="POST">
Name: <input type="text" name="name"/><br />
<input type="submit" value=" Enter "/>
</form>

. in action property of html form simply means that the user entered values will be passed on to itself.
Since we are using . in the action property, we are making use of user entered values in the same file by using if($_POST[‘name’]) PHP code.
we can even use if( isset($_POST[‘name’])) isset() is a PHP function to check whether a variable has some value or not.

Video Tutorial: Insertion of Records into Database using Forms: PHP & MySQL


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

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



To know the differences between GET and POST methods:
GET Method In Action: Simple Form
POST Method In Action: Simple Form

Data in the database, as shown in the video:
1. Google
2. Apple
3. Microsoft
4. Oracle
5. Technotip

Read Keyboard Input: Java

This video tutorial shows how to read user input from Keyboard in Java.

In this tutorial we illustrate using a relatively simple program, where user is asked to input some string and it is read by the program and then out put to the console window. Simple java program.

Import
We have to import io package, so that we can make use of DataInputStream Class present in io package.

    import java.io.*;

Creating Object
Next, we create an object of the inbuilt class DataInputStream. And not that it takes a parameter System.in for reading keyboard inputs.

DataInputStream d = new DataInputStream(System.in);

try catch
To know more about try catch and finally read: Try, Catch and Finally in Java.

Since user can give any kind of unwanted input, we should write those code (which may rise exception) inside the try block.
and write the corresponding catch statement.

try
{
System.out.println("Entered String is: "+d.readLine());
}
catch(Exception e)
{
System.out.println(e);
}

Video Tutorial: Read Keyboard Input: Java


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

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




Full Source Code:(keyboardInput.java)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.io.*;
 
class keyboardInput
{
public static void main(String args[])
{
DataInputStream d = new DataInputStream(System.in);
 
System.out.println("Enter a string");
try
{
System.out.println("Entered String is: "+d.readLine());
}
catch(Exception e)
{
System.out.println(e);
}
 
}
}

Output:
Enter a string
Microsoft Windows 8
Entered String is: Microsoft Windows 8

Find Position of Left most Vowel: JavaScript

Develop and demonstrate a xhtml file which uses javascript program to find the first occurrence or the left most vowel in the user entered string.
Input: A String.
Output: Position of the left most or first occurrence of the vowel in the user entered string.

Video Explaining The JavaScript Code:


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

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


Work Flow:
Step 1: Prompt the user to enter a string, store the user entered string in a variable called str.

Step 2: Convert the string in str to upper case using toUpperCase function. Now the original value of str is lost and its equivalent upper case string has been stored.
Ex: str = str.toUpperCase();

Step 3: Now find the length of the entered string using length function. i.e., variableName.length; Ex: str.length;

Step 4: Using for loop, loop through the string from 0 to the string length(which is calculated in Step 3).

Step 5: Using chatAt function fetch one character at a time from str and store it in variable chr. Ex: chr = str.charAt(i);

Step 6: Now compare the value in chr with upper case vowels: A, E, I, O, U.

Step 7: If chr matches any vowel then break out of for loop.

Step 8: If the value of i after coming out of the for loop, is less than string length then print the value of i which is the position of the left most occurrence of the vowel. If the value of i is greater than or equal to string length, then Vowel not found in the entered string.
Javascrip coding explained in above video:

 

 
Finding Left most Vowel in the Input String
 
 

Input/Output:
If Microsoft is the input string, then the position of the first occurrence of vowel is 2. i.e., i.
For Oracle, O is a vowel, so the position is 1.
For Technotip, e is the first vowel, so the position is 2.
For any input which do not have a vowel in it, the above javascript program outputs this statement: No vowel found in the entered string.