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...

Check If Two Numbers Are Anagrams - C Program

Check If Two Numbers Are Anagrams - C Program

✅ C Program to Check if Two Numbers Are Anagrams

#include <stdio.h>

int countDigits(int n, int count[]) {
    while (n > 0) {
        count[n % 10]++;
        n /= 10;
    }
}

int areAnagrams(int num1, int num2) {
    int count1[10] = {0};
    int count2[10] = {0};

    countDigits(num1, count1);
    countDigits(num2, count2);

    for (int i = 0; i < 10; i++) {
        if (count1[i] != count2[i])
            return 0; // Not anagrams
    }
    return 1; // Anagrams
}

int main() {
    int num1, num2;
    printf("Enter two numbers: ");
    scanf("%d %d", &num1, &num2);

    if (areAnagrams(num1, num2))
        printf("Yes, the numbers are anagrams of each other.\n");
    else
        printf("No, the numbers are not anagrams.\n");

    return 0;
}
  

๐Ÿ“˜ Explanation:

This C program checks if two numbers are anagrams of each other. An anagram means both numbers contain the same digits in any order. Here's how it works:

  • countDigits() function builds a frequency count array for digits 0–9.
  • Two arrays are created: one for each number's digit frequency.
  • If both frequency arrays match, the numbers are anagrams.
  • Otherwise, they are not.

๐Ÿงพ Sample Output:

Enter two numbers: 121 211
Yes, the numbers are anagrams of each other.

Enter two numbers: 123 456
No, the numbers are not anagrams.
  

๐Ÿ”‘ Keywords:

Anagram numbers in C, number digit match, frequency count in C, C program for number comparison, logic-based C program, digit counter

๐Ÿ“Œ Hashtags:

#CProgramming #AnagramNumbers #InterviewLogic #DigitFrequency #BeginnerC #CodeWithExplanation

๐Ÿ” Search Description:

This C program checks if two numbers are anagrams by comparing the frequency of digits in both. Efficient and beginner-friendly approach for digit-based problems in C.

Comments

Popular Posts

๐ŸŒ™