Welcome...

Monday, 16 September 2013

C program to print the prime numbers in the range 2 to given number

Following is a C program which prints the prime numbers in the range 2 to the given number by the user:

/*C program to print prime numbers from 2 to given number*/
main()
{
    int number,i,j,flag;
    clrscr();
    printf("Enter a number:");
    scanf("%d",&number);
    if(number<2)
    {
        printf("Invalid number!");
    }
    else
    {
        printf("Prime numbers in the range 2 to %d are:\n",number);
        for(i=2;i<=number;i++)
        {
            flag = 0;
            for(j=2;j<=i-1;j++)
            {
                if(i%j==0)
                {
                    flag = 1;
                }
            }
            if(flag==0)
            {
                printf("%d ",i);
            }
        }
    }
    getch();
}

Sample input and output:
Enter a number:100
Prime numbers in the range 2 to 100 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

C program to find a given number is prime or not using a flag variable

Following program reads a number from the user and prints whether it is prime or not by using a flag variable. A flag variable is a binary variable with values 0 and 1:

/*Program to find the given number is prime number or not using a flag variable*/
main()
{
    int number,i,flag=0;
    clrscr();
    printf("Enter the number:");
    scanf("%d",&number);
    for(i=2;i<=number-1;i++)
    {
        if(number%i==0)
        {
            flag = 1;
        }
    }
    if(flag==1)
    {
        printf("\n%d is not a prime number",number);
    }
    else if(number<=1)
    {
        printf("\n%d is invalid number!",number);
    }
    else
    {
        printf("\n%d is a prime number",number);
    }
    getch();
}
Sample input and output:
Enter the number:17

17 is a prime number

C program to find whether a given number is prime or not

Following program reads a number from the user and prints whether it is prime or not along with the factors of the given number:

/*Program to find the given number is prime number or not*/
main()
{
    int number,i,count=0;
    clrscr();
    printf("Enter the number:");
    scanf("%d",&number);
    printf("\nFactors of %d are:",number);
    for(i=1;i<=number;i++)
    {
        if(number%i==0)
        {
            printf("\n%d",i);
            count++;
        }
    }
    if(count>2)
    {
        printf("\n\n%d is not a prime number",number);
    }
    else if(number<=1)
    {
        printf("\n\n%d is invalid!",number);
    }
    else
    {
        printf("\n\n%d is a prime number",number);
    }
    getch();
}
Sample input and output:
Enter the number:13

Factors of 13 are:
1
13

13 is a prime number

Tuesday, 10 September 2013

C program to find the two's complement of a number

Below is a C program which takes as input a decimal number and prints the two's complement of that number. The given decimal number is first converted to binary format. Then we find the 1's complement and then add one to the one's complement to get the required 2's complement of the given decimal number.

/*Program to find the two's complement of given number*/
main()
{
    int num,tnum,tbin[16],bin[16],one[16],fin[16],i,j,k,quot,rem,sum=0,carry=0;
    clrscr();

    /*Reading the decimal number from user*/
    printf("Enter a decimal number:");
    scanf("%d", &num);

    /*Converting decimal number to binary*/
    tnum = num;
    i = 0;
    while(num > 0)
    {
        tbin[i] = num % 2;
        num = num / 2;
        i++;
    }

    for(k=0,j=i-1;j>=0;j--,k++)
    bin[k] = tbin[j];

    /*Printing the binary number*/
    printf("Binary representation of %d is: ",tnum);
    for(k=0;k<i;k++)
    printf("%d",bin[k]);

    /*One's complement of the number*/
    for(k=0;k<i;k++)
    {
        if(bin[k]==0)
        bin[k] = 1;
        else
        bin[k] = 0;
    }

    /*Printing one's complement*/
    printf("\nOne's complement of %d is: ",tnum);
    for(k=0;k<i;k++)
    printf("%d",bin[k]);

    /*Creating binary number 1 for adding to the one's complement*/
    for(k=0;k<i-1;k++)
    {
        one[k] = 0;
    }
    one[i-1] = 1;

    /*Two's complement of the number*/
    for(k=i-1;k>=0;k--)
    {
        /*printf("\nbin[%d] = %d, one[%d] = %d",k,bin[k],k,one[k]);*/
        if(bin[k]==1 && one[k]==1)
        {
            sum = 0;
            carry = 1;
        }
        else if(bin[k]==0 && one[k]==0)
        {
            if(carry==1)
            {
                sum = 1;
                carry = 0;
            }
            else
            {
                sum = 0;
                carry = 0;
            }
        }
        else
        {
            if(carry==1)
            {
                sum = 0;
                carry = 1;
            }
            else
            {
                sum = 1;
                carry = 0;
            }
        }
        fin[k] = sum;
        /*printf("\nsum = %d, carry = %d",sum,carry);*/
    }

    /*Printing 2's complement*/
    printf("\n2's complement of %d is: ",tnum);
    for(k=0;k<i;k++)
    printf("%d",fin[k]);

    getch();
}


Sample input and output:
Enter a decimal number:36
Binary representation of 36 is: 100100
One's complement of 36 is: 011011
2's complement of 36 is: 011100

C program to convert a number to its one's complement representation

Below is a C program which converts the given decimal number to binary format and then prints the one's complement of the given number. First the number is converted to binary and then each bit is inverted to get the 1's complement.

/*Program to find the one's complement of given number*/
main()
{
    int num,tnum,tbin[16],bin[16],i,j,k,quot,rem;
    clrscr();

    /*Reading the decimal number from user*/
    printf("Enter a decimal number:");
    scanf("%d", &num);

    /*Converting decimal number to binary*/
    tnum = num;
    i = 0;
    while(num > 0)
    {
        tbin[i] = num % 2;
        num = num / 2;
        i++;
    }

    for(k=0,j=i-1;j>=0;j--,k++)
    bin[k] = tbin[j];

    /*Printing the binary number*/
    printf("Binary representation of %d is: ",tnum);
    for(k=0;k<i;k++)
    printf("%d",bin[k]);

    /*One's complement of the number*/
    for(k=0;k<i;k++)
    {
        if(bin[k]==0)
        bin[k] = 1;
        else
        bin[k] = 0;
    }

    /*Printing one's complement*/
    printf("\nOne's complement of %d is: ",tnum);
    for(k=0;k<i;k++)
    printf("%d",bin[k]);
    getch();
}


Sample input and output:
Enter a decimal number:36
Binary representation of 36 is: 100100
One's complement of 36 is: 011011

C program to convert a number to binary format

Below is a small C program to convert a given decimal number to its binary representation. The code is very easy to understand and is commented out for better readability.

/*Program to convert the given number into binary format*/
main()
{
    int num,tnum,bin[16],i,j,quot,rem;
    clrscr();

    /*Reading the decimal number from user*/
    printf("Enter a decimal number:");
    scanf("%d", &num);

    /*Converting decimal number to binary*/
    tnum = num;
    i = 0;
    while(num > 0)
    {
        bin[i] = num % 2;
        num = num / 2;
        i++;
    }

    /*Printing the binary number*/
    printf("Binary representation of %d is: ",tnum);
    for(j=i-1;j>=0;j--)
    printf("%d",bin[j]);
    getch();
}

Sample input and output:
Enter a decimal number:36
Binary representation of 36 is: 100100