Saturday, June 18, 2016

C Program Code: Draw A Right Angled Triangle

C Program Code:

#include<stdio.h>
#include<conio.h>
int main()
{
    int size;
    int i,j,space;

    printf("Draw A Right Angled Triangle in C \n");
    printf("Enter Size : ");
    scanf("%d",&size);

    for(i=1; i<=size; i++)
    {
        for(space=1; space<=size-i; space++)
        {
            printf(" ");
        }
        for(j=1; j<=i; j++)
        {
            printf("*");
        }
        printf("\n");
    }
getch();
return 0;
}

Draw A Right Angled Triangle in C Output Screenshot

C Program Code: Print Reverse Triangle

C Program Code:

#include<stdio.h>
#include<conio.h>
int main()
{
    int size,space;
    int i,j;
    printf("Print Reverse Triangle in C \n");
    printf("Enter Size : ");
    scanf("%d",&size);
    for(i=size; i>=1; i--)
    {
        for(space=1; space<=size-i; space++)
        {
            printf(" ");
        }
        for(j=1; j<=i; j++)
        {
            printf("*");
        }
        printf("\n");
    }
getch();
return 0;
}

Print Reverse Triangle in C Output Screenshot

C Program Code: Equilateral Triangle

C Program Code:


#include<stdio.h>
#include<conio.h>
int main()
{
    int size;
    int i,j;

    printf("Equilateral Triangle in C \n");
    printf("Enter Size : ");
    scanf("%d",&size);

    for(i=0; i<size; i++)
    {
        for(j = i ; j < size; j++)
        {
            printf(" * ");
        }
        printf("\n");
    }
getch();
return 0;
}

Equilateral Triangle in C Output Screenshot

C Program Code: Print Multiplication Table

C Program Code:

#include<stdio.h>
#include<conio.h>
int main()
{
    int number;
    int i;
    printf(" Print Multiplication Table in C \n");
    printf(" Enter Number for Table : ");
    scanf("%d",&number);
    for(i = 1 ;i<=10 ; i++)
    {
        printf(" %d * %d = %d \n",number,i,number*i);
    }
getch();
return 0;
}

Print Multiplication Table in C Output Screenshot



C Program Code: Print Pascal Triangle

C Program Code:

#include <stdio.h>
#include <conio.h>
int main()
{
    int size,space;
    int i,j;
    printf("Print Pascal Triangle in C \n");
    printf("Enter Size : ");
    scanf("%d",&size);
    for(i=1; i<=size; i++)
    {
        for(space=1; space<=size-i; space++)
        {
            printf(" ");
        }
        for(j=1; j<=2*i-1; j++)
        {
            printf("*");
        }
        printf("\n");
    }
getch();
return 0;
}

Print Pascal Triangle in C Output Screenshot

C Program Code: Find LCM of Two Numbers

C Program Code:

#include<stdio.h>
int main()
{
    int firstNumber;
    int secondNumber;
    int lcm;
    int i,n;
    printf("Find LCM of Two Numbers in C \n");
    printf("Enter First Number :  ");
    scanf("%d",&firstNumber);
    printf("Enter Second Number : ");
    scanf("%d",&secondNumber);
    lcm=0;
    n=firstNumber*secondNumber;
    for(i=1; i<=n && lcm ==0; i++)
    {
        if(i%firstNumber==0&&i%secondNumber==0)
        {
            lcm=i;
        }
    }
    printf("LCM is %d \n",lcm);
    printf("Press enter to continue...\n");
    fflush(stdin);
    getchar();
    return 0;
}

Find LCM of Two Numbers C Program Output Screenshot

C Program Code: Convert Hexadecimal to Decimal

C Program Code:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
unsigned long convertToDecimal(char hex[]);
int main()
{
    char hex[9];// 8 characters for 32-bit Hexadecimal Number and one for '\0'.
    unsigned long decimalNumber;
    printf(" C Program to Convert Hexadecimal Number to Decimal Number \n");
    printf(" Enter 32-bit Hexadecimal Number : ");
    scanf("%s", hex);
    decimalNumber = convertToDecimal(hex);
    printf(" Decimal Number is %u  \n", decimalNumber);
    printf(" Press enter to continue... \n");
    fflush(stdin);
    getchar();
    return 0;
}
unsigned long convertToDecimal(char hex[])
{
    char *hexString;
    int length = 0;
    const int base = 16; // Base of Hexadecimal Number
    unsigned long decimalNumber = 0;
    int i;
    // Find length of Hexadecimal Number
    for (hexString = hex; *hexString != '\0'; hexString++)
    {
        length++;
    }
    // Find Hexadecimal Number
    hexString = hex;
    for (i = 0; *hexString != '\0' && i < length; i++, hexString++)
    {
        // Compare *hexString with ASCII values
        if (*hexString >= 48 && *hexString <= 57)   // is *hexString Between 0-9
        {
            decimalNumber += (((int)(*hexString)) - 48) * pow(base, length - i - 1);
        }
        else if ((*hexString >= 65 && *hexString <= 70))   // is *hexString Between A-F
        {
            decimalNumber += (((int)(*hexString)) - 55) * pow(base, length - i - 1);
        }
        else if (*hexString >= 97 && *hexString <= 102)   // is *hexString Between a-f
        {
            decimalNumber += (((int)(*hexString)) - 87) * pow(base, length - i - 1);
        }
        else
        {
            printf(" Invalid Hexadecimal Number \n");
            printf(" Press enter to continue... \n");
            fflush(stdin);
            getchar();
            return 0;
            exit(0);
        }
    }
    return decimalNumber;
}

Convert Hexadecimal to Decimal C Output Screenshot


C Program Code:Find Square Root of a Number

C Program Code:

#include<stdio.h>
#include<math.h>
int main()
{
    int number;
    double squareRoot;
    printf(" Find Square Root of a Number in C \n");
    printf(" Enter a Number: ");
    scanf("%d", &number);
    if (number > 0)
    {
        squareRoot = sqrt(number);
        printf(" Square Root of Number %d is %.2f \n", number, squareRoot);
    }
    else
    {
        printf(" Invalid Input, Please Enter Positive Number \n");
    }
    printf(" Press enter to continue...\n");
    fflush(stdin);
    getchar();
    return 0;
}
Find Square Root of a Number C Output Screenshot

C Program Code:Find Number is Even or Odd

C Program Code:

#include<stdio.h>

int main()
{
    int number;

    printf(" Find Number is Even or Odd in C \n");
    printf(" Enter a Number : ");
    scanf("%d", &number);

    if (number % 2 == 0)
    {
        printf(" %d is Even \n", number);
    }
    else
    {
        printf(" %d  is Odd \n", number);
    }

    printf(" Press enter to continue...\n");
    fflush(stdin);
    getchar();
    return 0;
}

Find Number is Even or Odd C Output Screenshot

C Program Code:Find Largest of Three Numbers

C Program Code:

#include<stdio.h>
int main()
{
    int firstNumber, secondNumber, thirdNumber, largest;
    printf("Find Largest of Three Numbers in C \n");
    printf("Enter First Number  : ");
    scanf("%d", &firstNumber);
    printf("Enter Second Number : ");
    scanf("%d", &secondNumber);
    printf("Enter Third Number  : ");
    scanf("%d", &thirdNumber);
    largest = firstNumber;
    if (secondNumber > largest)
    {
        largest = secondNumber;
    }
    if (thirdNumber > largest)
    {
        largest = thirdNumber;
    }
    printf("Largest Number is %d \n", largest);
    printf("Press enter to continue...\n");
    fflush(stdin);
    getchar();
    return 0;
}

Find Largest of Three Numbers C using if else Output Screenshot

C Program Code: Convert Decimal to Hexadecimal

C Program Code:

#include<stdio.h>
void decimalToHex(int number , int base);
int main()
{
    int decimalNumber;
    int base;
    base = 16; // Base of Hexadecimal
    printf("Decimal to Hexadecimal Number Converter in C \n");
    printf("Enter number in decimal: ");
    scanf("%d",&decimalNumber);
    printf("Decimal %d = ",decimalNumber);
    decimalToHex(decimalNumber, base);
    printf(" Hex \n");
    printf("Press enter to continue... \n");
    fflush(stdin);
    getchar();
    return 0;
}
void decimalToHex(int number , int base)
{
    if( number > 0 )
    {
        decimalToHex( number/base , base );
        switch(number%base)
        {
        case 10 :
            printf("A");
            break;
        case 11 :
            printf("B");
            break;
        case 12 :
            printf("C");
            break;
        case 13 :
            printf("D");
            break;
        case 14 :
            printf("E");
            break;
        case 15 :
            printf("F");
            break;
        default :
            printf("%d",number%base);
            break;
        }
    }
}

Convert Decimal to Hexadecimal in C Output Screenshot

C Program Code: Convert Binary to Decimal

C Program Code:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int convertToDecimal(const char binary[]);
int main()
{
    char binaryNumber[17]; // 16 characters for 16-bit Binary  Number and one for ''.
    int decimalNumber;
    printf("Binary to Decimal Converter in C \n");
    printf("Enter 16-bit Binary  Number : ");
    scanf("%s", binaryNumber);
    decimalNumber = convertToDecimal(binaryNumber);
    printf("Decimal Number is %d \n", decimalNumber);
    printf("Press enter to continue... \n");
    fflush(stdin);
    getchar();
    return 0;
}
int convertToDecimal(const char binaryNumber[])
{
    const char *binaryString = binaryNumber;
    int length = 0;
    int decimalNumber = 0;
    int base = 2;        // Base Of Binary Number
    int i;
    // Find length of Binary Number
    for (; *binaryString != '\0'; binaryString++)
    {
        length++;
    }
    // Find Decimal Number
    binaryString = binaryNumber;
    for (i = 0; *binaryString != '\0' && i < length; i++, binaryString++)
    {
        if (*binaryString == '0')
        {
            // Multiplying by zero, equal to zero. So you can discard statement written below.
            decimalNumber += 0 * pow(base, length - i - 1);
        }
        else if (*binaryString == '1')
        {
            decimalNumber += 1 * pow(base, length - i - 1);
        }
        else
        {
            printf("Invalid Binary Number \n");
            printf("Binary Number Contains only 0's or 1's \n");
            printf("Press enter to continue... \n");
            fflush(stdin);
            getchar();
            exit(0);
        }
    }
    return decimalNumber;
}
Convert Binary to Decimal in C Output Screenshot