Coding Practice
Showing posts with label C Programming. Show all posts
Showing posts with label C Programming. Show all posts

Write a C program to convert Binary to Decimal number system

Convert Binary to Decimal number system
Sample Output
Enter binary number: 1111111

Decimal Number: 127
Source Code
// Convert Binary to Decimal number system

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

int main()
{
    int i, bin_length, power, decimal_number = 0;
    char binary_number[100];

    printf("Enter binary number: ");
    scanf("%s", binary_number);

    // Length of binary_number string
    for(i = 0; binary_number[i] != '\0'; i++);
    bin_length = i;

    /*
    Decimal to Binary
    Example:
    Binary number = 1001

    2^3     2^2     2^1     2^0
    1       0       0       1
    8           +           1

    Decimal number = 9

    */

    for(i = 0, power = (bin_length - 1); i < bin_length; i++, power--)
    {
        if(binary_number[i] == '1')
        {
            decimal_number += pow(2, power);
        }
    }

    printf("\nDecimal Number: %d\n\n", decimal_number);

    return 0;
}
Sample Output
Enter binary number: 01101110

Decimal Number: 110

Write a C program to convert Binary to Octal number system

Convert Binary to Octal number system
Sample Output
Enter binary number: 11010

Octacl Number: 32
Source Code
// Convert Binary to Octal number system

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

int main()
{
    int i, bin_length, dec_length, remainder[100];
    int power, decimal_number = 0, octal_number, temp;
    char binary_number[100];

    printf("Enter binary number: ");
    scanf("%s", binary_number);

    // Length of binary_number string
    for(i = 0; binary_number[i] != '\0'; i++);
    bin_length = i;

    /*
    Decimal to Binary
    Example:
    Binary number = 1001

    2^3     2^2     2^1     2^0
    1       0       0       1
    8           +           1

    Decimal number = 9

    */

    for(i = 0, power = (bin_length - 1); i < bin_length; i++, power--)
    {
        if(binary_number[i] == '1')
        {
            decimal_number += pow(2, power);
        }
    }

    i = 0;
    while(decimal_number != 0)
    {
        remainder[i] = (decimal_number % 8);
        decimal_number /= 8;

        i++;
    }
    dec_length = i;

    for(i = (dec_length - 1); i >= 0; i--)
    {
        octal_number +=  (remainder[i] * pow(10, i));
    }

    printf("\nOctacl Number: %d\n\n", octal_number);

    return 0;
}
Sample Output
Enter binary number: 1111111

Octacl Number: 177

Write a C program to find two's complement of a binary number

Find two's complement of a binary number
Sample Output
Enter binary number: 01101110

One's Complement: 10010001
Two's Complement: 10010010
Source Code
// Find two's complement of a binary number

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

int main()
{
    int i, bin_length;
    char carry = '1', binary_number[100], ones_comp[100], twos_comp[100];
input:
    printf("Enter binary number: ");
    scanf("%s", binary_number);

    // Length of binary_number string
    for(i = 0; binary_number[i] != '\0'; i++);
    bin_length = i;

    for(i = 0; i < bin_length; i++)
    {
        if(binary_number[i] == '0')
        {
            ones_comp[i] = '1';
        }
        else if(binary_number[i] == '1')
        {
            ones_comp[i] = '0';
        }
        else
        {
            printf("Unexpected character! Please enter a valid input...\n\n");
            goto input;
        }
    }
    ones_comp[i] = '\0'; // End of string

    for(i = (bin_length - 1); i >= 0; i--)
    {
        if(ones_comp[i] == '1' && carry == '1')
        {
            twos_comp[i] = '0';
        }
        else if(ones_comp[i] == '0' && carry == '1')
        {
            twos_comp[i] = '1';
            carry = '0';
        }
        else if(carry == '0')
        {
            twos_comp[i] = ones_comp[i];
        }
    }

    printf("\nOne's Complement: %s\nTwo's Complement: %s\n\n", ones_comp, twos_comp);

    return 0;
}
Sample Output
Enter binary number: 00010100    

One's Complement: 11101011
Two's Complement: 11101100

Write a C program to find one's complement of a binary number

Find one's complement of a binary number
Sample Output
Enter binary number: 110101

Binary Number: 110101
One's Complement: 001010
Source Code
// Find one's complement of a binary number

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

int main()
{
    int i, bin_length;
    char binary_number[100], ones_comp[100];
input:
    printf("Enter binary number: ");
    scanf("%s", binary_number);

    // Length of binary_number string
    for(i = 0; binary_number[i] != '\0'; i++);
    bin_length = i;

    for(i = 0; i < bin_length; i++)
    {
        if(binary_number[i] == '0')
        {
            ones_comp[i] = '1';
        }
        else if(binary_number[i] == '1')
        {
            ones_comp[i] = '0';
        }
        else
        {
            printf("Unexpected character found! Please enter a valid input...\n\n");
            goto input;
        }
    }
    ones_comp[i] = '\0'; // End of string by creating a NULL value.

    printf("\nBinary Number: %s\nOne's Complement: %s\n\n", binary_number, ones_comp);

    return 0;
}
Sample Output
Enter binary number: 1010

Binary Number: 1010
One's Complement: 0101

Write a C program to print Fibonacci series up to n terms

Print Fibonacci series up to n terms
Sample Output
Enter a limit: 10

Fibonacci series: 0 1 1 2 3 5 8 13 21 34
Source Code
// Print Fibonacci series up to n terms

#include<stdio.h>

int main()
{
    int i, limit, first = 0, second = 1, last;

    /*
    FirstNum + LastNum = LastNum
    Fibonacci series: 0 1 1 2 3 5 8 13. . .
    */

    printf("Enter a limit: ");
    scanf("%d", &limit);

    printf("\nFibonacci series: ");
    for(i = 1; i <= limit; i++)
    {
        printf("%d ", first);

        last = first + second;
        first = second;
        second = last;
    }
    printf("\n\n");

    return 0;
}
Sample Output
Enter a limit: 11

Fibonacci series: 0 1 1 2 3 5 8 13 21 34 55

Write a C program to print all Strong numbers between 1 to n

Print all Strong numbers between 1 to n
Sample Output
Enter a limit for Strong numbers: 150

All Strong numbers between 1 to n: 1 2 145
Source Code
// Print all Strong numbers between 1 to n

#include<stdio.h>
#include<math.h>

int main()
{
    int i, j, limit, number, numberClone, digit, sum = 0, factorial = 1;

    printf("Enter a limit for Strong numbers: ");
    scanf("%d", &limit);

    printf("\nAll Strong numbers between 1 to n: ");
    for(i = 1; i <= limit; i++)
    {
        number = numberClone = i;

        while(number != 0)
        {
            digit = (number % 10);
            number = (number / 10);

            // Sum of the factorial of the individual digits
            for(j = 1; j <= digit; j++)
            {
                factorial = (factorial * j);
            }
            sum = (sum + factorial);

            factorial = 1; // Reset value
        }

        if(numberClone == sum)
        {
            printf("%d ", numberClone);
        }

        sum = 0; // Reset value
    }
    printf("\n\n");

    return 0;
}
Sample Output
Enter a limit for Strong numbers: 50000

All Strong numbers between 1 to n: 1 2 145 40585

Write a C program to check whether a number is Strong number or not

Check whether a number is Strong number or not
Sample Output
Enter a number: 145

145 is a Strong number!
Source Code
// Check whether a number is Strong number or not

#include<stdio.h>
#include<math.h>

int main()
{
    int i, number, numberClone, digit, sum = 0, factorial = 1;

    printf("Enter a number: ");
    scanf("%d", &number);

    numberClone = number;

    while(number != 0)
    {
        digit = (number % 10);
        number = (number / 10);

        // Sum of the factorial of the individual digits
        for(i = 1; i <= digit; i++)
        {
            factorial = (factorial * i);
        }
        sum = (sum + factorial);

        factorial = 1; // Reset value
    }
    
    if(numberClone == sum)
    {
        printf("\n%d is a Strong number!\n\n", numberClone);
    }
    else
    {
        printf("\n%d is not a Strong number.\n\n", numberClone);
    }

    return 0;
}
Sample Output
Enter a number: 40585

40585 is a Strong number!

Write a C program to print all Armstrong numbers between 1 to n

Print all Armstrong numbers between 1 to n
Sample Output
Enter a limit for armstrong numbers: 900

All armstrong numbers 1 to 900: 1 2 3 4 5 6 7 8 9 153 370 371 407
Source Code
#include<stdio.h>
#include<math.h>

//Print all Armstrong numbers between 1 to n

int main()
{
    int i, limit, number, numberClone, digit, count = 0, newNumber = 0;

    printf("Enter a limit for armstrong numbers: ");
    scanf("%d", &limit);

    printf("\nAll armstrong numbers 1 to %d: ", limit);
    for(i = 1; i <= limit; i++)
    {
        number = i;
        numberClone = number;
        while(number != 0)
        {
            digit = (number%10);
            number /= 10;
            count++;
        }

        number = numberClone;
        while(number != 0)
        {
            digit = (number%10);
            number /= 10;

            newNumber += pow(digit, count);
        }

        if(numberClone == newNumber)
        {
            printf("%d ", newNumber);
        }

        count = 0;
        newNumber = 0;
    }
    printf("\n\n");

    return 0;
}
Sample Output
Enter a limit for armstrong numbers: 900

All armstrong numbers 1 to 900: 0 1 2 3 4 5 6 7 8 9 153 370 371 407

Take 5 numbers in an array and find all pairs of prime numbers from that array

Find all pair of prime numbers from a array

Sample Input Sample Output
2, 3, 4, 5, 6 2, 3
2, 5
3, 5
Sample Output
Enter 5 element: 2 3 4 5 6
2,3
2,5
3,5
Source Code
#include<stdio.h>

void main()
{
    int i, j, k = 0, isPrime, size = 5, arr[5], primeNum[5];

    printf("Enter 5 element: ");
    for(i = 0; i < size; i++)
    {
        scanf("%d", &arr[i]);
    }

    for(i = 0; i < size; i++)
    {
        if(arr[i] == 2)
        {
            primeNum[k++] = arr[i];
        }
        else
        {
            isPrime = 1;
            for(j = 2; j < arr[i]; j++)
            {
                if(arr[i] % j == 0)
                {
                    isPrime = 0;
                    break;
                }
            }
            if(isPrime == 1)
            {
                primeNum[k++] = arr[i];
            }
        }
    }
    if(k < 2)
    {
        printf("\nPair not possible!");
    }
    for(i = 0; i < ((k * (k+1))/2); i++)
    {
        for(j = i+1; j < k; j++)
        {
            printf("%d,%d\n", primeNum[i], primeNum[j]);
        }
    }
    printf("\n");

}
Sample Output
Enter 5 element: 2 3 5 7 11
2,3
2,5
2,7
2,11
3,5
3,7
3,11
5,7
5,11
7,11

Write a C Code to find total marks from given input...

Write a C Code to find total marks from given input: Attendance, Class test, Quiz test, Assignment, Midterm and Final Exam and find the grade using the following method:

  1. If Total mark >=80 then A+
  2. If Total mark >=70 then A
  3. If Total mark >=60 then A
  4. If Total mark >=50 then B
  5. Else Grade = F

Sample Input

Enter Attendance, Class test, Quiz test, Assignment, Midterm and Final Exam: 2.5 7.5 3.5 7 21.5 33

Sample Output

Total Mark: 75 & Grade: A

Sample Output
Enter Attendance, Class test, Quiz test, Assignment, Midterm and Final Exam: 2.5 7.5 3.5 7 21.5 33
Total Mark: 75.000000 & Grade: A
Source Code
#include<stdio.h>

int main()
{
    float attendance, ct, qt, assignment, mid_mark, final_mark, total_mark;

    printf("Enter Attendance, Class test, Quiz test, Assignment, Midterm and Final Exam: ");
    scanf("%f %f %f %f %f %f", &attendance, &ct, &qt, &assignment, &mid_mark, &final_mark);
    //Input: 2.5 7.5 3.5 7 21.5 33

    total_mark = (attendance + ct + qt + assignment + mid_mark + final_mark);

    if(total_mark >= 80 && total_mark <= 100)
    {
        printf("Total Mark: %f & Grade: A+\n\n", total_mark);
    }
    else if(total_mark >= 70 && total_mark <= 79)
    {
        printf("Total Mark: %f & Grade: A\n\n", total_mark);
    }
    else if(total_mark >= 60 && total_mark <= 69)
    {
        printf("Total Mark: %f & Grade: A-\n\n", total_mark);
    }
    else if(total_mark >= 50 && total_mark <= 59)
    {
        printf("Total Mark: %f & Grade: B\n\n", total_mark);
    }
    else
    {
        printf("Total Mark: %f & Grade: F\n\n", total_mark);
    }

    return 0;
}
Sample Output
Enter Attendance, Class test, Quiz test, Assignment, Midterm and Final Exam: 2.5 7.5 3.5 7 21.5 33
Total Mark: 75.000000 & Grade: A

Write a C Code to calculate D = 𝒃^𝟐 − 𝟒𝐚𝐜 and...

Write a C Code to calculate D = 𝒃^𝟐 − 𝟒𝐚𝐜 and show the result according to following conditions:

a. D>0 then Calculate X1, X2. Where 𝒙𝟏 = (−𝒃+√𝒃^𝟐−𝟒𝒂𝒄)/𝟐𝒂 and 𝒙𝟐 = (−𝒃−√𝒃^𝟐−𝟒𝒂𝒄)/𝟐𝒂

        b. D =0 then calculate X. Where 𝐱 = 𝒃^𝟐/𝟐𝒂

        c. D<0 then print “No Solution”.

Sample Input

        Enter a, b & c: 5 6 1

Sample Output

    X1: -0.2

    X2: -1
Sample Output
Enter a, b, & c: 5 6 1
X1: -0.200
x2: -1.000
Source Code
#include<stdio.h>
#include<math.h>

int main()
{
    float D, b, a, c, x, x1, x2;

    printf("Enter a, b, & c: ");
    scanf("%f %f %f", &a, &b, &c);

    D = ((b*b) - 4*a*c);

    if(D > 0)
    {
        x1 = (-b + sqrt(D))/(2*a);
        x2 = (-b - sqrt(D))/(2*a);

        printf("X1: %.3f\nx2: %.3f\n\n", x1, x2);
    }
    else if(D == 0)
    {
        x = (b*b)/(2*a);

        printf("X: %f\n\n");
    }
    else if(D < 0)
    {
        printf("No Solution\n\n");
    }
    return 0;
}
Sample Output
Enter a, b, & c: 5 6 1
X1: -0.200
x2: -1.000

Write a C program to solve the Drake equation...

Write a C program to solve the Drake equation N = R*fp*ne*fl*fi*fc*L. Where R, ne and L must be discrete value.

Sample Input

    Enter R, F(p), n(e), f(l), f(i), f(c), L: 5 0.25 20 0.2 0.15 0.11 150

Sample Output

    The number of communicative civilizations within the Milky Way today: 12.375

Sample Output
Enter R, F(p), n(e), f(l), f(i), f(c): 5 0.25 20 0.2 0.15 0.11 150
The number of communicative civilizations within the Milky Way today: 12.375
Source Code
#include<stdio.h>
#include<math.h>

int main()
{
    float N, fp, fl, fi, fc;
    int R, ne, L;

    printf("Enter R, F(p), n(e), f(l), f(i), f(c): ");
    scanf("%d %f %d %f %f %f %d", &R, &fp, &ne, &fl, &fi, &fc, &L); //Input: 5 0.25 20 0.2 0.15 0.11 150

    float n = (float)R*ne*L;
    N = n*fp*fl*fi*fc;

    printf("The number of communicative civilizations within the Milky Way today: %.3f\n\n", N);

    return 0;
}
Sample Output
Enter R, F(p), n(e), f(l), f(i), f(c): 5 0.25 20 0.2 0.15 0.11 150
The number of communicative civilizations within the Milky Way today: 12.375

Lab Task: MID TERM [20 Marks] | Assignment - June 2021

1. [5] Write a C program to solve the Drake equation N = R*fp*ne*fl*fi*fc*L. Where R, ne and L must be discrete value

Sample Input

    Enter R, F(p), n(e), f(l), f(i), f(c), L: 5 0.25 20 0.2 0.15 0.11 150

Sample Output

    The number of communicative civilizations within the Milky Way today: 12.375

2. [7] Write a C Code to calculate D = 𝒃^𝟐 − 𝟒𝐚𝐜 and show the result according to following conditions:

a. D>0 then Calculate X1, X2. Where 𝒙𝟏 = (−𝒃+√𝒃^𝟐−𝟒𝒂𝒄)/𝟐𝒂 and 𝒙𝟐 = (−𝒃−√𝒃^𝟐−𝟒𝒂𝒄)/𝟐𝒂

        b. D =0 then calculate X. Where 𝐱 = 𝒃^𝟐/𝟐𝒂

        c. D<0 then print “No Solution”.

Sample Input

        Enter a, b & c: 5 6 1

Sample Output

    X1: -0.2

    X2: -1

3. [8] Write a C Code to find total marks from given input: Attendance, Class test, Quiz test, Assignment, Midterm and Final Exam and find the grade using the following method:

  1. If Total mark >=80 then A+
  2. If Total mark >=70 then A
  3. If Total mark >=60 then A
  4. If Total mark >=50 then B
  5. Else Grade = F

Sample Input

Enter Attendance, Class test, Quiz test, Assignment, Midterm and Final Exam: 2.5 7.5 3.5 7 21.5 33

Sample Output

Total Mark: 75 & Grade: A

Mini Project Contact Management System | Based on C Language

 TABLE OF CONTENTS

1. Introduction

2. Background

3. Requirements & Design

4. Implementation

5. Result and Evaluation

6. Conclusion

7. References

8. Source Code

1. Introduction

Contact Management System is a simple console application that is developed in C Language. This program enables to store data and manage contact information. Those are essentially databases that track all (i.e. Name, Phone Number and Email address) information and communication based on user contacts.

2. Background

Features for this application: Login, Show Contact List, Insert/Add Contact, Delete Contact, Search Contact, Security Settings, Change Username and Password, Sort Contact, Log Out, Login Again or Exit program. For this project, all of the contents based on C Language.

3. Requirements & Design

Contact Management System is console base project without graphics. It is similar to the contact manager in cell phones. This mini project use file handling and data structures. For design this project I used array, function, if...else, for loop, nested loop, switch, case, and etc. Basically, this is an easy and fundamental level mini project for learning purposes. We used simple design for this project.

4. Implementation

Describe the program code in below for understanding this project-

4.1 Set Username and Password

If user run this project for first time, Set Username and Password is mandatory option to manage contact. After set Username and Password, this is not required for next time.

Sample output:

4.2 Login to manage contacts

After set Username and Password, Login to manage contacts is another mandatory option to manage contacts.

Sample output:

4.3.1 See Contact List

See Contact List can help to show all contact.

Sample output:

4.4.2 Add Contact

Add Contact can add Name, Phone Number and Email Address in Contact list.

Sample output:

4.5.3 Delete Contact

Delete Contact can delete Name, Phone Number and Email Address in Contact list on specific position. 

Sample output:

4.5.4 Search Contact

Search Contact can search by Name or Phone Number in Contact list for easily find people. 

Sample Output:

4.5.5 Sort Contact

Sort Contact can sort by Name or Phone Number on Contact list.

Sample Output:

4.5.6 Security Setting

For this project, the user can change Username and Password by Security Setting.

Sample Output:

4.5.8 Log Out

The user can log out from all features by Log Out option.

Sample Output:

5. Result and Evaluation

After run this project, we can see many features and add Name, Phone Number and Email address as Contact Management System. Here, the data will be stored in the ContactList.txt file. If the file is deleted, the user must lost his/her all data.

6. Conclusion

The principle behind constructing a Contact Management System is to effectively retrieve and implement any information. All the information related to a particular people can be linked and archived only to be retrieved later when they are required most.

7. References

This project based on Data Structure.

Source Code
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>

int size = 0, i = 0, j, position, option, isFound = 0, m, n;
FILE *fp;
char search[20];

struct contact
{
    char name[20], phone[20], email[30], username[20], password[20];

} list[100], temp;

int fileOpenRead();
int fileOpenWrite();
int readFile();
int mainMenu();
int main();
int forgetUserPass();

int optionMenu()
{
    printf("\n\n1. Main Menu\n\nEnter a following option: ");
    scanf("%d", &option);

    system("cls");

    switch(option)
    {
    case 1:
        mainMenu();
        break;

    default:
        printf("Invalid Input for Main Menu!\n\n");
        mainMenu();
        break;
    }
}

int aboutAuthor()
{
    printf("Rakibul Islam\n\n");

    optionMenu();
    system("cls");
}

int contactList()
{
    printf("Contact List");
    printf("\n------------\n\n");

    fileOpenRead();

    for(j = 0; j < size; j++)
    {
        printf("Name: %s\nPhone: %s\nEmail: %s\n\n", list[j].name, list[j].phone, list[j].email);
    }
    fclose(fp);

    optionMenu();
}

int optionMenuTwo()
{
    printf("\n\n1. Contact List\n2. Main Menu\n\nEnter a following option: ");
    scanf("%d", &option);

    system("cls");

    switch(option)
    {
    case 1:
        contactList();
        break;

    case 2:
        mainMenu();

    default:
        printf("Invalid Input for Main Menu!\n\n");
        mainMenu();
        break;
    }
}

int addContact()
{
    readFile();

    printf("Add New Contact");
    printf("\n---------------\n\n");

    fp = fopen("ContactList.txt", "wb");

    printf("Enter Name: ");
    scanf("%s", list[i].name);

    printf("Enter Phone Number: ");
    scanf("%s", list[i].phone);

    printf("Enter Email: ");
    scanf("%s", list[i].email);

    size++;
    i++;

    fileOpenWrite();
    readFile();
    printf("\n\n1. Contact List\n2. Add New Contact\n3. Main Menu\n\nEnter a following option: ");
    scanf("%d", &option);
    printf("\n");

    system("cls");

    switch(option)
    {
    case 1:
        contactList();
        break;

    case 2:
        addContact();
        break;

    case 3:
        mainMenu();
        break;

    default:
        printf("Invalid Input for Main Menu!\n\n");
        mainMenu();
        break;
    }
}

int deleteContact()
{
    printf("Delete Contact");
    printf("\n--------------\n\n");

    readFile();
    fileOpenRead();

    for(j = 0; j < size; j++)
    {
        printf("%d. Name: %s\nPhone: %s\nEmail: %s\n\n", j + 1, list[j].name, list[j].phone, list[j].email);
    }
    fclose(fp);

    printf("Enter a position to delete: ");
    scanf("%d", &position);

    if(position <= j && position > 0)
    {
        fileOpenRead();

        fp = fopen("ContactList.txt", "wb");

        for(j = position; j < size; j++)
        {
            strcpy(list[j - 1].name, list[j].name);
            strcpy(list[j - 1].phone, list[j].phone);
            strcpy(list[j - 1].email, list[j].email);
        }
        size--;
        i--;
        j--;

        fileOpenWrite();
        readFile();

        system("cls");

        printf("Deleted!");
        optionMenuTwo();
    }
    else
    {
        system("cls");

        printf("Invalid Position!");
        optionMenu();
    }
}

int searchContact()
{
    printf("Search Contact");
    printf("\n--------------\n\n");

    fileOpenRead();

    printf("Enter Name or Number: ");
    scanf("%s", search);
    printf("\n");

    for(j = 0; j < size; j++)
    {
        if((strcmp(search, list[j].name) == 0))
        {
            printf("Name: %s\nPhone: %s\nEmail: %s\n\n", list[j].name, list[j].phone, list[j].email);
            isFound = 1;
        }
        else if((strcmp(search, list[j].phone) == 0))
        {
            printf("Name: %s\nPhone: %s\nEmail: %s\n\n", list[j].name, list[j].phone, list[j].email);
            isFound = 1;
        }
    }
    fclose(fp);

    if(isFound == 1);
    else
    {
        printf("Not Found!\n\n");
    }
    optionMenu();
}

int SortByName()
{
    fileOpenRead();

    fp = fopen("ContactList.txt", "wb");

    for(m = 0; m < size - 1; m++)
    {
        for(n = 0; n < size - m - 1; n++)
        {
            if(strcmp(list[n].name, list[n + 1].name) > 0)
            {
                temp = list[n];
                list[n] = list[n + 1];
                list[n + 1] = temp;
            }
        }
    }
    fileOpenWrite();
    readFile();
    printf("Sorted!");
    optionMenuTwo();
}

int SortByNumber()
{

    fileOpenRead();
    fp = fopen("ContactList.txt", "wb");

    for(m = 0; m < size - 1; m++)
    {
        for(n = 0; n < size - m - 1; n++)
        {
            if(strcmp(list[n].phone, list[n + 1].phone) > 0)
            {
                temp = list[n];
                list[n] = list[n + 1];
                list[n + 1] = temp;
            }
        }
    }
    fileOpenWrite();
    readFile();
    printf("Sorted!");
    optionMenuTwo();
}

int SortContact()
{
    printf("Sort Contact");
    printf("\n--------------\n\n");

    readFile();

    printf("1. Sort by Name\n2. Sort by Number\n3. Main Menu\n\nEnter a following option: ");
    scanf("%d", &option);

    system("cls");

    switch(option)
    {
    case 1:
        SortByName();
        break;

    case 2:
        SortByNumber();
        break;

    case 3:
        mainMenu();
        break;

    default:
        printf("Invalid Input for Main Menu!\n\n");
        mainMenu();
        break;
    }
}

int changeUsername()
{
    readFile();

    printf("Change Username");
    printf("\n------------\n\n");

    fileOpenRead();
    fp = fopen("ContactList.txt", "wb");

    printf("New Username: ");
    scanf("%s", list[1].username);

    printf("Password: ");
    scanf("%s", list[1].password);

    if(strcmp(list[1].password, list[0].password) == 0)
    {
        system("cls");

        strcpy(list[0].username, list[1].username);

        printf("Username changed successfully!");
        printf("\n\n");

        fileOpenWrite();
        fclose(fp);
        readFile();
        optionMenu();
    }
    else
    {
        fclose(fp);
        system("cls");

        printf("Wrong password!\n\n");
        forgetUserPass();
    }
}

int changePassword()
{
    readFile();

    printf("Change Password");
    printf("\n---------------\n\n");

    fileOpenRead();

    printf("Password: ");
    scanf("%s", list[1].password);

    if(strcmp(list[1].password, list[0].password) == 0)
    {

        printf("New Password: ");
        scanf("%s", list[2].password);

confirmNewPass:

        fp = fopen("ContactList.txt", "wb");

        printf("Confirm New Password: ");
        scanf("%s", list[3].password);

        if(strcmp(list[2].password, list[3].password) == 0)
        {
            strcpy(list[0].password, list[3].password);

            fileOpenWrite();
            fclose(fp);
            system("cls");

            printf("Password changed successfully!");
            printf("\n\n");

            readFile();
            optionMenu();
        }
        else
        {
            system("cls");

            printf("New Password and Confirm New Password are not same!");
            printf("\n\n");

            //printf("Password: %s\nNew Password: %s\n", list[1].password, list[2].password);
            printf("New Password: %s\n", list[2].password);
            goto confirmNewPass;
        }
    }
    else
    {
        fclose(fp);
        readFile();
        system("cls");

        printf("Wrong password!\n\n");
        forgetUserPass();
    }
}

int securitySetting()
{
    printf("Security Setting");
    printf("\n-------------------\n\n");

    printf("\n\n1. Change Username\n2. Change Password\n3. Main Menu\n\nEnter a following option: ");
    scanf("%d", &option);

    system("cls");

    switch(option)
    {
    case 1:
        changeUsername();
        break;

    case 2:
        changePassword();
        break;

    case 3:
        mainMenu();
        break;

    default:
        printf("Invalid Input for Main Menu!\n\n");
        mainMenu();
        break;
    }

    readFile();


}

int LoggedOutPage()
{
    printf("Logged Out!\n\n");
loginOption:
    printf("1. Login\n2. Exit\n\nEnter a following option: ");
    scanf("%d", &option);

    system("cls");

    switch(option)
    {
    case 1:
        main();
        break;

    case 2:
        printf("Exiting...\n\n");
        exit(0);
        break;

    default:
        printf("Invalid Input!\n\n");
        goto loginOption;
        break;
    }
}

int forgetUserPass()
{
    printf("Forget Username or Passwowrd");
    printf("\n---------------------------\n\n");

    printf("\n\n1. Show Username\n2. Show Password\n3. Main Menu\n\n\nEnter a following option: ");
    scanf("%d", &option);

    system("cls");

    switch(option)
    {
    case 1:
        printf("Username: %s", list[0].username);
        optionMenu();
        break;

    case 2:
        printf("Password: %s", list[0].password);
        optionMenu();
        break;

    case 3:
        mainMenu();
        break;

    default:
        system("cls");

        printf("Invalid Input!\n\n");
        mainMenu();
        break;
    }
}

int mainMenu()
{
    printf("Contact Managmenet System");
    printf("\n-------------------------\n\n\n");
    printf("1. See Contact List\n\n");
    printf("2. Add Contact\n\n");
    printf("3. Delete Contact\n\n");
    printf("4. Search Contact\n\n");
    printf("5. Sort Contact\n\n");
    printf("6. Security Setting\n\n");
    printf("7. About Author\n\n");
    printf("8. Log Out\n\n");

    printf("Enter a following option: ");
    scanf("%d", &option);
    printf("\n");

    system("cls");
    switch(option)
    {
    case 1:
        contactList();
        break;

    case 2:
        addContact();
        break;

    case 3:
        deleteContact();
        break;

    case 4:
        searchContact();
        break;

    case 5:
        SortContact();
        break;

    case 6:
        securitySetting();
        break;

    case 7:
        aboutAuthor();
        break;

    case 8:
        LoggedOutPage();
        break;

    default:
        printf("Invalid Input! Please Enter 1 to 6:\n\n");
        break;
    }
}


int main()
{
    SetConsoleTitle("Contact Managment System");

    //Default Password and Username.
    //strcpy(list[0].username, "0");
    //strcpy(list[0].password, "0");

    readFile();
    fileOpenRead();

    if(strcmp(list[0].username, "") == 0 && strcmp(list[0].password, "") == 0)
    {
        fp = fopen("ContactList.txt", "wb");

        printf("\t\t\t\tSet Username and Password\n");
        printf("\t\t\t\t------------------------\n");

        printf("\t\t\t\tEnter username: ");
        scanf("%s", list[0].username);

        printf("\n\t\t\t\tEnter password: ");
        scanf("%s", list[0].password);
        printf("\n");

        fileOpenWrite();
        readFile();
        fclose(fp);

        system("cls");
    }
    fclose(fp);
login:
    readFile();
    fileOpenRead();

    printf("\t\t\t\tLogin to manage contacts\n");
    printf("\t\t\t\t------------------------\n");
    printf("\t\t\t\tEnter username: ");
    scanf("%s", list[1].username);

    printf("\n");
    printf("\t\t\t\tEnter password: ");
    scanf("%s", list[1].password);
    printf("\n");

    if(strcmp(list[1].username, list[0].username) == 0 && strcmp(list[1].password, list[0].password) == 0)
    {
        fclose(fp);

        system("cls");
        printf("\t\t\t\tLogin successful!\n\n");
        printf("\n\n");
        readFile();

        while(1) //1 for infinite loop.
        {
            mainMenu();
        }
    }
    else
    {
invalid:

        system("cls");

        printf("Wrong useraname or password!\n\n");

        printf("1. Forget Useraname or Password\n2. Login\n\nEnter a following option: ");
        scanf("%d", &option);

        system("cls");

        switch(option)
        {
        case 1:
            printf("After few second it's automatically go Login page.\n\n");
            printf("Username: %s\nPassword: %s\n\nPlease wait...", list[0].username, list[0].password);

            Sleep(5000);
            system("cls");

            goto login;
            break;

        case 2:
            goto login;
            break;

        default:
            printf("Invalid Input!\n\n");
            goto invalid;
            break;
        }
    }

}

int fileOpenRead()
{
    fp = fopen("ContactList.txt", "rb");

    fread(&list, sizeof(list), 1, fp);
    fread(&size, sizeof(size), 1, fp);
    fread(&i, sizeof(i), 1, fp);
}

int fileOpenWrite()
{
    fwrite(&list, sizeof(list), 1, fp);
    fwrite(&size, sizeof(size), 1, fp);
    fwrite(&i, sizeof(i), 1, fp);
    fclose(fp);
}

int readFile()
{
    fileOpenRead();

    for(j = 0; j < size; j++)
    {
        list[j].name;
        list[j].phone;
        list[j].email;
    }
    fclose(fp);
}

Write a C/JAVA program to implement CRC

Implement CRC

The program will take a sequence of binary data bits, and a divider as input. And gives the resultant data with CRC as output. (Where, divider < binary data bits)

Sample Input:
Data: 10110
Divider: 1101

Sample Output:
Modified data: 10110000
Data Send: 10110101
CRC bit: 101

Sample Output
Enter data: 1001
Enter divisor: 1011

Input Data With Zero(s): 1001000

CRC Bit: 110

Modified Data: 1001110
Source Code
#include<stdio.h>
#include<stdlib.h>

void main()
{
    int i, j, k, l, numOfZero, dataLen;
    char divisor[100], data[100], remainder[100], divisorClone[100], dataClone[100], crc[100];

    printf("Enter data: ");
    scanf("%s", data);
    printf("Enter divisor: ");
    scanf("%s", divisor);

    for(i = 0; data[i] != '\0'; i++);
    dataLen = i;
    for(i = 0; divisor[i] != '\0'; i++);
    numOfZero = i;

    for(i = 0; i < numOfZero-1; i++)
    {
        data[dataLen+i] = '0';
    }
    printf("\nInput Data With Zero(s): %s\n", data);
    strcpy(dataClone, data);

    for(i = 0; i < dataLen; i++)
    {
        strcpy(divisorClone, divisor);
        if(data[i] < divisor[0])
        {
            for(l = 0; l < numOfZero; l++)
            {
                divisor[l] = '0';
            }
            divisor[l] = '\0';
        }
        for(j = i, k = 0; j < numOfZero+i; j++, k++)
        {
            if(data[j] == divisor[k])
            {
                remainder[j] = '0';
            }
            else
            {
                remainder[j] = '1';
            }
        }
        strcpy(divisor, divisorClone);
        remainder[j] = dataClone[j];
        remainder[j+1] = '\0';
        strcpy(data, remainder);
    }
    strcpy(data, remainder);

    for(i = dataLen, j = 0; i < dataLen+numOfZero; i++)
    {
        dataClone[i] = data[i];
        crc[j++] = data[i];
    }
    crc[j-1] = '\0';
    dataClone[i-1] = '\0';

    printf("\nCRC Bit: %s\n\nModified Data: %s\n\n", crc, dataClone);
}
Sample Output
Enter data: 10111011
Enter divisor: 1001

Input Data With Zero(s): 10111011000

CRC Bit: 110

Modified Data: 10111011110

Write a program to find Hamming Distance

Hamming Distance

Hamming distance is a metric for comparing two binary data strings. While comparing two binary strings of equal length, Hamming distance is the number of bit positions in which the two bits are different.

Example: Find the distance between the vectors 01101010 and 11011011.
(01101010
11011011)
They differ in four places, So the Hamming distance d(01101010, 11011011) = 4.

Sample Output
Enter Comp1 data: 111101010
Enter Comp2 data: 010101010

Hamming Distance: 2
C-Source Code
#include<stdio.h>

void main()
{
    int i, comp1Len, comp2Len, hammingDistance = 0;
    char comp1[500], comp2[500];
input:
    printf("Enter Comp1 data: ");
    scanf("%s", comp1);
    printf("Enter Comp2 data: ");
    scanf("%s", comp2);

    for(i = 0; comp1[i] != '\0'; i++);
    comp1Len = i;

    for(i = 0; comp2[i] != '\0'; i++);
    comp2Len = i;

    if(comp1Len != comp2Len)
    {
        printf("\nComp1 and Comp2 length are not same!\n\n");
        goto input;
    }

    for(i = 0; i < comp1Len; i++)
    {
        if(comp1[i] != comp2[i])
        {
            hammingDistance++;
        }
    }
    printf("\nHamming Distance: %d\n\n", hammingDistance);
}
Sample Output
Enter Comp1 data: 01101010
Enter Comp2 data: 11011011

Hamming Distance: 4

Write a program to calculate the MINIMUM HAMMING DISTANCE for a given set of binary data taken as input

Minimum Hamming Distance
Sample Input:
    Number of inputs: 3
    10110
    01110
    11001
    Sample Output:
    Minimum hamming distance, (C1, C2) = 2
Sample Output
Number of inputs: 3
10110
01110
11001

Minimum hamming distance, (10110, 01110) = 2
C-Source Code
/**
I will share the code after 15 April 2021,
If you need urgent, please contact with me.
*/
Sample Output
Number of inputs: 4
1110
0011
0000
10

Data must be same length!
Enter 4 data, whose length 4 bits...
1110
0011
0000
1011

Minimum hamming distance, (0011, 1011) = 1

Write a program that choose Even parity / Odd parity according to user choose. And accordingly performs Even parity / Odd parity process

Parity Bit Checker

Example 1:
Enter 1 for Even parity and 2 for Odd parity.
Input: 1

Even parity:
Input:
Data: 110110
Output:
modified data: 1101100
Parity bit: 0

Example 2:
Enter 1 for Even parity and 2 for Odd parity.
Input: 2

Odd parity:
Input:
Data: 1101101
Output:
Modified data: 11011010
Parity bit: 0

Sample Output
Enter 1 for Even parity and 2 for Odd parity.

Enter your choice: 1

Even parity:
Data: 110110

Modified Data: 1101100
Parity Bit: 0
C-Source Code
#include<stdio.h>
#include<string.h>

void main()
{
    int i, choice, even_parity_len, count = 0, p_bit;
    char parity_bit, even_parity[100];
main:
    printf("Enter 1 for Even parity and 2 for Odd parity.\n\nEnter your choice: ");
    scanf("%d", &choice);

    switch(choice)
    {
    case 1:
        printf("\nEven parity:\nData: ");
        scanf("%s", even_parity);

        for(i = 0; even_parity[i] != '\0'; i++);
        even_parity_len = i;

        for(i = 0; i < even_parity[i]; i++)
        {
            if(even_parity[i] == '1')
            {
                count++;
            }
        }

        if(count%2 == 0)
        {
            even_parity[even_parity_len] = '0';
            p_bit = 0;
        }
        else
        {
            even_parity[even_parity_len] = '1';
            p_bit = 1;
        }
        break;

    case 2:
        printf("\nOdd parity:\nData: ");
        scanf("%s", even_parity);

        for(i = 0; even_parity[i] != '\0'; i++);
        even_parity_len = i;

        for(i = 0; i < even_parity[i]; i++)
        {
            if(even_parity[i] == '1')
            {
                count++;
            }
        }

        if(count%2 == 0)
        {
            even_parity[even_parity_len] = '1';
            p_bit = 1;
        }
        else
        {
            even_parity[even_parity_len] = '0';
            p_bit = 0;
        }
        break;

    default:
        printf("\nInvalid Input! Please enter 1 or 2.\n");
        goto main;
        break;
    }
    even_parity[even_parity_len+1] = '\0';

    printf("\nModified Data: %s\nParity Bit: %d\n\n", even_parity, p_bit);
}
Sample Output
Enter 1 for Even parity and 2 for Odd parity.

Enter your choice: 2

Odd parity:
Data: 1101101

Modified Data: 11011010
Parity Bit: 0

Write a program that construct an Even parity Hamming code

Even parity Hamming code

Example-1:
Input: 10111010100101
Output: 
Mod data: 1111011110101000101
Number of parity bits: 5

Example-2:
Input: 101100
Output: 
Mod data: 0110011000
Number of parity bits: 4

Table Even Parity Hamming Code

Before add parity bit, data will generate like this: 0X1X011X1010100X101

P1 = [3, 5, 7, 9, 11, 13, 15, 17, 19] = 101111011
= X1 = 1 {Odd parity}
P2 = [3, 6, 7, 10, 11, 14, 15, 18, 19] = 111010001
= X2 = 1 {Odd parity}
P4 = [5, 6, 7, 12, 13, 14, 15] = 0110100
= X4 = 1 {Odd parity}
P8 = [9, 10, 11, 12, 13, 14, 15] = 1010100
= X8 = 1 {Odd parity}
P16 = [17, 18, 19] = 101
= X16 = 0 {Even parity}

So, Modified data = 1111011110101000101

Sample Output
Enter data: 101100

Mod data: 0110011000
Number of parity bits: 4
Source Code
#include<stdio.h>
#include<math.h>

void main()
{
    int i, j, k, l, m, n, x, p_bit[50], p_bit_count = -1;
    int count, dataLen, value = 0, temp_even_parity_len;
    char data[500], temp_data[500], modData[500], temp_even_parity[100];

    printf("Enter data: ");
    scanf("%s", data);

    for(i = 0; data[i] != '\0'; i++);
    dataLen = i;

    for(i = 0; value <= (dataLen + i + 1); i++)
    {
        value = pow(2, i);
        p_bit[i] = value;
        p_bit_count++;
    }

    i = 0;
    j = 0;
    k = 0;
    while(i < (dataLen + p_bit_count))
    {
        if(p_bit[k] == i+1)
        {
            modData[i] = 'X';
            k++;
        }
        else
        {
            modData[i] = data[j];
            j++;
        }
        i++;
    }
    modData[i] = '\0';

    for(k = 0; k < p_bit_count; k++)
    {
        l = 0;
        for(m = p_bit[k]; m <= dataLen + p_bit_count;)
        {
            for(n = 0; n < p_bit[k] && m <= dataLen + p_bit_count; n++)
            {
                if(m == p_bit[k])
                {
                    m++;
                }
                else
                {
                    temp_even_parity[l++] = modData[m-1];
                    m++;
                }
            }
            for(n = 0; n < p_bit[k] && m <= dataLen + p_bit_count; n++)
            {
                m++;
            }
        }
        temp_even_parity[l] ='\0';

        for(i = 0; temp_even_parity[i] !='\0'; i++);
        temp_even_parity_len = i;

        for(i = 0, count = 0; i < temp_even_parity_len; i++)
        {
            if(temp_even_parity[i] == '1')
            {
                count++;
            }
        }

        if(count % 2 == 0)
        {
            x = (p_bit[k]-1);
            modData[x] = '0';
        }
        else
        {
            x = (p_bit[k]-1);
            modData[x] = '1';
        }
    }
    printf("\nMod data: %s\n", modData);
    printf("Number of parity bits: %d\n\n", p_bit_count);
}
Sample Output
Enter data: 10111010100101

Mod data: 1111011110101000101
Number of parity bits: 5

Write a C program to check whether a number is Armstrong number or not

Check whether a number is Armstrong number or not
Sample Output
Check whether a number is Armstrong number or not.

Enter a number: 370

370 is an Armstrong number!
Source Code
#include<stdio.h>
#include<math.h>

int main()
{
    int i, j, digit, number, numberClone, count = 0, newNumber = 0, temp;

    printf("Check whether a number is Armstrong number or not.\n\n");
    printf("Enter a number: %d", number);
    scanf("%d", &number);

    numberClone = number;

    while(number != 0)
    {
        digit = (number % 10);
        //newNumber = newNumber;
        number /= 10; /// Or, number = number / 10;
        count++;
    }

    number = numberClone;
    while(number != 0)
    {
        digit = (number % 10);
        temp = pow(digit, count);
        newNumber += temp;
        number /= 10; /// Or, number = number / 10;
    }

    number = numberClone;
    if(newNumber == number)
    {
        printf("\n%d is an Armstrong number!\n\n", number);
    }
    else
    {
        printf("\n%d is not an Armstrong number.\n\n", number);
    }
}
Sample Output
Check whether a number is Armstrong number or not.

Enter a number: 372

372 is not an Armstrong number.
Change Theme
X