Write a C program to find duplicate numbers in an array

#include <stdio.h>

int main() {
    int data[] = {1, 3, 4, 5, 3, 6, 7, 5, 8, 11, 3};
    int len = sizeof(data) / sizeof(data[0]);
    printf("Length of array: %d\n", len);

    int i, j, count = 0, flag = 0;

    for (i = 0; i < len; i++) {
        for (j = i + 1; j < len; j++) {
            if (data[i] == data[j]) {
                count++;
                flag = 1;
                printf("Duplicate number: %d\n", data[j]);
            }
        }
    }

    if (!flag) {
        printf("No duplicate numbers found.\n");
    }

    return 0;
}

Output

Length of array: 11
Duplicate number: 3
Duplicate number: 3
Duplicate number: 5
Duplicate number: 3

Write a C program to remove duplicate numbers from the given array

#include <stdio.h>

int main() {
    int data[] = {1, 3, 4, 5, 3, 6, 7, 5, 8, 11, 3};
    int len = sizeof(data) / sizeof(data[0]);
    printf("Length of original array: %d\n", len);

    int i, j, k;

    for (i = 0; i < len; ++i) {
        for (j = i + 1; j < len;j++) {
            if (data[j] == data[i]) {
                for (k = j; k < len; ++k) {
                    data[k] = data[k + 1];
                }
                --len;
            } 

        }
    }

    printf("Array after removing duplicates: ");
    for (i = 0; i < len; ++i) {
        printf("%d ", data[i]);
    }
    printf("\n");

    return 0;
}

Length of original array: 11
Array after removing duplicates: 1 3 4 5 6 7 8 11 

Method 2

#include <stdio.h>

int main() {
    int data[] = {1, 3, 4, 5, 3, 6, 7, 5, 8, 11, 3};
    int len = sizeof(data) / sizeof(data[0]);
    printf("Length of array: %d\n", len);

    int i, j, k, is_duplicate;
    int unique[len]; // Array to store unique elements
    int unique_len = 0; // Length of the unique array

    // Iterate through the array to find unique elements
    for (i = 0; i < len; i++) {
        is_duplicate = 0;
        // Check if the current element is already in the unique array
        for (j = 0; j < unique_len; j++) {
            if (data[i] == unique[j]) {
                is_duplicate = 1;
                break;
            }
        }
        // If the element is not a duplicate, add it to the unique array
        if (is_duplicate == 0) {
            unique[unique_len++] = data[i];
        }
    }

    // Print the array with duplicates removed
    printf("Array with duplicates removed: ");
    for (k = 0; k < unique_len; k++) {
        printf("%d ", unique[k]);
    }
    printf("\n");

    return 0;
}

Output

Length of array: 11
Array with duplicates removed: 1 3 4 5 6 7 8 11 

Write a program for a little-endian and big-endian

#include <stdio.h>

typedef union {
    unsigned int n;
    char bytes[4];
} EndianCheck;

int main() {
    EndianCheck endianCheck;
    endianCheck.n = 0x11223344;

    // Check if the least significant byte (LSB) is stored at the first byte (0x44)
    if (endianCheck.bytes[0] == 0x44) {
        printf("System is little-endian\n");
    } else {
        printf("System is big-endian\n");
    }

    printf("Least significant byte (LSB): 0x%x\n", endianCheck.bytes[0]);

    return 0;
}

Output

System is little-endian
Least significant byte (LSB): 0x44

C program that converts between little endian and big endian formats

#include <stdio.h>
#include <inttypes.h>
//Function to change one endian to another
uint32_t ChangeEndianness(uint32_t u32Value)
{
    uint32_t u32Result = 0;
    u32Result |= (u32Value & 0x000000FF) << 24;
    u32Result |= (u32Value & 0x0000FF00) << 8;
    u32Result |= (u32Value & 0x00FF0000) >> 8;
    u32Result |= (u32Value & 0xFF000000) >> 24;
    return u32Result;
}
int main()
{
    uint32_t u32CheckData  = 0x11223344;
    uint32_t u32ResultData =0;
    //swap the data
    u32ResultData = ChangeEndianness(u32CheckData);
    //converted data
    printf("0x%x\n",u32ResultData);
    return 0;
}

Output

0x44332211

Can you write a program to reverse a given 32-bit hexadecimal value

#include <stdio.h>

int main() {
    unsigned int hexValue = 0xaabbccdd;
    unsigned int reversedValue = 0;

    // Reverse the hexadecimal value using bitwise operations
    reversedValue |= (hexValue & 0x000000FF) << 24;
    reversedValue |= (hexValue & 0x0000FF00) << 8;
    reversedValue |= (hexValue & 0x00FF0000) >> 8;
    reversedValue |= (hexValue & 0xFF000000) >> 24;

    printf("Original Hex Value: 0x%08x\n", hexValue);
    printf("Reversed Hex Value: 0x%08x\n", reversedValue);

    return 0;
}

Output

Original Hex Value: 0xaabbccdd
Reversed Hex Value: 0xddccbbaa

Can you write a program to reverse a given 32-bit hexadecimal value using function?

#include <stdio.h>
#include <stdint.h>

uint32_t reverseHex(uint32_t num) {
    // Extracting individual bytes and swapping them
    uint32_t reversed = ((num & 0x000000FF) << 24) |
                        ((num & 0x0000FF00) << 8)  |
                        ((num & 0x00FF0000) >> 8)  |
                        ((num & 0xFF000000) >> 24);
    return reversed;
}

int main() {
    uint32_t inputHex = 0xaabbccdd;
    
    printf("Input hex value: 0x%x\n", inputHex);
    
    uint32_t reversedHex = reverseHex(inputHex);
    
    printf("Reversed hex value: 0x%x\n", reversedHex);
    
    return 0;
}

Output

Input hex value: 0xaabbccdd
Reversed hex value: 0xddccbbaa

Write a C program to check if a given number is prime or not

#include <stdio.h>

int main() {
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);
    
    int flag = 1;
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            flag = 0; // Not prime
            break;    // No need to check further
        }
    }

    if (flag == 1 && n > 1) {
        printf("%d is a prime number.\n", n);
    } else {
        printf("%d is not a prime number.\n", n);
    }

    return 0;
}

Write a C program to reverse a given string

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

char Data[] = "MEVIHUB.COM";
char Rev[12]; // Adjusted size to accommodate the reversed string and null terminator

int main()
{
    int len = strlen(Data);
    int count = len - 1; // Adjusted count to start from the last character of Data

    printf("Len = %d Count = %d\n", len, count);

    for (int i = 0; i < len; i++)
    {
        Rev[i] = Data[count];
        count--;
    }

    Rev[len] = '\0'; // Added null terminator at the end of Rev

    printf("\nReversed String = %s\n", Rev);

    return 0;
}

Write a C program to reverse a given string without using any standard library functions like strlen or strrev

#include <stdio.h>

char Rev[12]; // Adjusted size to accommodate the reversed string and null terminator

int Str_len(char *str)
{
    int len = 0;
    while (*str != '\0')
    {
        len++;
        str++;
    }
    return len;
}

int main()
{
    char Data[] = "MEVIHUB COM";
    int len = Str_len(Data);
    int count = len - 1; // Adjusted count to start from the last character of Data

    printf("Len = %d Count = %d\n", len, count);

    for (int i = 0; i < len; i++)
    {
        Rev[i] = Data[count];
        count--;
    }

    Rev[len] = '\0'; // Added null terminator at the end of Rev

    printf("\nReversed String = %s\n", Rev);

    return 0;
}

Output

Len = 11 Count = 10

Reversed String = MOC BUHIVEM

Write a C program that toggles the 3rd bit in a given byte

#include <stdio.h>

int main() {
    unsigned char byte = 0x0A; // Example byte: 00001010 in binary

    // Toggle the 3rd bit
    byte ^= (1 << 2); // Bit numbering starts from 0

    // Output the result
    printf("Byte after toggling the 3rd bit: 0x%x\n", byte);

    return 0;
}

Output

Byte after toggling the 3rd bit: 0x6

Write a program to set clear and toggle bits in c

#include <stdio.h>

// Function to set a bit at position 'pos'
int setBit(int num, int pos) {
    return num | (1 << pos);
}

// Function to toggle a bit at position 'pos'
int toggleBit(int num, int pos) {
    return num ^ (1 << pos);
}

// Function to delete (clear) a bit at position 'pos'
int clearBit(int num, int pos) {
    return num & ~(1 << pos);
}

int main() {
    int num = 4; // Initial number (42 in decimal)
    
    int  setbitValue =setBit(4,2);
    int ToglebitValue = toggleBit(5,3);
    int ClearbitValue = clearBit(6,4);
    
    // Set bit at position 2
    printf("After setting bit 2: %d\n", setbitValue);

    // Toggle bit at position 3
    printf("After toggling bit 3: %d\n", ToglebitValue);

    // Clear bit at position 4
    printf("After clearing bit 4: %d\n", ClearbitValue);

    return 0;
}

Output

After setting bit 2: 4
After toggling bit 3: 13
After clearing bit 4: 6

Write a C program to demonstrate how to set, clear, and toggle individual bits in a number using macros

#include <stdio.h>

// Macro to set a bit at position 'pos'
#define SET_BIT(num, pos) ((num) |= (1 << (pos)))

// Macro to clear (reset) a bit at position 'pos'
#define CLEAR_BIT(num, pos) ((num) &= ~(1 << (pos)))

// Macro to toggle a bit at position 'pos'
#define TOGGLE_BIT(num, pos) ((num) ^= (1 << (pos)))

int main() {
    int num = 13; // Initial number (13 in decimal, 1101 in binary)

    // Set bit at position 2
    SET_BIT(num, 2);
    printf("After setting bit 2: %d\n", num); // Output: 13 (decimal), 1101 (binary) -> 13 (decimal), 1111 (binary)

    // Clear bit at position 3
    CLEAR_BIT(num, 3);
    printf("After clearing bit 3: %d\n", num); // Output: 13 (decimal), 1111 (binary) -> 5 (decimal), 101 (binary)

    // Toggle bit at position 1
    TOGGLE_BIT(num, 1);
    printf("After toggling bit 1: %d\n", num); // Output: 5 (decimal), 101 (binary) -> 7 (decimal), 111 (binary)

    return 0;
}

Output

After setting bit 2: 13
After clearing bit 3: 5
After toggling bit 1: 7

Write a program to find missing Numbers in the given array

#include <stdio.h>

int main()
{
    int miss[] = {1, 3, 5, 6, 9, 10, 11};
    int len = sizeof(miss) / sizeof(miss[0]);
    
    printf("Len: %d\n", len);

    int max = miss[0]; // find largest number
    for(int i = 0; i < len; i++)
    {
        if(miss[i] > max)
        {
            max = miss[i];
        }
    }
    printf("Max: %d\n", max);

    printf("Missing numbers: ");
    for(int i = 1; i <= max; i++) // Iterate up to max + 1
    {
        int found = 0; // flag to check if i is found in miss

        // Check if i is present in miss
        for(int j = 0; j < len; j++)
        {
            if(miss[j] == i)
            {
                found = 1; // set found flag to true
                break;
            }
        }

        // If i is not found, print it as missing
        if(found == 0)
        {
            printf("%d ", i);
        }
    }
    printf("\n");

    return 0;
}

Write a C program to store student information using structures. Each student should have a name, hall ticket number, and total marks. Implement the program to store the details of 10 students and display their information.

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

struct Student {
    char name[50];
    int hallTicketNumber;
    float totalMarks;
};

int main() {
    struct Student students[10];

    strcpy(students[0].name, "Devilal");
    students[0].hallTicketNumber = 0x11A22;
    students[0].totalMarks = 55;

    printf("Student 1:\n");
    printf("Name: %s\n", students[0].name);
    printf("Hall Ticket Number: %X\n", students[0].hallTicketNumber);
    printf("Total Marks: %.2f\n", students[0].totalMarks);

    return 0;
}

Output

Student 1:
Name: Devilal
Hall Ticket Number: 11A22
Total Marks: 55.00

Write a C program to demonstrate passing a structure to a function

#include <stdio.h>

// Structure definition for a student
struct Student {
    char name[50];
    int rollNumber;
    float marks;
};

// Function to display details of a student
void displayStudent(struct Student s) {
    printf("Name: %s\n", s.name);
    printf("Roll Number: %d\n", s.rollNumber);
    printf("Marks: %.2f\n", s.marks);
}

int main() {
    // Declare and initialize a structure variable
    struct Student student1 = {"Devilal", 101, 85.5};

    // Call the function to display student details
    printf("Student Details:\n");
    displayStudent(student1);

    return 0;
}

Output

Student Details:
Name: Devilal
Roll Number: 101
Marks: 85.50

Write a program for the void pointer in c

#include <stdio.h>

int main() {
    int intValue = 10;
    float floatValue = 3.14;
    char charValue = 'A';

    // Declaring a void pointer
    void *ptr;

    // Assigning the address of intValue to the void pointer
    ptr = &intValue;

    // Dereferencing the void pointer and casting it back to int
    printf("Value of intValue: %d\n", *((int*)ptr));

    // Assigning the address of floatValue to the void pointer
    ptr = &floatValue;

    // Dereferencing the void pointer and casting it back to float
    printf("Value of floatValue: %.2f\n", *((float*)ptr));

    // Assigning the address of charValue to the void pointer
    ptr = &charValue;

    // Dereferencing the void pointer and casting it back to char
    printf("Value of charValue: %c\n", *((char*)ptr));

    return 0;
}

Output

Value of intValue: 10
Value of floatValue: 3.14
Value of charValue: A

By Devilal

Leave a Reply

Your email address will not be published. Required fields are marked *