Skip to main content

Featured

C Program to Solve Two Sum Using Brute Force (With Algorithm & Output)

 Introduction The Two Sum problem is a popular coding interview question where we must find two indices of an array whose values add up to a given target. This program demonstrates a simple brute-force solution in C using nested loops and dynamic memory allocation. Problem Statement Given an integer array and a target value, return the indices of the two numbers such that they add up to the target. Each input has exactly one solution, and the same element cannot be used twice. The result should return the indices, not the values. If no solution exists, return NULL.  Algorithm / Logic Explanation Start the program. Traverse the array using a loop from index 0 to numsSize - 1 . Inside this loop, use another loop starting from i + 1 to numsSize - 1 . For every pair (i, j) , check if nums[i] + nums[j] == target . If condition becomes true: Allocate memory for 2 integers using malloc() . Store indices i and j . Set returnSize = 2 . Return the result poi...

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

๐ŸŒ™