0% found this document useful (0 votes)
3 views

201SampleProgramsFall2022

The document outlines a series of programming assignments for a CSC 201 course, detailing sample programs that demonstrate various algorithms and mathematical concepts. Each program includes a brief description of its purpose, such as calculating Lucas numbers, Fibonacci numbers, and checking for Automorphic numbers. The document serves as a guide for students to understand and implement these concepts in their programming assignments.

Uploaded by

varuncv2001
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

201SampleProgramsFall2022

The document outlines a series of programming assignments for a CSC 201 course, detailing sample programs that demonstrate various algorithms and mathematical concepts. Each program includes a brief description of its purpose, such as calculating Lucas numbers, Fibonacci numbers, and checking for Automorphic numbers. The document serves as a guide for students to understand and implement these concepts in their programming assignments.

Uploaded by

varuncv2001
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

CSC 201, Fall 2022

Project1 Sample Program Assignment

Section # Team # Sample Program #


1 1 1
1 2 2
1 3 3
1 4 4
1 5 5
1 6 6
2 1 7
2 2 8
2 3 9
2 4 10
2 5 11
3 1 12
3 2 13
3 3 14
3 4 15
3 5 16
/*
Sample 1.
The Lucas number is defined to be the sum of its two immediate previous terms:
L(0) = 2
L(1) = 1
L(n) = L(n-1)+L(n-2) n>1
In this sample program, define n = 15, and the final answer should be 1364.
*/

PROGRAM lucas
{

n Integer;
cur Integer;
prev1 Integer;
prev2 Integer;
i Integer;
answer Integer;

n = 15;

IF (n Eq 0) THEN answer = 2
ELSE
{
i = 1;
IF (n Eq 1) THEN answer = 1
ELSE
{
prev2 = 2;
prev1 = 1;
WHILE (i Lt n)
{
i = i Plus 1;
cur = prev1 Plus prev2;
prev2 = prev1;
prev1 = cur;
}
answer = cur;
}
}

}
/*
Sample 2.
The program is used to determine whether a number is a Fibonacci number.
A Fibonacci number is defined as follow:
Fib(n) = 0 n=0
Fib(n) = 1 n=1
Fib(n) = Fib(n-1) + Fib(n-2) n>1
In this sample program, number = 233, the answer should be True.
*/

PROGRAM isFib
{
number Integer;
prev1 Integer;
prev2 Integer;
cur Integer;
answer Boolean;

number = 233;

IF (number Eq 0) THEN answer = True;


ELSE
{
prev1 = 0;
prev2 = 1;
cur = 1;

WHILE (cur Lt number)


{
cur = prev1 + prev2;
prev1 = prev2;
prev2 = cur;
}

IF (cur Eq number) THEN answer = True;


ELSE answer = False;
}
}
/*
Sample 3.
This program checks if the given number is Automorphic. A number is said to be Automorphic
or Cyclic number if and only if its square ends in the same digits as the number itself.
For example, number 5 is automorphic as its square ends with digit 5 i.e. 52 = 25
Some of the other examples are
62 = 36
762 = 5776
3762 = 141376 etc.
*/

PROGRAM isautomorphic
{
number Integer;
sqno Integer;
automorphic Boolean;

number = 76;
automorphic = True;

sqno = number Times number;

WHILE (number Gt 0)
{
IF ((number Minus 10 Times (number Div 10)) Ne (sqno Minus 10 Times (sqno Div 10)))
THEN
{
automorphic = False;
number = 0;
}

number = number Div 10;


sqno = sqno Div 10;

}
}
/*
Sample 4.
This program calculates generic root of a given positive number. Generic root of a number is sum
of all digits in a given number until a single digit output (less than 10) is obtained.
For example, Generic root of 98765 = 9 + 8 + 7 + 6 + 5 = 35 = 8
*/

PROGRAM generic_number
{
num Integer;
sum Integer;
reminder Integer;
result Integer;

num = 98765;

WHILE (num Ge 10)


{
sum = 0;
WHILE(num Gt 0)
{
reminder = (num Minus 10 Times (num Div 10));
sum = sum Plus reminder;
num = num Div 10;
}

IF (sum Ge 10)
THEN
{
num = sum;
}

ELSE result = sum;


}
}
/*
Sample 5
In number theory, the aliquot sum s(n) of a positive integer n is the sum of all proper divisors of n,
that is, all divisors of n other than n itself.
For Example :
Input : 12 Input : 15
Output : 16 Output : 9
Explanation : Explanation :
Proper divisors of 12 is = 1, 2, 3, 4, 6 Proper divisors of 15 is 1, 3, 5
and sum 1 + 2 + 3 + 4 + 6 = 16 and sum 1 + 3 + 5 = 9
*/

PROGRAM aliquotSum
{
n Integer;
sum Integer;
i Integer;

n = 12;
sum = 0;

IF (n Le 0) THEN sum = 0;
ELSE
{
i = 1;
WHILE (i Lt n)
{
IF ((n Minus i Times (n Div i)) Eq 0) THEN sum = sum Plus i;
i = i Plus 1;
}
}

}
/*
Sample 6.
This program is used to calculate the sum of odd digits in a number n≥0.
For example in this sample program, n = 3765395 the sum of the odd digits would be 32.
*/

PROGRAM sumOddDigits
{
n Integer;
result Integer;
digit Integer;
temp Integer;

n = 3765395;
temp = 0;

IF (n Eq 0) THEN result = 0;
ELSE
{
While(n Ne 0)
{
temp = n;
n = n Div 10;
digit = temp Minus n Times 10;
IF (digit Div 2 Eq 1) THEN sum = sum Plus digit;
}
}
/*
Sample 7.
This program Calculate the numbers of 3, multiples of 3 and number include 3 within n (n>0).
For example:
n = 10, have 3, 6, 9, count = 3
n = 20, have 3, 6, 9, 12, 13, 15, 18, count = 7
n = 35, have 3, 6, 9, 12, 13, 15, 18, 21, 23, 24, 27, 30, 33, count = 13
*/

PROGRAM Include Three


{
n Integer;
count Integer;
num Integer;
i Integer;

n = 35;
count = 0;
i = 1;

WHILE (i Le n)
{ IF (i Minus 3 Times (i Div 3) Eq 0)
THEN
{ count = count Plus 1;
}
ELSE
{ num = i;
WHILE (num Div 10 Gt 0)
{ IF ((num Minus 10 Times (num Div 10)) Eq 3)
THEN
{ count = count Plus 1;
}
num = num Div 10;
}
}
i = i Plus 1;
}

}
/*
Sample 8.
A Pronic number is a number which can be represented as the product of two consecutive integers.
For example, 6 is a Pronic number as 6 = 2 * 3. 56 is also a Pronic number as 56 = 7 * 8.
The program is to determine whether a given number is a Pronic number.
In this sample program, define num = 210, and the answer should be True.
*/

PROGRAM isPronic
{

number Integer;
i Integer;
product Integer;
answer Boolean;

number = 210;

IF (number Le 0) THEN answer = False;


ELSE
{
i = 1;
product = i Times (i Plus 1);

WHILE (product Lt number)


{
i = i Plus 1;
product = i Times (i Plus 1);
}

IF (product Eq number) THEN answer = True;


ELSE answer = False;
}

}
/*
Sample 9.
The program is used to calculate the sum of all digits of a number.
If the given number is negative, only consider its absolute value.
In this sample, define number = 95819, the sum should be 32.
*/

PROGRAM sumDigit
{
number Integer;
digit Integer;
sum Integer;
temp Integer;

number = 95819;

IF (number Eq 0) THEN sum = 0;


ELSE
{
IF (number Lt 0) THEN number = 0 Minus number;

sum = 0;

WHILE (number Ne 0)
{
temp = number;
number = number Div 10;
digit = temp Minus number Times 10;
sum = sum Plus digit;
}
}
}
/*
Sample 10.
The program is to find the largest integer that is less than or equal to the square root of a given
number. The number must be non-negative.
For example, number = 16, the answer is 4; number = 35, the answer is 5.
In this sample program, number = 255, and the answer should be 15.
*/

PROGRAM sqrt_int
{

number Integer;
cur Integer;
cur_square Integer;
answer Integer;

number = 255;

IF (number Lt 0) THEN answer = -1; // indicate invalid input


ELSE
{
cur = 0;
cur_square = 0;

WHILE (cur_square Lt number)


{
cur = cur Plus 1;
cur_square = cur Times cur;
}

IF (cur_square Eq number) THEN answer = cur;


ELSE answer = cur Minus 1;
}

}
/*
Sample 11.
A Neon number is an integer where the digits of square of the number sums up to the number itself.
For example, 9 is a Neon number because 92 = 81, 8 + 1 = 9.
The program is to determine whether a given number is a Neon number.
In this sample program, define num = 12, and the answer should be False.
*/

PROGRAM isNeonNum
{
num Integer;
square Integer;
temp Integer;
digit Integer;
sum Integer;
answer Boolean;

num = 12;

IF (num Lt 0) THEN answer = False;


ELSE
{
square = num Times num;
sum = 0;

WHILE (square Ne 0)
{
temp = square;
square = square Div 10;
digit = temp Minus square Times 10;
sum = sum Plus digit;
}

IF (sum Eq num) THEN answer = True;


ELSE answer = False;
}

}
/*
Sample 12.
A k-rough or k-jagged number is a number whose smallest prime factor is greater than or equal to
the number ‘k’.
Given numbers ‘n’ and ‘k’ as input, we are required to find whether ‘n; is a k-rough number or
not. Note that in this program n, k > 0;
For example -
1. Input : n = 10, k = 2 Output : true
Explanation: The prime factors of 10 are 2 and 5 Hence its smallest prime factor is 2 which
is greater than or equal to k, i.e. 2
2. Input : n = 55, k = 7 Output : false
Explanation: The prime factors of 55 are 5 and 11 Hence its smallest prime factor is 5 which
is not greater than or equal to k, i.e. 7
*/

PROGRAM isRoughNumber
{
n Integer;
k Integer;
ans Boolean;
i Integer;

n = 75;
k = 3;
ans = false;

IF (n Minus 2 Times (n Div 2) Eq 0)


THEN
{
IF (k Le 2) THEN ans = true;
}
ELSE
{
i = 3;
WHILE (i Times i Le n)
{
IF (n Minus i Times (n Div i) Eq 0)
THEN
{
IF (i Ge k)
THEN
{
ans = true;
i = i Times i Plus n;
}
}
i = i Plus 2;
}
}
/*
Sample 13.
Given a number, find minimum sum of its factors.
To find minimum sum of the factors of a number, we find sum of its prime factors.
For example -
1. Input : 12 Output : 7
Explanation: Following are different ways to factorize 12 and sum of factors in different
ways.
12 = 12 * 1 = 12 + 1 = 13
12 = 2 * 6 = 2 + 6 = 8
12 = 3 * 4 = 3 + 4 = 7
12 = 2 * 2 * 3 = 2 + 2 + 3 = 7
Therefore, minimum sum is 7
2. Input : 105 Output : 15
*/

PROGRAM minSumOfFactors
{
num Integer;
sum Integer;
i Integer;

num = 12;
sum = 0;
i = 2;

IF (num Gt 0)
THEN
{
WHILE (i Times i Le num)
{
WHILE ((num Minus i Times (num Div i)) Eq 0)
{
sum = sum Plus i;
num = num Div i;
}
i = i Plus 1;
}
sum = sum Plus num;
}
ELSE
{
sum = 0;
}
}
/*
Sample 14.
Given an integer value n, find out the n-th positive integer whose sum is 10.
For example -
1. Input : n = 2 Output : 27
The first number with sum of digits as
10 is 19. Second number is 27.
2. Input : 15 Output : 154
*/

PROGRAM nNumberWhoseSumOfDidgitsIsTen
{
count Integer;
curr Integer;
n Integer;
result Integer;
sum Integer;
x Integer;

count = 0;
curr = 1;
n = 5;
result =0;

WHILE (count Le n)
{
sum = 0;
x = curr;
WHILE (x Gt 0)
{
sum = sum Plus (x Minus 10 Times (x Div 10));
x = x Div 10;
}

IF (sum Eq 10) THEN count= count Plus 1;

IF (count Eq n)
THEN
{
result = curr;
count = n Plus 1;
}
curr = curr Plus 1;
}
}
/*
Sample 15.
Given an integer n. Find politeness of number n. Politeness of a number is defined as the number of
ways it can be expressed as the sum of consecutive integers.
For example –
Input: n = 15 Output: 3
Explanation: There are only three ways to express
15 as sum of consecutive integers i.e.,
15 = 1 + 2 + 3 + 4 + 5
15 = 4 + 5 + 6
15 = 7 + 8
Hence answer is 3
*/

PROGRAM politenessOfN
{
finalAnswer Integer;
result Integer;
n Integer;
i Integer;
divCount Integer;

result = 1;
n = 90;
i = 3;

WHILE ((n Minus 2 Times (n Div 2)) Eq 0)


{
n = n Div 2;
}

WHILE (i Times i Le n)
{
divCount = 0;
WHILE ((n Minus i Times (n Div i)) Eq 0)
{
n = n Div i;
divCount = divCount Plus 1;
}
result = result Times (divCount Plus 1);
i = i Plus 2;
}
IF (n Gt 2) THEN result = result Times 2;
finalAnswer = result Minus 1;
}
/*
Sample 16.
Perfect number is a positive number which sum of all its positive divisors, including 1 but excluding
the number itself, is equal to that number.
The program tells if the selected number ‘num’ is a perfect number or not. In this sample
program, define num = 496, and it is a perfect number.
*/

PROGRAM perfect_number
{
num Integer;
perfect Boolean;
k Integer;
sum Integer;
cond Integer;
remainder Integer;

num = 496;
sum = 1;
k = 2;
cond = num Div 2;

IF (num Lt 2) THEN perfect = False;


ELSE
{
WHILE (k Le cond)
{
remainder = num Minus (num Div k Times k);

IF (remainder Eq 0) THEN sum = sum Plus k;

k = k Plus 1;
}

IF (num Eq sum) THEN perfect = True


ELSE perfect = False;
}
}
/*
Sample 17.
The program is used to calculate the sum of 00 + 11 + 22 + 33 + 44 + …+ nn.
Here we define 00 = 1.
In this sample, define n = 5, and the sum should be 3414.
*/

PROGRAM sum
{
sum Integer;
n Integer;
i Integer;
j Integer;
current Integer;
result Integer;

n = 6;

IF (n Eq 0) THEN sum = 1
ELSE
{
i = 1;
sum = 1;
WHILE ( i Le n )
{
j = 1;
current = i;
WHILE ( j Lt i )
{
current = current Times i;
j = j Plus 1;
}
sum = sum Plus current;
i = i Plus 1;
}
}
}

You might also like