12.13 Lab9
intro-to-computer-science-gtiit-laboratory-9-2024.pdf
Introduction to Computer Science - Laboratory 9
-
Finish the exercises present in the tutorial.
-
Function for Exponentiation Write a function that takes two positive integer numbers as parameters (
x
andy
) and returns the result of \( x^y \). Write a program that tests the implementation of your function.#include<stdio.h> int expo(int x, int y) { int temp = x; for(int i = 2; i <= y; i++) { x *= temp; } return x; } int main() { int x, y; scanf("%d%d", &x, &y); printf("%d", expo(x, y)); return 0; }
-
Next Prime Function Design a function
int nextPrime(int N)
that, given an integer number \( N \), returns the first prime number greater than or equal to \( N \). Write a program that tests the implementation of your function.#include<stdio.h> int isPrime(int n) { if(n <= 1) return 0; else if(n == 2) return 1; else { for(int i = 2; i < n; i++) { if(n % i == 0) return 0; } return 1; } } int nextPrime(int N) { int nextp = N; while(isPrime(nextp) == 0) { nextp++; } return nextp; } int main() { int N; scanf("%d", &N); printf("%d", nextPrime(N)); return 0; }
-
Character Check Write a function that takes a character
c
as a parameter and returns 1 ifc
is a letter and 0 otherwise. -
Vowel Check Implement a function that takes a character
c
as a parameter and returns 1 ifc
is a vowel and 0 otherwise.#include<stdio.h> int isVowel(char c) { if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') return 1; else return 0; } int main() { char c; c = getchar(); printf("%d", isVowel(c)); return 0; }
-
Character Sequence Analysis Write a program that takes as input a character sequence and counts how many letters, vowels, and consonants are in the sequence. Reuse the functions previously defined.
#include<stdio.h> int count_letter(int seq[], int digit) { int count = 0; for(int i = 0; i < digit; i++) { if((seq[i] >= 'a' && seq[i] <= 'z') || (seq[i] >= 'A' && seq[i] <= 'Z')) count++; } return count; } int count_vowel(int seq[], int digit) { int count = 0; for(int i = 0; i < digit; i++) { if(seq[i] == 'a' || seq[i] == 'e' || seq[i] == 'i' || seq[i] == 'o' || seq[i] == 'u' || seq[i] == 'A' || seq[i] == 'E' || seq[i] == 'I' || seq[i] == 'O' || seq[i] == 'U') count++; } return count; } int count_consonant(int seq[], int digit) { return (count_letter(seq, digit) - count_vowel(seq, digit)); } int main() { int seq[8192]; int ch; int digit = 0; ch = getchar(); for(int i = 0; ch != '\n'; i++) { seq[i] = ch; ch = getchar(); digit++; } printf("Letters: %d, Vowels: %d, Consonants: %d", count_letter(seq, digit), count_vowel(seq, digit), count_consonant(seq, digit)); return 0; }
-
Greatest Common Divisor (GCD) Implement a function that, given two integer numbers
a
andb
, calculates the greatest common divisor (GCD) betweena
andb
. Write a program that tests the implementation of your function.#include <stdio.h> int gcd(int x, int y) { int gcd = 1, divisor = 2, max; // determine what the largest divisor should be if (x <= y) { max = x; } else { max = y; } while (divisor <= max) { if ((x % divisor) != 0 || (y % divisor) != 0) { divisor++; } else { gcd = divisor; divisor++; } } return gcd; } int main() { int x, y; scanf("%d%d", &x, &y); printf("%d", gcd(x, y)); }
-
Least Common Multiple (LCM) Define a function that, given two integer numbers
a
andb
, calculates the least common multiple (LCM) betweena
andb
. Write a program that tests the implementation of your function.#include <stdio.h> int gcd(int x, int y) { int gcd = 1, divisor = 2, max; // determine what the largest divisor should be if (x <= y) { max = x; } else { max = y; } while (divisor <= max) { if ((x % divisor) != 0 || (y % divisor) != 0) { divisor++; } else { gcd = divisor; divisor++; } } return gcd; } int lcm(int x, int y) { return x * y / gcd(x, y); } int main() { int x, y; scanf("%d%d", &x, &y); printf("%d", lcm(x, y)); }
#include <stdio.h> int lcm(int x, int y) { int max = x * y; int lcm; for(int i = max; i <= max && (i >= x || i >= y); i--) { if(i % x == 0 && i % y ==0) lcm = i; } return lcm; } int main() { int x, y; scanf("%d%d", &x, &y); printf("%d", lcm(x, y)); }
-
Sum of Digits Write a function that, given an integer number \( N \), returns the sum of \( N \)'s digits. Write a program that tests the implementation of your function.
#include <stdio.h> int readseq(int seq[]) { int current_digit = getchar(); int i = 0; while(current_digit != '\n') { seq[i] = current_digit - '0'; //store the real number instead of character number current_digit = getchar(); i++; } return i; } int sum_of_digits(int seq[]) { int sum = 0; int digit = readseq(seq); for(int i = 0; i < digit; i++) { // donot put i < readseq(seq), this will repeatedly input sum += seq[i]; } return sum; } int main() { int seq[256]; printf("%d", sum_of_digits(seq)); }
#include <stdio.h> int sum_of_digits(int N) { int sum = 0; while(N != 0) { sum += N % 10; N /= 10; } return sum; } int main() { int N; scanf("%d", &N); printf("%d", sum_of_digits(N)); }
-
Palindrome Check Write a function that takes an integer number \( N \) as a parameter and checks if \( N \) is a palindrome. Write a program that tests the implementation of your function.
#include <stdio.h> int read(int N, int seq1[]) { int i = 0; while(N != 0) { seq1[i] = N % 10; N /= 10; i++; } return i; } int isPa(int N) { int seq[64]; int digit = read(N, seq); for(int i = 0; i < digit; i++) { if(seq[i] != seq[digit - i - 1]) return 0; } return 1; } int main() { int N; scanf("%d", &N); printf("%d", isPa(N)); }
-
Sum of Even Numbers Write a function
int sumEven(int numberArray[], int numberArraySize)
that returns the sum of all the even numbers innumberArray
. Write a program that tests the implementation of your function.See (9) method 1, they are very similar
-
Increment Function Define a function that, given a positive integer \( N \), returns \( N + 1 \).
Are you kidding? Not necessary to do it.
-
Addition without
+
Operator Implement a function that, given two positive integer numbersa
andb
, returns \( a + b \). This function must not use the C add operator (+
).