Professional Documents
Culture Documents
CS111 Intro. To CS (Lecture 12 - Fall 2021)
CS111 Intro. To CS (Lecture 12 - Fall 2021)
12.3 Recursion, Base Cases (and Recursion without a Base Case), & Recursive
Functions.
5
/*EMI Calculator (C program to calculate EMI).*/
#include <stdio.h>
#include <math.h>
int main() {
float principal, rate, time, emi;
printf("Enter principal: "); scanf( "%f", &principal );
printf("Enter rate: "); scanf( "%f", &rate);
printf("Enter time in year: "); scanf( "%f", &time);
rate = rate / ( 12 * 100 ); /*one month interest*/
time = time * 12; /*one month period*/
emi = calculate_emi( principal, rate, time );
printf("Monthly EMI is = %f\n", emi );
return 0;
6
}
Please pause the video and try to answer the question on your own!
7
Please pause the video and try to answer the question on your own!
First run:
Enter an integer number: 12321
12321 is a palindrome.
Second run:
Enter an integer number: 1234
1234 is not a palindrome.
8
/* C program to check whether a number is palindrome or not */
#include <stdio.h>
/*function to check Palindrome Number*/
int isPalindrome( int num ) {
int tempNumber = num; int dig, revNumber;
/*getting reverse number*/
revNumber = 0;
while(num > 0) {
dig = num % 10;
revNumber = ( revNumber * 10 ) + dig;
num/=10; }
if( revNumber == tempNumber )
return 1; /*Palindrome Number*/
else
return 0; /*Not a Palindrome Number*/
}
int main() {
int number;
printf("Enter an integer number: "); scanf("%d", &number);
if( isPalindrome( number ) ) printf("%d is a palindrome.", number);
else printf("%d is not a palindrome.", number);
9
return 0; }
Please pause the video and try to answer the question on your own!
o This program will read an integer number and count the total
number of digits in that number. For example, the total number of
digits in the integer 1234 is 4.
10
/* C program to count digits in a number.*/
#include <stdio.h>
/*function to count digits*/
int countDigits( int num ) {
int count = 0;
while( num > 0 ) {
count++;
num/=10;
}
return count;
}
int main() {
int num, cnt;
printf("Enter a number: ");
scanf("%d", &num);
cnt = countDigits( num );
printf("Total numbers of digits are: %d in the number: %d.", cnt, num);
return 0;
11
}
Lecture 12: Solved Examples [ 2nd Set ]
Solved Examples on:
12.3 Recursion, Base Cases (and Recursion without a Base Case), & Recursive
Functions.
o For example, if the user entered the number 12311, in which (s)he
wants to find the occurrence of the digit 1, the occurrence of 1 will
be 3 in the given number (12311).
13
/*C program to print occurrence of a particular digit in a number.*/
#include <stdio.h>
/*function to get occurrence of a digit in a number*/
int findOccurrence( int num, int dig ) {
int rem, cnt; cnt = 0;
while( num > 0) {
rem = num%10;
if( rem == dig )
cnt++;
num/=10;
}
return cnt;
}
int main() {
int num, digit, cnt;
printf("Enter a number: "); scanf("%d", &num);
printf("Enter a digit to search: "); scanf("%d", &digit);
cnt = findOccurrence( num, digit);
printf("Total occurrence of the digit is: %d in number: %d.", cnt, num);
14
}
Please pause the video and try to answer the question on your own!
First run:
Enter an integer number: 153
153 is an Armstrong number.
Second run:
Enter an integer number: 167
167 is not an Armstrong number.
16
/* C program to check whether a number is armstrong or not */
#include <stdio.h>
/*function to check Armstrong Number*/
int isArmstrong( int num ) {
int tempNumber = num; int rem; int sum = 0; /*sum of digit's cube*/
while( tempNumber != 0) {
rem = tempNumber % 10;
sum = sum + ( rem * rem * rem );
tempNumber /= 10;
}
if( sum == num) return 1; /*Armstrong Number*/
else return 0; /*Not an Armstrong Number*/
}
int main() {
int number;
printf("Enter an integer number: "); scanf("%d", &number);
if( isArmstrong( number ) )
printf("%d is an Armstrong number.", number);
17
else printf("%d is not an Armstrong number.", number);
return 0; }
Please pause the video and try to answer the question on your own!
This result of 12 sets is the calculation of nPr( 4, 2), the total number
of permutations of 4 objects taking 2 at a time. We can confirm that
the formula produces 12:
nP ( 4, 2) = 4! / ( 4 − 2 )! = ( 4 ∗ 3 ∗ 2 ∗ 1 ) / ( 2 ∗ 1 ) = 24 / 2 = 12
r
Please pause the video and try to answer the question on your own!
12.3 Recursion, Base Cases (and Recursion without a Base Case), & Recursive
Functions.
int main() {
int no = 5;
fun1( no );
return 0; }
24
12.7: What is the output of the following code
segment?
What will be the output of the program?
#include <stdio.h>
int fun1( int no ) {
if( no == 0 )
return 0;
else
printf("%d,", no );
fun1( no-- ); }
int main() {
int no = 5;
fun1( no );
return 0; }
Infinite loop. 5, 5, 5, 5, ……
25
Please pause the video and try to answer the question on your own!
int main() {
int no = 5;
fun1( no );
return 0; }
26
12.8: What is the output of the following code
segment?
What will be the output of the program?
#include <stdio.h>
int fun1( int no ) {
if( no == 0 )
return 0;
else
printf("%d,", no );
fun1( --no ); }
int main() {
int no = 5;
fun1( no );
return 0; }
Print 5, 4, 3, 2, 1.
27
Please pause the video and try to answer the question on your own!
int main()
{
int num, prime;
printf("Enter a positive number to check if it's a Prime Number: ");
scanf("%d", &num);
prime = isPrime( num, num / 2 );
if( prime == 1 )
{ printf("\n%d is a Prime Number.\n", num); }
else
{ printf("\n%d is a Composite Number.\n", num); }
return 0;
}
12.3 Recursion, Base Cases (and Recursion without a Base Case), & Recursive
Functions.
o Write a program in C to
convert a decimal number into
binary.
31
Please pause the video and try to answer the question on your own!
For Example:
32
#include <stdio.h>
void main()
{
int n, i, j, binno = 0, dn;
printf("\n\nConvert Decimal to Binary:\n ");
printf("-------------------------\n");
printf("Enter a number to convert : ");
scanf("%d", &n);
dn = n;
i = 1;
for( j = n; j > 0; j = j / 2)
{
binno = binno + ( n % 2 ) * i;
i = i * 10;
n = n / 2;
}
printf("\nThe Binary of %d is %d.\n\n", dn, binno);
} 33
Please pause the video and try to answer the question on your own!
o Write a program in
C to convert a
binary number into
a decimal number.
34
Please pause the video and try to answer the question on your own!
35
#include <stdio.h>
void main() {
int n1, n, p = 1;
int dec = 0, i = 1, j, d;
printf("\n\n Convert Binary to Decimal:\n ");
printf("-------------------------\n");
printf("Input a binary number :");
scanf("%d", &n);
n1 = n;
for ( j = n; j > 0; j = j / 10) {
d = j % 10;
if( i == 1 )
p = p * 1;
else
p = p * 2;
dec = dec + ( d * p );
i++;
}
printf("\nThe equivalent Decimal Number : %d \n\n", dec); 36
}
Please pause the video and try to answer the question on your own!
an Introduction to
Computer Science
Thank you!