0% found this document useful (0 votes)
4 views6 pages

Lecture 11

The document discusses type conversion in programming, explaining both implicit and explicit type casting with examples in C. It also includes code snippets for finding the number of digits in a number and checking if a number is an Armstrong number. The content is aimed at teaching special topics and problem-solving approaches in programming.

Uploaded by

najmulalamarif24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Lecture 11

The document discusses type conversion in programming, explaining both implicit and explicit type casting with examples in C. It also includes code snippets for finding the number of digits in a number and checking if a number is an Armstrong number. The content is aimed at teaching special topics and problem-solving approaches in programming.

Uploaded by

najmulalamarif24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

SPECIAL TOPICS AND

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.

// An example of implicit conversion


Implicit Type Conversion: #include<stdio.h>
• also known as ‘automatic type int main()
conversion‘ {
• done by the compiler on its own, int x = 10; // integer x
without any external trigger from char y = 'a'; // character c
the user
• Generally takes place when an // y implicitly converted to int. ASCII
expression contains more than // value of 'a' is 97
one data type. In such condition x = x + y;
type conversion (type
promotion) takes place to avoid // x is implicitly converted to float
loss of data. float z = x + 1.0;

printf("x = %d, z = %f", x, z);


return 0; 2
TYPE CONVERSION/ TYPE
CASTING
Explicit Type Conversion: // An example of explicit conversion
• User defined #include <stdio.h>
• the user can type cast the result to
make it of a particular data type int main()
• Syntax: {
(type) expression
int sum = 17, count = 5;
double mean;
Try to find the units digit
and the tenths digit of a mean = (double) sum / count;
floating point number! printf("Value of mean : %f\n", mean );
return 0;
}

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
}

You might also like