0% found this document useful (0 votes)
13 views5 pages

CodeTest1 Answers

The document contains solutions to various programming problems including checking if a number is even or odd, summing two numbers, finding the maximum in an array, reversing a word, and removing duplicates from a string. Each problem is presented with logic and implementations in Python, Java, and C. The solutions provide a clear understanding of how to approach these common programming tasks.

Uploaded by

paxekig944
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

CodeTest1 Answers

The document contains solutions to various programming problems including checking if a number is even or odd, summing two numbers, finding the maximum in an array, reversing a word, and removing duplicates from a string. Each problem is presented with logic and implementations in Python, Java, and C. The solutions provide a clear understanding of how to approach these common programming tasks.

Uploaded by

paxekig944
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CodeTest1 Answers

Check if a Number is Even or Odd

Logic:
- Read an integer number.
- If the remainder when divided by 2 is 0, print "Even".
- Otherwise, print "Odd".
Python
n = int(input())
if n % 2 == 0:
print("Even")
else:
print("Odd")

Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if (n % 2 == 0) {
System.out.println("Even");
} else {
System.out.println("Odd");
}
}
}

C
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
if (n % 2 == 0)
printf("Even\n");
else
printf("Odd\n");
return 0;
}

Sum of Two Numbers

Logic:
- Read two integers.
- Add them and print the sum.
Python
a = int(input())
b = int(input())
sum = a + b
print(sum)

Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int sum = a + b;
System.out.println(sum);
}
}

C
#include <stdio.h>
int main() {
int a, b, sum;
scanf("%d %d", &a, &b);
sum = a + b;
printf("%d\n", sum);
return 0;
}

Maximum in an Array

Logic:
- Read `N`, the number of elements in the array.
- Read `N` numbers and store them in an array.
- Assume the first element is the largest.
- Compare each number in the array with the current maximum and update if a larger number is
found.
- Print the maximum number.
Python
n = int(input())
arr = []
for _ in range(n):
num = int(input())
arr.append(num)

max_num = arr[0]
for num in arr:
if num > max_num:
max_num = num

print(max_num)

Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];

for (int i = 0; i < n; i++) {


arr[i] = sc.nextInt();
}

int max_num = arr[0];


for (int i = 1; i < n; i++) {
if (arr[i] > max_num) {
max_num = arr[i];
}
}
System.out.println(max_num);
}
}

C
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int arr[n];

for (int i = 0; i < n; i++) {


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

int max_num = arr[0];


for (int i = 1; i < n; i++) {
if (arr[i] > max_num) {
max_num = arr[i];
}
}

printf("%d\n", max_num);
return 0;
}

Reverse a Word

Logic:
- Read a string character by character.
- Store characters in an array.
- Print characters from the last index to the first.
Python
s = input()
reversed_s = ""
for i in range(len(s) - 1, -1, -1):
reversed_s += s[i]
print(reversed_s)

Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
for (int i = s.length() - 1; i >= 0; i--) {
System.out.print(s.charAt(i));
}
System.out.println();
}
}

C
#include <stdio.h>
#include <string.h>
int main() {
char s[100];
scanf("%s", s);
int len = 0;
while (s[len] != '\0') {
len++;
}
for (int i = len - 1; i >= 0; i--) {
printf("%c", s[i]);
}
printf("\n");
return 0;
}

Remove Duplicates from a String

Logic:
- Read a string character by character.
- Store encountered characters in a tracking array.
- Append each character only if it is encountered for the first time.
Python
s = input()
seen = []
result = ""
for char in s:
if char not in seen:
result += char
seen.append(char)
print(result)

Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
boolean[] seen = new boolean[256];
String result = "";
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (!seen[c]) {
result += c;
seen[c] = true;
}
}
System.out.println(result);
}
}

C
#include <stdio.h>
#include <string.h>
int main() {
char s[100], result[100];
int seen[256] = {0}, index = 0;
scanf("%s", s);
for (int i = 0; s[i] != '\0'; i++) {
if (!seen[(int)s[i]]) {
result[index++] = s[i];
seen[(int)s[i]] = 1;
}
}
result[index] = '\0';
printf("%s\n", result);
return 0;
}

You might also like