Thursday, February 1, 2024

C Language - Combination of Loop & Conditional Statements - Write a program to check a given number is Armstrong or not. A number is said to be Armstrong if sum of the cube of the individual digit is equal to the number

Write a program to check a given number is Armstrong or not. A number is said to be Armstrong if sum of the cube of the individual digit is equal to the number.


Example: - 153 = (1)3 + (5)3 + (3)3.

#include <stdio.h>
#include <conio.h>

void main()
{
            int num,num1,digit,arm=0;
            clrscr();
            printf("\nENTER A NUMBER: ");
            scanf("%d",&num);
            num1=num;
            do
            {
                        digit=num%10;
                        num=num/10;
                        arm=arm+(digit*digit*digit);
            }
            while(num!=0);
            if(arm==num1)
                        printf("\n%d IS AN AMSTORNG NUMBER",num1);
            else
                        printf("\n%d IS NOT AN AMSTRONG NUMBER",num1);
            getch();
}
 

C Language - Loop Control Statements - Do While Loop - Write a program to print the sum of digit of a given number

Question: Write a program to print the sum of digit of a given number.

Solution:

#include <stdio.h>

#include <conio.h>

void main()

{

            int digit;

            unsigned long num,sum=0;

            clrscr();

            printf("\nENTER A NUMBER: ");

            scanf("%lu",&num);

            do

            {

                        digit=num%10;

                        num=num/10;

                        sum=sum+digit;

            }

            while(num!=0);

            printf("\nSUM OF THE DIGITS IS %lu",sum);

            getch();

}

C Language - Loop Control Statements - For Loop - Write a program to compute the factorial of a given number.

For Loop Example 9 : Write a program to compute the factorial of a given number. 

Solution: 

#include <stdio.h>

#include <conio.h>

void main()

{

            int i=1,n;

            unsigned long fact=1;

            clrscr();

            printf("\nENTER A NUMBER: ");

            scanf("%d",&n);

            for(i=1;i<=n;i+=1)

            {

                        fact=fact*i;

            }

            printf("\nFACTORIAL OF %d IS %lu",n,fact);

            getch();

}

C Language - Conditional Control Statements - If Conditions - Write a program to check whether a given year is leap year or not.

 Control Statements (Conditional Control Statements)
If Conditions

Example 8: Write a program to check whether a given year is leap year or not.

#include <stdio.h>

#include <conio.h>

void main()

{

            int year,n;

            clrscr();

            printf("\nENTER A YEAR: ");

            scanf("%d",&year);

            if(year%4==0 && year%100 !=0 || year%400==0)

                        printf("\n%d IS A LEAP YEAR",year);

            else

                        printf("\n%d IS NOT A LEAP YEAR",year);

            getch();

}

 

 

C Language - Conditional Control Statements - If Conditions - Write a program to compute the division from the given marks of 5 subjects

 Control Statements (Conditional Control Statements)
If Conditions

Example 7: Write a program to compute the division from the given marks of 5 subjects. The division can be calculated as follows: -

Average Mark

Division

>=60

First

>=50

Second

>=40

Third

<40

Fai

#include <stdio.h>

#include <conio.h>

void main()

{

            int m1,m2,m3,m4,m5,per;

            clrscr();

            printf("\nENTER THE MARKS OF THE SUBJECTS:\n");

            scanf("%d %d %d %d %d",&m1,&m2,&m3,&m4,&m5);

            per=(m1+m2+m3+m4+m5)/5;

            if(per>=60)

                        printf("\nFIRST DIVISION");

            else

            {

                        if(per>=50)

                                    printf("\nSECOND DIVISION");

                        else

                        {

                                    if(per>=40)

                                                printf("\nTHIRD DIVISION");

                                    else

                                                printf("\nFAIL");

                        }

            }

            getch();

}

 

C Language - Conditional Control Statements - If Conditions - Find out the highest number from three given numbers.

Control Statements (Conditional Control Statements)
If Conditions

Example 2: Find out the highest number from three given numbers.

#include <stdio.h>

#include <conio.h>

void main()

{

            int a,b,c,h;

            clrscr();

            printf("\nENTER THREE NUMBERS:\n");

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

            h=a;

            if(b>h)

                        h=b;

            if(c>h)

                        h=c;

            printf("\nHIGHEST NUMBER IS %d",h);

            getch();

}

 

Alternative Method

 

#include <stdio.h>

#include <conio.h>

void main()

{

            int a,b,c;

            clrscr();

            printf("\nENTER THREE NUMBERS:\n");

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

            if(a>b && a>c)

                        printf("\nHIGHEST NUMBER IS %d",a);

            if(b>c && b>a)

                        printf("\nHIGHEST NUMBER IS %d",b);

            if(c>a && c>b)

                        printf("\nHIGHEST NUMBER IS %d",c);

            getch();

}

 

C Language - Conditional Control Statements - If Conditions

Control Statements (Conditional Control Statements)
If Conditions

Example 1: Write a program to compute net amount from the given quantity purchased and rate per quantity. Discount @10% is allowed if quantity purchased exceeds 100.

Net Amount = (Quantity Purchased x Rate Per Quantity) – Discount.

#include <stdio.h>
#include <conio.h>
void main()
{
            int qty,rate;
            float disc=0.0,net;
            clrscr();
            printf("\nENTER QUANTITY: ");
            scanf("%d",&qty);
            printf("\nENTER RATE: ");
            scanf("%d",&rate);
            if (qty>100)
                        disc=qty*rate*10/100;
            net=(qty*rate)-disc;
            printf("\nNET AMOUNT: %0.2f",net);
            getch();
}