29 October 2014

//Written by MA Saleh
#include<stdio.h>
int main()
{
    int a, b, t;
    printf("Enter Two Number for find GCD: ");

    while(scanf("%d %d", &a, &b) == 2) //Take two number as input
    {
        if(a == 0)
        {
            printf("Gcd = %d\n", b);
        }
        else if(b == 0 || a == b)
        {
            printf("Gcd  = %d\n", a);
        }
        else if(a > b)
        {
            while(b != 0)
            {
                t = b;
                b = a % b;
                a = t;
            }
            printf("Gcd = %d\n", a);
        }
        else if(b > a)
        {
            while(a != 0)
            {
                t = a;
                a = b % a;
                b = t;
            }
            printf("Gcd = %d\n", b);
        }
        printf("Again give two number: \n");

    }


    return 0;

    //Copyright@MAS
}

Sample Input: 10 30, 4 28


Sample Output:


Posted on Wednesday, October 29, 2014 by ICT Lab

No comments

28 October 2014


//Written by MA Saleh
#include<stdio.h>
int main()
{
    int binary[30];
    int decimal_no, i = 0, j;

    printf("Enter the Decimal no which u want to convert to Binary: \n");


    while(scanf("%d", &decimal_no) == 1)

    {
        while(decimal_no > 0)
    {
        binary[i] = decimal_no % 2;
        i++;
        decimal_no = decimal_no/2;
    }

    for(j = i-1; j >=0; j--)

    {
        printf("%d", binary[j]);
    }
    printf("\n\n");
    i = 0; j = 0;
    }

    return 0;


    //copyright@MAS
}


Sample Input: 12, 15


Sample Output:



Previous Post(Factorial Using Recursion )

Posted on Tuesday, October 28, 2014 by ICT Lab

No comments

24 October 2014

//Written by MA Saleh
#include<stdio.h>
//This is a function for Factorial Number
long long find_factorial(int n)
{
    if (n == 0)
    {
        return 1;
    }
    else
    {
        return (n * find_factorial (n - 1)); //using recursion
    }
}

int main()
{
    int num;
    long long int factorial;

    printf("Enter a number for find factorial: ");
    while(scanf("%d", &num) == 1) //Take a number as input from user
    {
        if(num < 0)
    {
        printf("Negative is not allowed\n");
    }
    else
    {
        factorial = find_factorial(num); //call the function
        printf("%d! = %lld\n", num, factorial); //print the result
    }
    }

    return 0;

    //Copyright@MAS

}


Sample Input: 5, 10


Sample Output:




Posted on Friday, October 24, 2014 by ICT Lab

No comments


//Written by MA Saleh
#include<stdio.h>
int main()
{
    int i, n;
    long long int factorial = 1;
    printf("Given  a number for finding factorial: ");
    while(1==1)
    {
        scanf("%d", &n);
        if(n < 0)
        {
            printf("Negative number not allowed\n");
        }
        else if(n == 0)
        {
            printf("0! = 1\n");
        }
        else
        {
            for(i = n; i > 0; i--)
            {
                factorial = factorial * i;
            }
            printf("%d! = %lld\n", n, factorial); //print the result
            factorial = 1;
        }
    }
    return 0;
    //copyright@MAS
    }

Posted on Friday, October 24, 2014 by ICT Lab

3 comments

22 October 2014

//Written by MA Saleh

//Check your word is polindrome?

#include<stdio.h>

#include<string.h>

int main()

{

    int i, j, m, length;

    char word[50];

    scanf("%s", &word);

    length = strlen(word);

    m = length/2;

    for(i = m, j = length - m-1; i>0; i--,j++)

    {

        if(word[i] != word[j])

        {

            printf("%s is not a polindrome word", word);

            break;

        }

    }

    if(word[i] == word[j])

    {

        printf("%s is a polindrome word", word);

    }

else
    {
        printf("%s is not a polindrome word", word);
    }

    return 0;

    //copyright@MAS

}


Sample Input: madam, you


Sample Output:



Posted on Wednesday, October 22, 2014 by ICT Lab

2 comments

//Written by:MA saleh
//find prime using function
#include<stdio.h>
#include<math.h>
int prime(int n)
{
    int i, z;
if(n < 2)
    {
        return 0;
    }
    if (n == 2)
    {
        return 1;
    }
    if(n%2== 0)
    {
        return 0;
    }
    z = sqrt(n); //get root of this number
    for(i = 3; i<= z; i= i+2)
    {
        if(n%i == 0)
        {
            return 0;
        }
    }
    return 1;
}
int main()
{
    int n;
    printf("Enter a number for checking prime: ");
    while(scanf("%d", &n) == 1)
    {
        int is_prime = prime(n);
        if(is_prime == 1)
        {
            printf("The number is prime\n");
        }
        else
        {
            printf("The number is not prime\n");
        }
    }
    return 0;
    //Copyright@MAS
}

Sample Input: 5, 10, 0


Sample Output: 


Posted on Wednesday, October 22, 2014 by ICT Lab

No comments


//Written by MA Saleh

#include<stdio.h>

int main()
{
    int i, j, n = 0, k, row;
    printf("Enter the number of rows: ");
    scanf("%d", &row);
    k = row;

    for( i = 0; i<row; i++)
    {
        for(j = 0; j<=i; j++)
        {
            printf("*");
        }
        while(n != 2*k-2)
            {
                printf(" ");
                n++;
            }
            k = k -1;
        for(j = 0; j <= i; j++)
        {
            printf("*");
        }
        n = 0;
        printf("\n");

    }
    return 0;
//cpyright@MAS
}

Sample Input: 5, 10

Sample Output: 




Related Post (Making a Pyramid)
Previous Post (Fibonacci Number)
After this Post (Prime Number)

Posted on Wednesday, October 22, 2014 by ICT Lab

2 comments

21 October 2014

//find fibonacci number for n times
#include<stdio.h>
int main()
{
    int n, i = 0, j = 1, sum = 0, count;

    printf("Enter a number which you want to find for fibonacci: ");
    scanf("%d", &n); //input a number which is how many times u want find fibonnacci

    printf("%d ", i);
    printf("%d", j);
    count = 2;
    while(count < n)
    {
        sum = i+ j;
        printf(" %d", sum);
        i = j;
        j = sum;
        count++;
    }
    return 0;
}

Sample Input: 5 10


Sample Output:





Posted on Tuesday, October 21, 2014 by ICT Lab

2 comments