Skip to main content

Featured

C++ Program to Perform Linear Search on a Vector

  C++ Program to Perform Linear Search on a Vector Introduction In this C++ program, we will learn how to perform a Linear Search on a vector. The program first takes the size of the vector and its elements as input. Then it asks the user for the element to search. If the element is found, it displays the index where it is located. Otherwise, it displays a message indicating that the element is not found. C++ Program #include<bits/stdc++.h> using namespace std; int main() { int num, search, found = 0; cout << "Enter the size of the vector:" << endl; cin >> num; vector<int> v(num); cout << "Enter " << num << " elements in vector:" << endl; for(int i = 0; i < num; i++) { cin >> v[i]; } cout << "Enter element that you want to search:" << endl; cin >> search; for(int i = 0; i < num; i++) { ...

C Program to Check Armstrong Number (Any Digits)

C Program to Check Armstrong Number (Any Digits)

✅ C Program to Check Armstrong Number (Any Digits)

#include <stdio.h>

int main()
{
    int num, temp, remainder, sum = 0, n = 0;

    printf("Enter the number:\n");
    scanf("%d", &num);

    temp = num;

    // count digits
    int digits = num;
    while(digits != 0) {
        digits /= 10;
        n++;
    }

    temp = num;
    while(num > 0) {
        remainder = num % 10;

        // calculate remainder^n manually
        int power = 1;
        for(int i = 0; i < n; i++) {
            power *= remainder;
        }

        sum += power;
        num /= 10;
    }

    if(temp == sum)
        printf("%d is an Armstrong number\n", temp);
    else
        printf("%d is not an Armstrong number\n", temp);

    return 0;
}
  

๐Ÿ“˜ Explanation:

This program checks if a number is an Armstrong number without using the pow() function from math.h. Instead, it calculates the power of each digit manually with a for loop.

  • First count the total digits of the number.
  • Extract each digit using modulus (%).
  • Compute digit^n by multiplying in a loop.
  • Add all powered digits.
  • Compare with the original number → if equal, it’s an Armstrong number.

๐Ÿงพ Sample Output:

Enter the number:
9474
9474 is an Armstrong number

Enter the number:
123
123 is not an Armstrong number
  

๐Ÿ”‘ Keywords:

Armstrong number without pow, C program Armstrong check manually, Armstrong number code with loop, interview C programs, Armstrong logic in C

๐Ÿ“Œ Hashtags:

#CProgramming #ArmstrongNumber #WithoutPow #CodingForBeginners #InterviewPrep

๐Ÿ” Search Description:

This C program checks whether a number is an Armstrong number without using pow(). Instead, digit powers are calculated manually with a loop.

Comments

Popular Posts

๐ŸŒ™