You are on page 1of 10

FA19-BEE-040

HAFIZ MUHAMMAD ARSLAN MUNIR

ASSIGN
MENT
Introductio
n to
Problem 1: NO 1
Computer
Programmin Solution
g
Sir I have seeked help from different websites and online tutorials for the cited task in
order to meet all the requirements of the program, I ended with the underlying program.
I’ll be at your debt if you rectify me after assignment if it’s found beyond the required
conditions so that I may have better understanding in future. Thanks

Code for the


given Problem
/*FA19-BEE-040/LHR*/
/*HAFIZ MUHAMMAD ARSLAN MUNIR*/ /*TASK - 1*/
#include<stdio.h>
void expand(int);
int main()
{
int number;
double n;
int count = 1;
int digits;
while (count <=5){
printf("No. of digits you want to enter:");
FA19-BEE-040
HAFIZ MUHAMMAD ARSLAN MUNIR
scanf("%d", &digits);
if (digits > 4){
printf("Not allowed more than four digits\n");
}
else
break;
count++ ;
}
printf("Enter your desired Number to get it printed in Words\n");
scanf("%d" , &number);
expand(number);
}

void expand(int value)


{
const char * const ones[20] = {"ZERO", "ONE", "TWO",
"THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE","TEN",

"ELEVEN","TWELEVE","THIRRTEEN","FOURTEEN","FIFTEEN","SIXTEEN","SE
VENTEEN","EIGHTEEN","NINETEEN"};
const char * const tens[10] = {"", "TEN", "TWENTY",
"THIRTY","FOURTY","FIFTY","SIXTY","SEVENTY",
"EIGHTY","NINETY"};
if(value<0)
{
printf(" Minus ");
expand(-value);
}
else if(value>=1000)
FA19-BEE-040
HAFIZ MUHAMMAD ARSLAN MUNIR
{
expand(value/1000);
printf(" THOUSAND ");
if(value % 1000)
{
if(value % 1000 < 100)
{
printf(" AND ");
}
printf(" ");
expand(value % 1000);
}
}
else if(value >= 100)
{
expand(value / 100);
printf(" HUNDRED ");
if(value % 100)
{
printf(" AND ");
expand (value % 100);
}
}
else if(value >= 20)
{
printf(tens[value / 10]);
if(value % 10)
FA19-BEE-040
HAFIZ MUHAMMAD ARSLAN MUNIR
{
printf(" ");
expand(value % 10);
}
}
else
{
printf(ones[value]);
}
return;
}

Problem 2:

Solution
/*FA19-BEE-040/LHR*/
FA19-BEE-040
HAFIZ MUHAMMAD ARSLAN MUNIR
/*HAFIZ MUHAMMAD ARSLAN MUNIR*/ /*TASK - 2*/
#include<stdio.h>
int validateInput()
{
int number;
do{
printf("Enter numbers:\n");
scanf("%d",&number);

}
while(number < 0);
return number;
}
void showInstructions()
{

printf("Press 1 for multiplying numbers\n");


printf("Press 2 for finding nCr\n");
printf("Press 3 for finding nPr\n");
printf("Press -1 for Exit\n");

}
long factorial(int number)
{
long fact = 1;
int count = 1;
for(count=1;count<=number;count++){
FA19-BEE-040
HAFIZ MUHAMMAD ARSLAN MUNIR
fact = fact + 1;
}
return fact;
}
int multiply(int n, int r)
{
return n*r;
}
long comb(int n, int r)
{
return (factorial(n)/(factorial(r)*factorial(n-r)));
}
long perm(int n, int r)
{

return (factorial(n)/factorial(n-r));
}
main()
{

int n = validateInput();
int r = validateInput();
showInstructions();
int choice;
scanf("%d",&choice);
if (choice == -1){
printf("Exited!");
FA19-BEE-040
HAFIZ MUHAMMAD ARSLAN MUNIR
}
else if (choice == 1){
int mul;
mul = multiply(n,r);
printf("%d",mul);
}
else if (choice == 2){
long ncr = comb(n,r);
printf("%dC%d = %ld", n, r, ncr);
}
else if (choice == 3){
long npr = perm(n,r);
printf("%dC%d = %ld", n,r,npr);
}
}
FA19-BEE-040
HAFIZ MUHAMMAD ARSLAN MUNIR
Problem 3:

Solution
/*FA19-BEE-040/LHR*/
/*HAFIZ MUHAMMAD ARSLAN MUNIR*/ /*TASK - 3*/
#include<stdio.h>
int main()
{

int number;
int prime(int number);

int primefactor(int number);

printf("Input the number whose prime factors are required:");

scanf ("%d", &number);

primefactor(number);

}
prime(int num)
{
int counter, is_prime;
FA19-BEE-040
HAFIZ MUHAMMAD ARSLAN MUNIR
for (counter=2; counter<=num-1; counter++)

{
if (num%counter==0)
{

is_prime=0;
}
else
is_prime=1;

}
return (is_prime);
}
primefactor(int num)
{
int factor,is_prime;
for (factor=2; factor<=num;)

{
prime(factor);
if (is_prime)

{
if (num%factor==0)
{
FA19-BEE-040
HAFIZ MUHAMMAD ARSLAN MUNIR
printf("%d \n", factor);
num=num/factor;

continue;
}
else
{
factor++;

}
}
}
return 0;
}

You might also like