Skip to main content

CSE 102 - 02 : 27 Problems of Basic If-Else & Nested If-Else with Menu-Driven Program


01. Write a C program to accept two integers and check whether they are equal or not.



Test Data: 15 15
Expected Output:
Number1 and Number2 are equal


Solution :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h>
/************************************
** Moshiur Tonmoy *******************
** https://amardrafts.blogspot.com **  
************************************/

int main()
{
    int a,b;

    scanf("%d %d",&a,&b);

        if(a==b)
            {
                printf("Number1 and Number2 are equal");
            }
        else
            {
                printf("Number1 and Number2 are not equal");
            }


    return 0;
}

Sample Output :




02. Write a C program to check whether a given number is even or odd.


Test Data: 15
Expected Output:
15 is an odd integer

Solution :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h>
/************************************
** Moshiur Tonmoy *******************
** https://amardrafts.blogspot.com **  
************************************/

int main()
{
    int a;

    scanf("%d",&a);

    if(a%2==0)
    {
        printf("%d is an even integer",a);
    }
    else
    {
        printf("%d is an odd integer",a);
    }


    return 0;
}

Sample Output :




03. Write a C program to check whether a given number is positive or negative.


Test Data: 15
Expected Output:
15 is a positive number

Solution :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<stdio.h>
/************************************
** Moshiur Tonmoy *******************
** https://amardrafts.blogspot.com **  
************************************/

int main()
{
    int a;

    scanf("%d",&a);

        if(a>0)
            {
                printf("%d is a positive number",a);
            }
        else
            {
                printf("%d is a negative number",a);
            }

 return 0;
}

Sample Output :




04. Write a C program to find whether a given year is a leap year or not.


Test Data: 2016
Expected Output:
2016 is a leap year.

Solution :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<stdio.h>
/************************************
** Moshiur Tonmoy *******************
** https://amardrafts.blogspot.com **  
************************************/

int main()
{
    int y;
    scanf("%d",&y);

        if(y%4==0)
            {
                printf("%d is a leap year.",y);
            }
        else
            {
                printf("%d is not a leap year.",y);
            }

 return 0;
}

Sample Output :





05. Write a C program to read the age of a candidate and determine whether it is eligible for casting his/her own vote.


Test Data: 21
Expected Output:
Congratulation! You are eligible for casting your vote.

Solution :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<stdio.h>
/************************************
** Moshiur Tonmoy *******************
** https://amardrafts.blogspot.com **  
************************************/

int main()
{
    int v;

    scanf("%d",&v);

        if(v>=18)
        {
            printf("Congratulation! You are eligible for casting your vote.");
        }
        else
        {
            printf("Sorry! You are not eligible for casting your vote.");
        }

 return 0;
}

Sample Output :





06. Write a C program to read the value of an integer m and display the value of n is 1 when m is larger than 0, 0 when m is 0 and -1 when m is less than 0.


Test Data: -5
Expected Output:
The value of n = -1

Solution :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<stdio.h>
/************************************
** Moshiur Tonmoy *******************
** https://amardrafts.blogspot.com **  
************************************/

int main()
{
    int m,n;

    scanf("%d",&m);

        if(m>0)
            {
                printf("The value of n = 1");
            }
        else if(m==0)
            {
                printf("The value of n = 0");
            }
        else
            {
                printf("The value of n = -1");
            }

 return 0;
}

Sample Output :





07. Write a C program to accept the height of a person in centimeter and categorize the person according to their height.


Test Data: 135
Expected Output:
The person is Dwarf.

Solution :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<stdio.h>
/************************************
** Moshiur Tonmoy *******************
** https://amardrafts.blogspot.com **  
************************************/

int main()
{
    int h;

    scanf("%d",&h);

        if(h>=155)
            {
                printf("The person is tall");
            }
        else
            {
                printf("The person is dwarf");
            }

 return 0;
}

Sample Output :





08. Write a C program to find the largest of three numbers.


Test Data: 12 25 52
Expected Output:
1st Number = 12, 2nd Number = 25, 3rd Number = 52
The 3rd Number is the greatest among three

Solution :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<stdio.h>
/************************************
** Moshiur Tonmoy *******************
** https://amardrafts.blogspot.com **  
************************************/

int main()
{
    int x,y,z;

    scanf("%d %d %d",&x,&y,&z);

        if(x>y)
        {
            if(x>z)
                {
                    printf("1st Number = %d\t2nd Number = %d\t3rd Number = %d\nThe 1st Number is greatest among three",x,y,z);
                }
                else
                {
                    goto second;
                }
        }
        else
                {
                    goto second;
                }

        second:
        if(y>x)
        {
            if(y>z)
                {
                      printf("1st Number = %d\t2nd Number = %d\t3rd Number = %d\nThe 2nd Number is greatest among three",x,y,z);
                }
                else
                {
                    goto third;
                }
        }
        else
                {
                    goto third;
                }

        third:
                printf("1st Number = %d\t2nd Number = %d\t3rd Number = %d\nThe 3rd Number is greatest among three",x,y,z);


 return 0;
}

Sample Output :





09. Write a C program to accept a coordinate point in a XY coordinate system and determine in which quadrant the coordinate point lies.


Test Data: 7 9
Expected Output:
The coordinate point (7,9) lies in the First quadrant.

Solution :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<stdio.h>
/************************************
** Moshiur Tonmoy *******************
** https://amardrafts.blogspot.com **  
************************************/

int main()
{
    int x,y;

    scanf("%d %d",&x,&y);

        if(x>0)
            {
                if(y>0)
                    {
                        printf("The coordination point(%d,%d) lies in the First quadrant\n",x,y);
                    }
                else if(y<0)
                    {
                        printf("The coordination point(%d,%d) lies in the Fourth quadrant\n",x,y);
                    }
                else
                    {
                        printf("The coordination point(%d,%d) do not lies in any quadrant\n",x,y);
                    }

            }
        else if (x<0)
            {
                if(y>0)
                    {
                        printf("The coordination point(%d,%d) lies in the Second quadrant\n",x,y);
                    }
                else if(y<0)
                    {
                        printf("The coordination point(%d,%d) lies in the Third quadrant\n",x,y);
                    }
                else
                    {
                        printf("The coordination point(%d,%d) do not lies in any quadrant\n",x,y);
                    }
            }
        else if(x==0 && y == 0)
            {
                printf("The coordination point(%d,%d) i.e. the origin do not lies in any quadrant\n",x,y);
            }
        else
            {
                printf("Error!!");
            }

 return 0;
}

Sample Output :





10. Write a C program to find the eligibility of admission for a professional course based on the following criteria:

Marks in Maths >=65
Marks in Phy >=55
Marks in Chem>=50
Total in all three subject >=180
or
Total in Math and Chemistry >=140


Test Data:
Input the marks obtained in Physics :65
Input the marks obtained in Chemistry :51

Input the marks obtained in Mathematics :72
Expected Output:
The candidate is eligible for admission.

Solution :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<stdio.h>
/************************************
** Moshiur Tonmoy *******************
** https://amardrafts.blogspot.com **  
************************************/

int main()
{  int p,c,m;

   printf("Criteria of Eligibility :\n\n");
   printf("Marks in Maths \t\t\t>= 65\n");
   printf("Marks in Physics \t\t>= 55\n");
   printf("Marks in Chemistry \t\t>= 50\n");
   printf("Total in all three subject \t>= 180\n");
   printf("Total in Maths and Chemistry \t>= 140\n");///Question says,"Total in Maths and Subjects \t>= 140"

   printf("\nInput the marks obtained in Mathematics :");
   scanf("%d",&m);
   printf("Input the marks obtained in Physics :");
   scanf("%d",&p);
   printf("Input the marks obtained in Chemistry :");
   scanf("%d",&c);

       if (m>=65){
             if(p>=55){
                 if(c>=50){
                    if((m+p+c)>=180||(m+p)>=140){
                        printf("\nThe  candidate is eligible for admission.\n");
                                }
                    else{
                        printf("\nThe candidate is not eligible.\n");
                        }
                            }
                else{
                    printf("\nThe candidate is not eligible.\n");
                    }
                        }
            else{
                printf("\nThe candidate is not eligible.\n");
                }
                    }
        else{
            printf("\nThe candidate is not eligible.\n");
            }

return 0;

}

Sample Output :



11. Write a C program to calculate the root of a Quadratic Equation.


Test Data: 1 5 7
Expected Output:
Root are imaginary;
No solution.

Solution :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<stdio.h>
/************************************
** Moshiur Tonmoy *******************
** https://amardrafts.blogspot.com **  
************************************/
#include<math.h>
int main()
{
    float x1,x2;
    int a,b,c,det;

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

    det = b*b-4*a*c;

                if(det==0)
                    {
                        printf("Both roots are equal\n");
                        x1 = -b/(2.0*a);
                        x2 = x1;
                        printf("Root 1 :%.2f\tRoot 2 : %.2f",x1,x2);
                    }
                else if(det>0)
                    {
                        printf("Both roots are real\n");
                        x1 = (-b+sqrt(det))/(2*a);
                        x2 = (-b-sqrt(det))/(2*a);
                        printf("Root 1 : %.2f\tRoot 2 : %.2f\n",x1,x2);
                    }
                else
                    {
                        printf("Roots are imaginary;\nNo solution");
                    }


 return 0;
}

Sample Output :



12. Write a C program to read roll no, name and marks of three subjects and calculate the total, percentage and division.


Test Data:
Input the Roll Number of the student :784
Input the Name of the Student :James
Input the marks of Physics, Chemistry and Computer Application : 70 80 90
Expected Output:
Roll No : 784
Name of Student : James
Marks in Physics : 70
Marks in Chemistry : 80
Marks in Computer Application : 90
Total Marks = 240
Percentage = 80.00
Division = First

Solution :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<stdio.h>
/************************************
** Moshiur Tonmoy *******************
** https://amardrafts.blogspot.com **  
************************************/

int main()
{
    int r,m1,m2,m3;
    char name[20];

    printf("Input the Roll Number of the student: ");
    scanf("%d",&r);
    printf("Input the Name of the Student: ");
    scanf("%s",&name);
    printf("Input the obtained marks of Physics,Chemistry and Computer Application: ");
    scanf("%d %d %d",&m1,&m2,&m3);


    printf("Roll No : %d\n",r);
    printf("Name of Students: %s\n",name);
    printf("Marks in Physics : %d\n",m1);//marks in physics
    printf("Marks in Chemistry : %d\n",m2);//marks in chemistry
    printf("Marks in Computer Application : %d\n",m3);//marks in computer application
    printf("Total Marks = %d\n",m1+m2+m3);
    float p = (m1+m2+m3)/3;
    printf("Percentage = %.2f\n",p);


        if(p>=70)
            {
                printf("Division = First\n");
            }
        else if(p>=50)
            {
                printf("Division = Second\n");
            }
        else if(p>=40)
            {
                printf("Division = Third\n");
            }
        else
            {
                printf("Division = N\\A\n");
            }


 return 0;
}

Sample Output :





13. Write a C program to read temperature in centigrade and display a suitable message according to temperature state below :

Temp < 0 then Freezing weather
Temp 0-10 then Very Cold weather
Temp 10-20 then Cold weather
Temp 20-30 then Normal in Temp
Temp 30-40 then Its Hot
Temp >=40 then Its Very Hot


Test Data:
42
Expected Output:
Its very hot.

Solution :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<stdio.h>
/************************************
** Moshiur Tonmoy *******************
** https://amardrafts.blogspot.com **  
************************************/

int main()
{
    int temp;

    scanf("%d",&temp);

        if(temp<0)
            {
                printf("Freezing weather");
            }
        else if(temp>=0 && temp<=10)
                    {
                        printf("Very Cold Weather");
                    }
        else if(temp>=10 && temp<=20)
                    {
                        printf("Cold weather");
                    }
        else if(temp>=20 && temp<=30)
                    {
                        printf("Normal in Temp");
                    }
        else if(temp>=30 && temp<40)
                    {
                        printf("Its Hot");
                    }
        else
                    {
                        printf("Its very Hot");
                    }


 return 0;
}

Sample Output :



14. Write a C program to check whether a triangle is Equilateral, Isosceles or Scalene.


Test Data:
50 50 60
Expected Output:
This is an isosceles triangle.

Solution :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<stdio.h>
/************************************
** Moshiur Tonmoy *******************
** https://amardrafts.blogspot.com **  
************************************/

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

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

        if(a==b && b==c)
            {
                printf("This is an equilateral triangle.");
            }
        else if(a==b || b==a || a==c)
            {
                printf("This is an isosceles triangle");
            }
        else
            {
                printf("This is an scalene triangle");
            }


 return 0;
}

Sample Output :



15. Write a C program to check whether a triangle can be formed by the given value for the angles.


Test Data:
40 55 65
Expected Output:
The triangle is not valid.

Solution :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h>
/************************************
** Moshiur Tonmoy *******************
** https://amardrafts.blogspot.com **  
************************************/

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

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

        if(a+b+c==180)
            {
                printf("The triangle is valid");
            }
            else
                    {
                        printf("The triangle is not valid");
                    }


 return 0;
}

Sample Output :



16. Write a C program to check whether a character is an alphabet, digit or special character.


Test Data:
@
Expected Output:
This is a special character.

Solution :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<stdio.h>
/************************************
** Moshiur Tonmoy *******************
** https://amardrafts.blogspot.com **  
************************************/

int main()
{
    char z;

    scanf("%c",&z);

        if((z>='A' && z<='Z') || (z>='a' && z<='z'))
            {
                printf("This is an alphabet.");
            }
        else if(z>='0' && z<='9')
            {
                printf("This is a digit.");
            }
        else
            {
                printf("This is an special character");
            }


 return 0;
}

Sample Output :




17. Write a C program to check whether an alphabet is a vowel or consonant.


Test Data:
k
Expected Output:
The alphabet is a consonant.

Solution :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h>
/************************************
** Moshiur Tonmoy *******************
** https://amardrafts.blogspot.com **  
************************************/

int main()
{
    char z;

    scanf("%c",&z);

        if(z=='A' || z=='E' || z=='I' || z=='O' || z=='U' || z=='a' || z=='e' || z=='i' || z=='o' || z=='u')
            {
                printf("The alphabet is a vowel");
            }
        else
            {
                printf("The alphabet is a consonant");
            }


 return 0;
}

Sample Output :



18. Write a C program to calculate profit and loss on a transaction.


Test Data:
500 700
Expected Output:
You can booked your profit amount : 200

Solution :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<stdio.h>
/************************************
** Moshiur Tonmoy *******************
** https://amardrafts.blogspot.com **  
************************************/

int main()
{
    int c,s;

    scanf("%d %d",&c,&s);

        if(s>c)
            {
                printf("You can booked your profit amount : %d",s-c);
            }
        else if (c>s)
            {
                printf("Your loss amount : %d",c-s);
            }
        else
        {
            printf("Neither loss nor profit");
        }


 return 0;
}

Sample Output :



19. Write a program in C to calculate and print the Electricity bill of a given customer. The customer id., name and unit consumed by the user should be taken from the keyboard and display the total amount to pay to the customer. The charge are as follow:



Test Data:
1001
James
800
Expected Output:
Customer IDNO :1001
Customer Name :James
unit Consumed :800
Amount Charges @bdt. 2.00 per unit : 1600.00

Surcharge Amount : 240.00
Net Amount Paid By the Customer : 1840.00

Solution :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<stdio.h>
/************************************
** Moshiur Tonmoy *******************
** https://amardrafts.blogspot.com **  
************************************/

int main()
{
    int id,unit;
    float bill,sur=0,cu,netbill;
    char name[20];

    again:
    scanf("%d %s %d",&id,&name,&unit);

    printf("Customer IDNO : %d\n",id);
    printf("Customer Name : %s\n",name);
    printf("Unit Consumed : %d\n",unit);

        if(unit>=0 && unit<=199)
            {
                cu = 1.20;
                printf("Amount Charges @bdt. %.2f per unit : %.2f\n",cu,cu*unit);
            }
        else if(unit>=200 && unit <400)
            {
                cu = 1.50;
                printf("Amount Charges @bdt. %.2f per unit : %.2f\n",cu,cu*unit);
            }
        else if(unit>=400 && unit <600)
            {
                cu = 1.80;
                printf("Amount Charges @bdt. %.2f per unit : %.2f\n",cu,cu*unit);
            }
        else if (unit>=600)
            {
                cu = 2.00;
                printf("Amount Charges @bdt. %.2f per unit : %.2f\n",cu,cu*unit);
            }
        else
        {
            printf("\nError in unit value!\nPlease try again!\n");
            goto again;
        }

        bill = cu*unit;

        if(bill>400)
        {
            sur=bill*0.15;
            printf("Surcharge Amount : %.2f\n",sur);
            netbill = bill + sur;
        }
        else
        {
            sur = bill*0.00;
            printf("Surcharge Amount : %.2f\n",sur);
            netbill=bill+sur;
        }
        printf("Net Amount Paid By the Customer : %.2f\n",netbill);


 return 0;
}

Sample Output :



20. Write a program in C to accept a grade and declare the equivalent description :



Test Data:
Input the grade :A
Expected Output:
You have chosen : Average


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<stdio.h>
/************************************
** Moshiur Tonmoy *******************
** https://amardrafts.blogspot.com **  
************************************/

int main()
{
    char a;
    scanf("%c",&a);

        if(a=='E')
            {
                printf("You have chosen : Excellent");
            }
        else if(a=='V')
            {
                printf("You have chosen : Very Good");
            }
        else if(a=='G')
            {
                printf("You have chosen : Good");
            }
        else if(a=='A')
            {
                printf("You have chosen : Average");
            }
        else if (a=='F')
            {
                printf("You have chosen : Fail");
            }
        else
        {
            printf("Input is not defined");
        }

 return 0;
}

Sample Output :



21. Write a program in C to read any day number in integer and display day name in the word.


Test Data:
4
Expected Output:
Thursday

Solution :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<stdio.h>
/************************************
** Moshiur Tonmoy *******************
** https://amardrafts.blogspot.com **  
************************************/

int main()
{
    int a;
    scanf("%d",&a);

        if(a==1)
            {
                printf("Monday");
            }
        else if(a==2)
            {
                printf("Tuesday");
            }
        else if(a==3)
            {
                printf("Wednesday");
            }
        else if(a==4)
            {
                printf("Thursday");
            }
        else if (a==5)
            {
                printf("Friday");
            }
        else if (a==6)
            {
                printf("Saturday");
            }
        else if (a==7)
            {
                printf("Friday");
            }
        else
        {
            printf("Error!");
        }


 return 0;
}

Sample Output :



22. Write a program in C to read any digit, display in the word.


Test Data:
4
Expected Output:
Four

Solution :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<stdio.h>
/************************************
** Moshiur Tonmoy *******************
** https://amardrafts.blogspot.com **  
************************************/

int main()
{
    int a;
    scanf("%d",&a);

        if(a==0)
            {
                printf("zero");
            }
        else if(a==1)
            {
                printf("One");
            }
        else if(a==2)
            {
                printf("Two");
            }
        else if(a==3)
            {
                printf("Three");
            }
        else if (a==4)
            {
                printf("Four");
            }
        else if (a==5)
            {
                printf("Five");
            }
        else if (a==6)
            {
                printf("Six");
            }
        else if (a==7)
            {
                printf("Seven");
            }
        else if (a==8)
            {
                printf("Eight");
            }
        else if (a==9)
            {
                printf("Nine");
            }
        else
            {
                printf("Error");
            }

 return 0;
}

Sample Output :



23. Write a program in C to read any Month Number in integer and display Month name in the word.


Test Data:
4
Expected Output:
April

Solution :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<stdio.h>
/************************************
** Moshiur Tonmoy *******************
** https://amardrafts.blogspot.com **  
************************************/

int main()
{
    int a;
    scanf("%d",&a);

        if(a==1)
            {
                printf("January");
            }
        else if(a==2)
            {
                printf("February");
            }
        else if(a==3)
            {
                printf("March");
            }
        else if (a==4)
            {
                printf("April");
            }
        else if (a==5)
            {
                printf("May");
            }
        else if (a==6)
            {
                printf("June");
            }
        else if (a==7)
            {
                printf("July");
            }
        else if (a==8)
            {
                printf("August");
            }
        else if (a==9)
            {
                printf("September");
            }
        else if (a==10)
            {
                printf("October");
            }
        else if (a==11)
            {
                printf("November");
            }
        else if (a==12)
            {
                printf("December");
            }
        else
            {
                printf("Error!");
            }

 return 0;
}

Sample Output :



24. Write a program in C to read any Month Number in integer and display the number of days for this month.


Test Data:
4
Expected Output:
April

Solution :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<stdio.h>
/************************************
** Moshiur Tonmoy *******************
** https://amardrafts.blogspot.com **  
************************************/

int main()
{
    int m;
    scanf("%d",&m);

        if(m<=8)
            {
                if(m==8)
                {
                    printf("Month have 31 days\n");
                }
                else if(m==2)
                {
                     printf("Month have 28 days (29 days if it is leap year)\n");
                }
                else if(m%2==0)
                {
                    printf("Month have 30 days\n");
                }
                else
                {
                    printf("Month have 31 days\n");
                }
            }
        else if(m>8)
            {
                if(m%2 == 0)
                {
                    printf("Month have 31 days\n");
                }
                else
                {
                    printf("Month have 30 days\n");
                }
            }


 return 0;
}

Sample Output :



25. Write a program in C which is a Menu-Driven Program to compute the area of the various geometrical shape.


Test Data:
1
5
Expected Output:
The area is : 78.500000

Solution :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<stdio.h>
/************************************
** Moshiur Tonmoy *******************
** https://amardrafts.blogspot.com **  
************************************/

int main()
{
    int x,y;
    char c;

    retry :
    printf("SHAPE\t\t\tKEY\n\nTriangle\t\tT\nCircle\t\t\tC\nRectangle\t\tR\nSquare\t\t\tS\n\nInput a KEY : ");
    scanf("%c",&c);

        if(c=='T')
            {
                printf("Input the value of base & height :\n");
                scanf("%d %d",&x,&y);
                printf("The area is : %f\n",0.5*x*y);
            }
        else if(c=='C')
            {
                printf("Input the value of radius :\n");
                scanf("%d",&x);
                printf("The area is : %f\n",3.1416*x*x);
            }
        else if(c=='R')
            {
                printf("Input the value of length & width :\n");
                scanf("%d %d",&x,&y);
                printf("The area is : %f\n",(float)x*y);
            }
        else if(c=='S')
            {
                printf("Input the value of any side :\n");
                scanf("%d",&x);
                printf("The area is : %f\n",(float)x*x);///in a square, x==y
            }
        else
            {
                printf("Wrong input!!\nPlease try again!");
                goto retry;
            }


 return 0;
}

Sample Output :



26. Write a program in C which is a Menu-Driven Program to perform a simple calculation.


Test Data:
10
2
3
Expected Output:
The Multiplication of 10 and 2 is: 20

Solution :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<stdio.h>
/************************************
** Moshiur Tonmoy *******************
** https://amardrafts.blogspot.com **  
************************************/

int main()
{
    int x,y;
    char c;

    retry :
    printf("Operations\t\tKEY\n\nAddition\t\t+\nSubtraction\t\t-\nMultiplication\t\t*\nDivision\t\t/\n\nInput a KEY : ");
    scanf("%c",&c);

        if(c=='+')
            {
                scanf("%d %d",&x,&y);
                printf("%d\n",x+y);
            }
        else if(c=='-')
            {
                scanf("%d %d",&x,&y);
                printf("%d\n",x-y);
            }
        else if(c=='*')
            {
                scanf("%d %d",&x,&y);
                printf("%d\n",x*y);
            }
        else if(c=='/')
            {
                scanf("%d %d",&x,&y);
                printf("%.2f\n",(float)x/y);
            }
        else
            {
                printf("Wrong input!!\nPlease try again!");
                goto retry;
            }


 return 0;
}

Sample Output :

Comments

Popular posts from this blog