Skip to main content

Featured

C Program to Check Prime Number Using Efficient Logic

  Introduction A prime number is a number that has exactly two distinct positive divisors: 1 and itself. In this program, we check whether a given number is prime or not using a simple and efficient logic. This type of program is commonly used in mathematics, competitive programming, and basic algorithm learning for beginners in C programming. Problem Statement The task is to write a C program that determines whether a given integer is a prime number or not. The program takes a single integer input from the user and analyzes its divisibility. If the number has no divisors other than 1 and itself, it should be identified as a prime number; otherwise, it is not prime. This problem is important in number theory and has practical relevance in areas such as cryptography, data validation, and algorithm design.  Algorithm / Logic Explanation To check whether a number is prime, we need to verify that it is not divisible by any number other than 1 and itself. The algorithm follows a si...

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

๐ŸŒ™