Menu

Wednesday, 13 May 2015

Chapter 5,Problem d: Let Us C by Yashawant Kanetkar Solution Manual

Problem Statement:

A positive integer is entered through the keyboard, write a
function to find the binary equivalent of this number using
recursion.

Solution:

//Ahmad Furqan
//P5.d
#include <iostream>
#include <conio.h>
using namespace std;
int num2bin_rec(int num)
{
//if number is less than 2 return it
if (num < 2)
return num;
//show num/2's binary equivalent
cout<<num2bin_rec(num/2);
//return 1st binary digit of number
return num % 2;
}
void main(void)
{
int num;
cout << "Enter a positive number: ";
cin >> num;
cout << num2bin_rec(num);
_getch();
}

Chapter 5,Problem c: Let Us C by Yashawant Kanetkar Solution Manual

Problem Statement:

Write a recursive function to obtain the first 25 numbers of a
Fibonacci sequence. In a Fibonacci sequence the sum of two
successive terms gives the third term. Following are the first
few terms of the Fibonacci sequence:
1 1 2 3 5 8 13 21 34 55 89...

Solution:

//Ahmad Furqan
//P5.c
#include <iostream>
#include <conio.h>
using namespace std;
int fabb[25],index=0;
void fabb_rec(int a, int b)
{
if (index == 25)
return;
fabb[index] = a + b; //add previous terms
//update last two terms
a = b;
b = fabb[index];
index++;
return fabb_rec(a, b);
}
void main(void)
{
cout << "First 25 numbers of a Fibnocci sequence are: " << endl;
index = 2;
fabb[0] = 1;
fabb[1] = 1;
fabb_rec(1, 1);
for (int i = 0; i < 25; i++)
cout << " " << fabb[i];
_getch();
}

Chapter 5,Problem b: Let Us C by Yashawant Kanetkar Solution Manual

Problem Statement:

A positive integer is entered through the keyboard, write a
program to obtain the prime factors of the number. Modify the
function suitably to obtain the prime factors recursively.

Solution:

//Ahmad Furqan
//P5.b
#include <iostream>
#include <conio.h>
using namespace std;
bool is_prime(int num)
{
if (num == 2 || num == 3)
return true;
if (num < 2 || num%2==0)  //If number is less than 2 or even its not prime
return false;
//starting from 3 we try dividing by odd numbers,
//since its not even its not divisible by any even number
//we need to go upto num/2
for (int i = 3; i < num / 2; i += 2)
{
if (num%i == 0)
return false;
}
return true;
}
void show_prime_fact_rec(int num)
{
if (is_prime(num)) //if num is prime show it and return since it don't have any factors
{
cout << num << endl;
return;
}
//find smallest prime factor of the number and show it
int i;
for (i = 2; i < num / 2; i++)
{
if (num%i == 0)
{
if (is_prime(i))
break;
}
}
cout << i << endl;
//call function with num divided with lowest prime factor to find other factors
return show_prime_fact_rec(num/i);
}
void main(void)
{
int num;
cout << "Enter a number: ";
cin >> num;
cout << "Prime factors of the number are:" << endl;
show_prime_fact_rec(num);
_getch();
}

Chapter 5,Problem a: Let Us C by Yashawant Kanetkar solution manual

Problem Statement:

A 5-digit positive integer is entered through the keyboard,
write a function to calculate sum of digits of the 5-digit
number:
(1) Without using recursion
(2) Using recursion

Solution:


//Ahmad Furqan
//P5.a
#include <iostream>
#include <conio.h>
using namespace std;
int dig_sum(int num)
{
int sum = 0;
for (int i = 0; i < 5; i++) //5 iterations for each digit
{
sum += num % 10; //remainder of number divided with 10 is the rightmost digit
num = num / 10; //dividing number with 10 eliminates reightmost digit.
}
return sum;
}
int dig_sum_rec(int num)
{
if (num <10)
return num;
//return sum of rightmost digit and sum of digits of number without rightmost digit
return dig_sum_rec(num / 10) + (num % 10);
}
void main(void)
{
int num;
cout << "Enter 5 digit number:";
cin >> num;
cout << "Sum of digits without recursion: " << dig_sum(num);
cout << "\nSum of digits with recursion: " << dig_sum_rec(num);
_getch();
}

Monday, 11 May 2015

Chapter 1, Problem c: Let Us C by Yashawant Kanetkar Solution Manual

Problem Statement:

If the marks obtained by a student in five different subjects
are input through the keyboard, find out the aggregate marks
and percentage marks obtained by the student. Assume that
the maximum marks that can be obtained by a student in each
subject is 100.

Solution:


#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
float s1,s2,s3,s4,s5,tmark,per;
char a;
cout<< "Enter Students marks in five different sunjects separated with semicolon(:)\n";
cin >>s1>>a>>s2>>a>>s3>>a>>s4>>a>>s5;
tmark=s1+s2+s3+s4+s5;
per=tmark/500*100;
cout<<"\nAggregate marks="<<tmark
<<"\npersentage marks="<<per<<'\n';
getch();

}

Chapter 1, Problem b: Lect Us C By Yashawant Kanetkar Solution Manual

Problem Statement:

The distance between two cities (in km.) is input through the
keyboard. Write a program to convert and print this distance
in meters, feet, inches and centimeters.

Solution:

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int km,m,cm;
float inch,foot;
cout<<"Enter Distance between two cities in Kilo Meters:\n";
cin>>km;
m=km*1000;
cm=m*100;
foot=3.28084*m;
inch=12*foot;
cout<<"The Distance is:\n"
<<m<<" meters\n"
<<cm<<" centi meters\n"
<<foot<<" Feet\n"
<<inch<<" inches\n";
getch();
}

Chapter 1, Problem a, Let Us C by Yashawant Kanetkar Solution Manual

Problem Statement:

Ramesh’s basic salary is input through the keyboard. His
dearness allowance is 40% of basic salary, and house rent
allowance is 20% of basic salary. Write a program to calculate
his gross salary.


Solution:


#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int basic_sal,gross_sal;
cout<<"Enter Rimshah's Basic salary:\n";
cin>>basic_sal;
int dear_all=40*basic_sal/100;
int rent_all=20*basic_sal/100;
gross_sal=basic_sal+dear_all+rent_all;
cout<<"Rimshah's Gross Salary is "<<gross_sal<<'\n';
getch();
}