Lecture 11
Lecture 11
PROBLEM SOLVING
APPROACH-1
Lecture: 7,8
1
TYPE CONVERSION/ TYPE
CASTING
Converting one datatype into another is known as type casting or, type-conversion.
3
Find the number of digits in a
number
#include<stdio.h>
int main()
{
int a=2;
int i=0;
while(a)
{
a=a/10;
i++;
}
printf("%d",i);
}
4
Finding the digits of a number
#include<stdio.h>
int main()
{
int a=2564;
int i=0;
int d;
while(a)
{
d=a%10;
printf("%d\t",d);
a=a/10;
i++;
}
} 5
ARMSTRONG NUMBER CHECKER
• Armstrong number is a number that is equal to the sum of cubes of its digits. For
example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers.
#include<stdio.h>
int main()
{
int a; Look up the definitions of
scanf("%d",&a); other special numbers like
int sum=0; Harshad number, Dudney
int b=a; number on the internet.
int i;
From those definitions,try
while(b!=0)
{ implementing a code to
i=b%10; check if an input is a
b=b/10; special number or not.
sum=sum+i*i*i;
}
if(sum==a)
printf("Armstrong");
else
printf("Not Armstrong");
6
}