Friday, May 6, 2022

Simple Write a program assignment in C language-02

Learning C programming language is mandatory for today’s students [as one programming language], during their academic career. The C language has many mechanisms to deal with decision making, repetitive tasks, such as if, if…else, for, while etc. Here are some of such C codes which may asked in theory/practical exams.

C is a typed language, so no need of code explanation as everything will be cleared by itself by reading the code. If you know C, you know this code. It’s that simple!

The Code::Blocks IDE 20.03+MinGW and Cygwin tool are used to test the code.

Code2a

Write A Program to display first five numbers and their sum using while loop

 

 

//WAP#2a:WAP to display first five numbers

// and their sum using while loop

/*

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

   int i, sum; //declare variables

   i = 1; sum =0; //initialize variables

 

   while (i <= 5)

    {

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

        sum = sum + i;

        i = i + 1;

    }

    printf("sum is :%d\n",sum);

 

    return 0;

}

//end of program

 

//-------------------------

//output WAP#2a:

 

/*

1

2

3

4

5

sum is :15

*/

 

 

 

 

 

Code3a

Write A Program to ask user to base value and then display a multiplication table using while loop

 

 

//WAP#3a:WAP to ask user for base value &

// then display a multiplication table using

// while loop

/*

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

   int num,counter;//declare variables

   counter = 1;

 

   printf("Enter base value [integer]: ");

   scanf("%d",&num); //get base value

 

   while (counter <= 10)

    {

        printf("%d * %d = %d\n",

               num,counter, num*counter);

        counter = counter + 1;

    }

 

    return 0;

}

//end of program

 

//-----------------------

//output WAP#3a:

 

/*

Enter base value [integer]: 5

5 * 1 = 5

5 * 2 = 10

5 * 3 = 15

5 * 4 = 20

5 * 5 = 25

5 * 6 = 30

5 * 7 = 35

5 * 8 = 40

5 * 9 = 45

5 * 10 = 50

*/

 

 

 

 

 

Code3b

Write A Program to ask base value from user, and then display a multiplication table using for loop in Ascending order

 

 

 

//WAP#3b:WAP to ask user for base value &

// then display a multiplication table using

// for loop in Ascending order.

/*

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

    int num;//declare variable

 

    printf("Enter base value [integer]: ");

    scanf("%d",&num); //get base value

 

    for (int i=1; i<=10; i++)

    {

        printf("%d * %d = %d\n",

               num,i, num*i);

    }

 

    return 0;

}

//end of program

 

//---------------------

//output WAP#3b:

 

/*

Enter base value [integer]: 6

6 * 1 = 6

6 * 2 = 12

6 * 3 = 18

6 * 4 = 24

6 * 5 = 30

6 * 6 = 36

6 * 7 = 42

6 * 8 = 48

6 * 9 = 54

6 * 10 = 60

 

*/

 

 

 

 

 

Code3c

Write A Program to ask base value from user, and then display a multiplication table using for loop in Descending order

 

 

 

//WAP#3c:WAP to ask user for base value &

// then display a multiplication table using

// for loop in descending order.

/*

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

    int num;//declare variable

 

    printf("Enter base value [integer]: ");

    scanf("%d",&num); //get base value

 

    for (int i=10; i>=1; i--)

    {

        printf("%d * %d = %d\n",

               num,i, num*i);

    }

 

    return 0;

}

//end of program

*/

//output WAP#3c:

 

/*

Enter base value [integer]: 6

6 * 10 = 60

6 * 9 = 54

6 * 8 = 48

6 * 7 = 42

6 * 6 = 36

6 * 5 = 30

6 * 4 = 24

6 * 3 = 18

6 * 2 = 12

6 * 1 = 6

 

*/

 

 

 

 

 

Code3d

Write A Program to ask base value from user, and then display a multiplication table using do…while loop

 

 

 

//WAP#3d:WAP to ask user for base value &

// then display a multiplication table using

// do...while loop.

/*

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

    int num,i;//declare variable

    i=1;

 

    printf("Enter base value [integer]: ");

    scanf("%d",&num); //get base value

 

    do

    {

        printf("%d * %d = %d\n",

               num,i, num*i);

        i = i + 1;

    }while(i<=10);

 

    return 0;

}

//end of program

 

//--------------------------

//output WAP#3d:

 

/*

Enter base value [integer]: 6

6 * 1 = 6

6 * 2 = 12

6 * 3 = 18

6 * 4 = 24

6 * 5 = 30

6 * 6 = 36

6 * 7 = 42

6 * 8 = 48

6 * 9 = 54

6 * 10 = 60

 

*/

 

 

 

 

 

For such codes click CCode label or check next day post on right-side post history bar.

---till next post, take care and bye-bye.

No comments:

Post a Comment