UIU: Learn C by Examples : Declarations and Initializations


Program 1


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>
#include <cmath>

int main ()
{
    printf ("%d\n", 14 % 5);

    // printf ("%lf\n", 14.0 % 5.0);   // Error!!
    // error: invalid operands of types 'double' and 'double' to binary 'operator%'

    printf ("%lf\n", fmod(14.0, 5.0));

	return 0;
}

// @END_OF_SOURCE_CODE

১. যদি দুইটা নাম্বার ‘double’ হয় তাহলে আমরা % দিয়ে তাদের ভাগশেষ বের করতে পারব না।
২. fmod() ব্যবহার করতে হবে।

Program 2


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>
#include <cmath>

int main ()
{
    printf ("%lf\n", ceil(5.5));

    printf ("%lf\n", ceil(5.01));

    printf ("%lf\n", ceil(5.99));

    printf ("%lf\n", floor(5.5));

    printf ("%lf\n", floor(5.01));

    printf ("%lf\n", floor(5.99));

	return 0;
}

// @END_OF_SOURCE_CODE

Program 3


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main ()
{
    int a;

    printf ("int size: %d\n", sizeof a);

    float b;

    printf  ("float size: %d\n", sizeof b);

    double c;

    printf  ("double size: %d\n", sizeof c);

    char d;

    printf  ("character size: %d\n", sizeof d);

    bool e;

    printf  ("boolean size: %d\n", sizeof e);

    int anArray [10];

    printf  ("Array size: %d\n", sizeof anArray);

	return 0;
}

// @END_OF_SOURCE_CODE

Program 4


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int x = 40;

void aFunction (int x)
{
    printf ("Inside aFunction %d\n", x);

    x = 50;
}

void anotherFunction ()
{
    x = 100;
}

void yetAnotherFunction ()
{
    printf ("Inside yetAnotherFunction %d\n", x);
}

int main ()
{
    int x = 20;

    printf ("%d\n", x);

    aFunction(x);

    printf ("after aFunction %d\n", x);

    anotherFunction();

    printf ("after anotherFunction %d\n", x);

    x = 80;

    yetAnotherFunction();

    printf ("after yetAnotherFunction %d\n", x);

	return 0;
}

// @END_OF_SOURCE_CODE

Program 5


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main ()
{
    int a [5] = {5, 4, 3, 2, 1};

    printf ("%d\n", a [2]);
    printf ("%d\n", a [1]);
    printf ("%d\n", a [4]);

    int b [5] = {5, 6, 7};

    printf ("%d\n", b [2]);
    printf ("%d\n", b [1]);
    printf ("%d\n", b [4]);

	return 0;
}

// @END_OF_SOURCE_CODE

Program 6


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main ()
{
    int a, b, c;

    int p = scanf ("%d", &a);

    int q = scanf ("%d %d", &a, &b);

    int r = scanf ("%d %d %d", &a, &b, &c);

    printf ("%d %d %d\n", p, q, r);

	return 0;
}

// @END_OF_SOURCE_CODE

Program 7


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main ()
{
    int x = 20;

    {
        int x = 40;

        printf ("Inside block %d\n", x);
    }

    printf ("%d\n", x);

	return 0;
}

// @END_OF_SOURCE_CODE

Program 8


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

typedef int Char;

int main ()
{
    Char x = 10;

    printf ("%d\n", x);

	return 0;
}

// @END_OF_SOURCE_CODE

Acknowledgment: http://indiabix.com/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s