Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

NAME :BESSALA LAURENE JUSTINE

MATRICULE :ET20200473
COMPUTER ASSIGNEMENT
1.What is the difference between Time complexity and space complexity
Time complexity of an algorithm quantifies the amount of time taken by an algorithm to run
as a function of the length of the input. Similarly, Space complexity of an algorithm
quantifies the amount of space or memory taken by an algorithm to run as a function of the
length of the input .

2. The best-case performance is used in computer science to describe an algorithm's behavior


under optimal conditions. For example, the best case for a simple linear search on a list occurs
when the desired element is the first element of the list.
The Average-case performance is used in computer science to describe an algorithm's behavior
under average conditions. For example, the average case for a simple linear search on a list
occurs when the desired element is the middle element of the list.

The worst-case performance is used in computer science to describe an algorithm's behavior


under the worst conditions. For example, the worst case for a simple linear search on a list
occurs when the desired element is the last element of the list or not even found in the list .
3.An Algorithims should be ;

 .Precise
 .Definiteness
 .Effectiveness
 . Finiteness
 .Independent

4: Pseudo-code  #include<stdio.h>
input Number1,Number2,Number3 :Real  void main()
 {
Begin
 int a[5],i,j,c;
if(Number1 < Number2) then
 clrscr();
if(Number1 < Number3) then  printf("Enter five no. :");
display "Num1 is the smallest"  for(i=0;i<=4;i++)
else  {
display "Num3 is the smallest" o scanf("%d",&a[i]);
 }
end-if
o for(i=0;i<=4;i++)
else if(Number2 < Number3) then o {
display "Num2 is the smallest"  for(j=i+1;j<=4;j++)
else  {
display "Num3 is the smallest"  if(a[i]>a[j])
 {
end-if
 c=a[i];
End .  a[i]=a[j];
 a[j]=c;
 }  }
 }  printf("\n");
o }  getch();
 for(i=0;i<=4;i++)  }
 {
 printf("%d",a[i]);

 5. Algorithm o // it does not support dynamic


memory allocation.
 countingSort(array, size)
o // So, its size is provided
 max <- find largest element in array statically.
 initialize count array with all zeros o int count[10];
 for j <- 0 to size
 find the total count of each unique
o // Initialize count array with all
element and zeros.
 store the count at jth index in count o for (int i = 0; i <= max; ++i) {
array o count[i] = 0;
o }
 for i <- 1 to max
 find the cumulative sum and store it in
count array itself o // Store the count of each
 for j <- size down to 1 element
 restore the elements to array o for (int i = 0; i < size; i++) {
o count[array[i]]++;
 decrease count of each element o }
restored by 1

o // Store the cummulative count


C PROGRAM of each array
o for (int i = 1; i <= max; i++) {
o // Counting sort in C o count[i] += count[i - 1];
programming o }

 #include <stdio.h> o // Find the index of each


element of the original array in
count array, and
o // place the elements in output
 void countingSort(int array[], int size) { array
o int output[10]; o for (int i = size - 1; i >= 0; i--) {
o output[count[array[i]] - 1] =
array[i];
o // Find the largest element of o count[array[i]]--;
the array o }
o int max = array[0];
o for (int i = 1; i < size; i++) {
o if (array[i] > max) // Copy the sorted elements into
o max = array[i]; original array
o }
for (int i = 0; i < size; i++) {
array[i] = output[i];
o // The size of count must be at
least (max+1) but }
o // we cannot declare it as int
count(max+1) in C as }
// Function to print an array // Driver code
void printArray(int array[], int size) { int main() {
for (int i = 0; i < size; ++i) { int array[] = {4, 2, 2, 8, 3, 3, 1};
printf("%d ", array[i]); int n = sizeof(array) / sizeof(array[0]);
} countingSort(array, n);
printf("\n"); printArray(array, n);
} }

6 . #include <stdio.h>

int main() {

int n, i, flag = 0;

printf("Enter a positive integer: ");

scanf("%d", &n);

for (i = 2; i <= n / 2; ++i) {

// condition for non-prime

if (n % i == 0) {

flag = 1;

break;

if (n == 1) {

printf("1 is neither prime nor composite.");

else {

if (flag == 0)

printf("%d is a prime number.", n);

else

printf("%d is not a prime number.", n);

}
return 0;

7. #include <stdio.h>

int main() {

int num;

printf("Enter an integer: ");

scanf("%d", &num);

(num % 2 == 0) ? printf("%d is even.", num) : printf("%d is odd.", num);

return 0;

8. ALGORITHM

Step 1. Start

Step 2. Read the number to be checked from the user

Step 3. If number >= 0:

3.1 Repeat for i=0 to number:

3.1.1: If number = (i*i):

3.1.2: Display “Perfect Square”

3.1.3: Stop

3.1.4: i = i + 1

3.2 Display “Not a Perfect Square”

Step 4. Else:

4.1: Display “Not a Perfect Square”

Step 5. Stop

C PROGRAM

#include <stdio.h>

#include <math.h>
/*function definition*/

int isPerfectSquare(int number)

int iVar;

float fVar;

fVar=sqrt((double)number);

iVar=fVar;

if(iVar==fVar)

return 1;

else

return 0;

int main()

int num;

printf("Enter an integer number: ");

scanf("%d",&num);

if(isPerfectSquare(num))

printf("%d is a perfect square.",num);

else

printf("%d is not a perfect square.",num);

return 0;

9. #include<stdio.h>

int main()

{
int num;

printf("Enter number: ");

scanf("%d",&num);

printf("Factors of %d are:\n", num);

for(int i=1; i<=num/2; i++)

if(num%i==0)

printf("%d\t", i);

return 0;

10. #include <stdio.h>

int main()

int i, Number;

printf("\n Please Enter any number to Find Factors \n");

scanf("%d", &Number);

printf("\n Factors of the Given Number are:\n");

for (i = 1; i <= Number; i++)

if(Number%i == 0)

printf(" %d ", i);

}
}

return 0;

11. #include<stdio.h>

#include<conio.h>

void main()

int num; //variable declaration

printf("\nEnter any number:"); //asking your to enter a number

scanf("%d",&num); //reading entered value from user

if(num< 0) //condition to check absolute value

num = num*-1; //converting to absolute value

printf("\nThe absolute value of given number is: %d", num);

printf("\n press any key to exit.");

getch();

14. Step 1: Start

Step 2: Declare Variable n, fact, i

Step 3: Read number from User

Step 4: Initialize Variable fact=1 and i=1

Step 5: Repeat Until i<=number

5.1 fact=fact*i

5.2 i=i+1

Step 6: Print fact

Step 7: Stop
#include <stdio.h>

int main() {

int n, i;

unsigned long long fact = 1;

printf("Enter an integer: ");

scanf("%d", &n);

// shows error if the user enters a negative integer

if (n < 0)

printf("Error! Factorial of a negative number doesn't exist.");

else {

for (i = 1; i <= n; ++i) {

fact *= i;

printf("Factorial of %d = %llu", n, fact);

return 0;

15. #include <stdio.h>

int main() {

int n, i;

printf("Enter an integer: ");

scanf("%d", &n);

for (i = 1; i <= 10; ++i) {

printf("%d * %d = %d \n", n, i, n * i);

return 0;

16. #include <stdio.h>

int main() {
int i;

n=4

for (i = 1; i <= 10; ++i) {

printf("%d * %d = %d \n", n, i, n * i);

return 0;

17. Get the number

Declare a variable to store the sum and set it to 0

Repeat the next two steps till the number is not 0

Get the rightmost digit of the number with help of the remainder ‘%’ operator by dividing it by 10
and add it to sum.

Divide the number by 10 with help of ‘/’ operator to remove the rightmost digit.

Print or retu / C program to compute sum of digits in

// number.

#include <stdio.h>

/* Function to get sum of digits */

int getSum(int n)

int sum = 0;

while (n != 0) {

sum = sum + n % 10;

n = n / 10;

return sum;

// Driver code

int main()

{
int n = 687;

printf(" %d ", getSum(n));

return 0;

}rn the sum

21. #include<stdio.h>

void main()

int a[5],i,j,c;

clrscr();

printf("Enter five no. :");

for(i=0;i<=4;i++)

scanf("%d",&a[i]);

for(i=0;i<=4;i++)

for(j=i+1;j<=4;j++)

if(a[i]>a[j])

c=a[i];

a[i]=a[j];

a[j]=c;

}
for(i=0;i<=4;i++)

printf("%d",a[i]);

} printf("\n"); getch();

22. Algorithm

 Compare x with the middle element.


 If x matches with the middle element, we return the mid index.
 Else If x is greater than the mid element, then x can only lie in the right half subarray after the mid
element. So we recur for the right half.
 Else (x is smaller) recur for the left half.
// C program to implement recursive Binary Search

#include <stdio.h>

// A recursive binary search function. It returns

// location of x in given array arr[l..r] is present,

// otherwise -1

int binarySearch(int arr[], int l, int r, int x)

if (r >= l) {

int mid = l + (r - l) / 2;

// If the element is present at the middle

// itself

if (arr[mid] == x)

return mid;

// If element is smaller than mid, then

// it can only be present in left subarray

if (arr[mid] > x)

return binarySearch(arr, l, mid - 1, x);

// Else the element can only be present

// in right subarray
return binarySearch(arr, mid + 1, r, x);

// We reach here when element is not

// present in array

return -1;

int main(void)

int arr[] = { 2, 3, 4, 10, 40 };

int n = sizeof(arr) / sizeof(arr[0]);

int x = 10;

int result = binarySearch(arr, 0, n - 1, x);

(result == -1) ? printf("Element is not present in array")

: printf("Element is present at index %d",

result);

return 0;

You might also like