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 Remove Duplicates, Ignore Spaces, and Sort Characters in Descending Order

Remove Duplicates and Sort Characters in Descending Order - C Program

๐Ÿš€ C Program to Remove Duplicates, Ignore Spaces, and Sort Characters in Descending Order

๐Ÿ“„ Source Code:


#include<stdio.h>
#include<string.h>

int present(char a[], char ch, int len)
{
    for (int i = 0; i < len; i++)
    {
        if (a[i] == ch)
        {
            return 1;
        }
    }
    return 0;
}

void des(char a[], int len)
{
    for (int i = 0; i < len - 1; i++)
    {
        for (int j = i + 1; j < len; j++)
        {
            if (a[i] < a[j])
            {
                char temp = a[i];
                a[i] = a[j];
                a[j] = temp;
            }
        }
    }
}

int main()
{
    char str[200], result[200];
    int j = 0;
    fgets(str, sizeof(str), stdin);
    int len = strlen(str);
    if (str[len - 1] == '\n')
    {
        str[len - 1] = '\0';
    }

    for (int i = 0; str[i] != '\0'; i++)
    {
        if (str[i] != ' ' && !present(result, str[i], j))
        {
            result[j++] = str[i];
        }
    }

    des(result, j);
    result[j] = '\0';

    printf("Resulting string: %s\n", result);
}
    

๐Ÿ“˜ Deep Explanation:

This C program performs three core operations on a string entered by the user:

  • ๐Ÿ”น 1. It removes duplicate characters from the string.
  • ๐Ÿ”น 2. It ignores spaces.
  • ๐Ÿ”น 3. It sorts the unique characters in descending order.
  • ๐Ÿ”ธ present() checks if a character is already in the result array.
  • ๐Ÿ”ธ des() uses a simple bubble sort in descending (Z to A) order.
  • ๐Ÿ”ธ fgets() is used instead of scanf() to read spaces.
  • ๐Ÿ”ธ Only unique characters are printed after sorting.

๐Ÿงช Sample Output:


Input:
Hello World

Output:
Resulting string: roledWH
    

Comments

Popular Posts

๐ŸŒ™