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>
We are trying to contribute to the net what ever Technical knowledge. I Believe that knowledge increases by spreading and sharing with others.
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>
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();
}
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();
}
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();
}
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();
}
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();
}
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.