Saturday, December 13, 2014

Print their Binary Sum in c pgm



Input 2 Binary Strings and Print their Binary Sum

#include 
#include 
#include 
#include 
 
int bin_verify(char []);
void sum(char [], char [], char []);
 
int main()
{
    char bin1[33], bin2[33], result[33];
    int len1, len2, check;
    clrscr();
    printf("Enter binary number 1: ");
    scanf("%s", bin1);
    printf("Enter binary number 2: ");
    scanf("%s", bin2);
    check = bin_verify(bin1);
    if (check)
    {
        printf("Invalid binary number %s.\n", bin1);
        exit(0);
    }
    check = bin_verify(bin2);
    if (check)
    {
        printf("Invalid binary number %s.\n", bin2);
        exit(0);
    }
    sum(bin1, bin2, result);
    printf("%s + %s = %s\n", bin1, bin2, result);
 
    return 0;
}
 
int bin_verify(char str[])
{
    int i;
 
    for (i = 0; i < strlen(str); i++)
    {
        if ((str[i] - '0' != 1 ) && (str[i] - '0' != 0))
        {
            return 1;
        }
    }
 
    return 0;
}
 
void sum(char bin1[], char bin2[], char result[])
{
    int i = strlen(bin1) - 1;
    int j = strlen(bin2) - 1;
    int carry = 0, temp, num1, num2;
 
    while (i > -1 && j > -1)
    {
        num1 = bin1[i] - '0';
        num2 = bin2[j] - '0';
        temp = num1 + num2 + carry;
        if (temp / 2 == 1)
        {
            carry = 1;
            temp %= 2;
        }
        if (i > j)
        {
            result[i + 1] = temp + '0';
            result[strlen(bin1) + 1] = '\0';
        }
        else
        {
            result[j +1] = temp + '0';
            result[strlen(bin2) + 1] = '\0';
        }
        i--;
        j--;
    }
    while (i > -1)
    {
        temp = bin1[i] + carry - '0';
        if (temp / 2 == 1)
        {
            carry = 1;
            temp %= 2;
        }
        result[i + 1] = temp + '0';
        i--;
    }
    while (j > -1)
    {
        temp = bin2[j] + carry - '0';
        if (temp / 2 == 1)
        {
            carry = 1;
            temp %= 2;
        }
        result[j + 1] = temp + '0';
        j--;
    }
    if (carry)
    {
        result[0] = '1';
    }
    else
    {
        result[0] = '0';
    }
 getch();
}
 
Output
 
Enter binary number 1: 0110 
Enter binary number 2: 1011
0110 + 1011 = 10001

Input a String with atleast one Number, Print the Square of all the Numbers in a String

#include 
#include 
#include 
#include 
#include 
 
struct detail
{
    int number;
    int square;
};
 
int update(struct detail [], int, int);
int toint(char []);
 
int main()
{
    struct detail s[10];
    char unit[20], string[100];
    char c;
    int num, i, j = 0, count = 0;
 
    printf("Enter string: ");
    i = 0;
    do
    {
        fflush(stdin);
        c = getchar();
        string[i++] = c;
 
    } while (c != '\n');
    string[i - 1] = '\0';
    printf("The string entered is: %s\n", string);
    for (i = 0; i < strlen(string); i++)
    {
        while (i < strlen(string) && !isspace(string[i]))
        {
            unit[j++] = string[i++];
        }
        if (j != 0)
        {
            unit[j] = '\0';
            num = toint(unit);
            count = update(s, num, count);
            j = 0;
        }
    }
    printf("*****************\nNumber\tSquare\n*****************\n");
    for (i = 0; i < count; i++)
    {
        printf("%d\t   %d\n", s[i].number, s[i].square);
    }
 
    return 0;
}
 
int update(struct detail s[], int num, int count)
{
    s[count].number = num;
    s[count].square = num * num;
 
    return (count + 1);
}
 
int toint(char str[])
{
    int len = strlen(str);
    int i, num = 0;
 
    for (i = 0; i < len; i++)
    {
        num = num + ((str[len - (i + 1)] - '0') * pow(10, i));
    }
 
   return num;
getch();
}

Output
 
Enter string: 1 2 3 4 5
The string entered is: 1 2 3 4 5
*****************
Number    Square
*****************
1      1
2      4
3      9
4      16
5      25

No comments:

Post a Comment