Week 0.1 - Assignments
Week 0.1 - Assignments
1. Accept a char input from the user and display it on the console.
#include <stdio.h>
int main() {
char a;
return 0;
}
2. Accept two inputs from the user and output its sum.
Number 1 Integer
Number 2 Float
Sum Float
#include <stdio.h>
int main() {
int number1;
float number2;
float sum;
a. The program should accept 3 inputs from the user and calculate simple interest
for the given inputs. Formula: SI=(P*R*n)/100)
#include <stdio.h>
int main() {
int principalAmount;
float interestRate, numberOfYears, simpleInterest;
4. Write a program to check whether a student has passed or failed in a subject after
he or she enters their mark (pass mark for a subject is 50 out of 100).
a. The program should accept input from the user and output a message as
“Passed” or “Failed.”
mark float
#include <stdio.h>
int main() {
float mark;
printf("Enter the student's mark (out of 100): ");
scanf("%f", &mark);
if (mark >= 50.0) {
printf("Passed\n");
} else {
printf("Failed\n");
}
return 0;
}
5. Write a program to show the grade obtained by a student after they enter their total
mark percentage.
a. The program should accept input from the user and display their grade as
follows
Mark Grade
> 90 A
80-89 B
70-79 C
60-69 D
50-59 E
< 50 Failed
Variable Data type
#include <stdio.h>
int main() {
float totalMark;
char grade;
return 0;
}
6. Using the ‘switch case,’ write a program to accept an input number from the user
and output the day as follows.
Input Output
1 Sunday
2 Monday
3 Tuesday
4 Wednesday
5 Thursday
6 Friday
7 Saturday
E.g.:
Input: 5
Output:
1x5=5
2 x 5 = 10
3 x 5 = 15
4 x 5 = 20
5 x 5 = 25
6 x 5 = 30
7 x 5 = 35
8 x 5 = 40
9 x 5 = 45
10 x 5 = 50
#include <stdio.h>
int main() {
int num;
return 0;
}
8. Write a program to find the sum of all the odd numbers for a given limit
a. Program should accept an input as limit from the user and display the sum
of all the odd numbers within that limit
Input: 10
#include <stdio.h>
int main() {
int limit;
int sum = 0;
printf("Enter a limit: ");
scanf("%d", &limit);
for (int i = 1; i <= limit; i++) {
if (i % 2 != 0) {
sum += i;
}
}
printf("Sum of odd numbers = %d\n", sum);
return 0;
}
9. Write a program to print the following pattern (hint: use nested loop)
12
123
1234
12345
#include <stdio.h>
int main() {
int rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}
10. Write a program to interchange the values of two arrays.
a. Program should accept an array from the user, swap the values of two arrays
and display it on the console
Input: 5
#include <stdio.h>
int main() {
int size;
printf("Enter the size of arrays: ");
scanf("%d", &size);
int array1[size], array2[size];
printf("Enter the values of Array 1 (%d values): ", size);
for (int i = 0; i < size; i++) {
scanf("%d", &array1[i]);
}
printf("Enter the values of Array 2 (%d values): ", size);
for (int i = 0; i < size; i++) {
scanf("%d", &array2[i]);
}
for (int i = 0; i < size; i++) {
int temp = array1[i];
array1[i] = array2[i];
array2[i] = temp;
}
printf("Arrays after swapping:\n");
printf("Array 1: ");
for (int i = 0; i < size; i++) {
printf("%d", array1[i]);
if (i < size - 1) {
printf(", ");
}
}
printf("\nArray 2: ");
for (int i = 0; i < size; i++) {
printf("%d", array2[i]);
if (i < size - 1) {
printf(", ");
}
}
printf("\n");
return 0;
}
Input: 5
#include <stdio.h>
int main() {
int size, count = 0;
int arr[size];
return 0;
}
12. Write a program to sort an array in descending order
a. Program should accept and array, sort the array values in descending order
and display it
Input: 5
#include <stdio.h>
int arr[size];
sortDescending(arr, size);
return 0;
}
13. Write a program to identify whether a string is a palindrome or not
a. A string is a palindrome if it reads the same backward or forward eg:
MALAYALAM
Input: MALAYALAM
Input: HELLO
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
if (isPalindrome(str)) {
printf("Entered string is a palindrome\n");
} else {
printf("Entered string is not a palindrome\n");
}
return 0;
}
14. Write a program to add to two dimensional arrays
a. Program should accept two 2D arrays and display its sum
Input: 3
Input:
123
456
789
Input:
10 20 30
40 50 60
70 80 90
11 22 33
44 55 66
77 88 99
#include <stdio.h>
int main() {
int size;
int array1[size][size];
int array2[size][size];
int sum[size][size];
return 0;
}
15. Write a program to accept an array and display it on the console using functions
a. Program should contain 3 functions including main() function
main()
1. Declare an array
2. Call function getArray()
3. Call function displayArray()
getArray()
displayArray()
#include <stdio.h>
int main() {
int size;
int arr[size];
getArray(arr, size);
displayArray(arr, size);
return 0;
}
16. Write a java program to check whether a given number is prime or not
a. Program should accept an input from the user and display whether the
number is prime or not
Input: 7
import java.util.Scanner;
if (isPrime(number)) {
System.out.println("Entered number is a Prime number");
} else {
System.out.println("Entered number is not a Prime number");
}
scanner.close();
}
if (num <= 3) {
return true;
}
if (num % 2 == 0 || num % 3 == 0) {
return false;
}
return true;
}
}
17. Write a menu driven java program to do the basic mathematical operations such as
addition, subtraction, multiplication and division (hint: use if else ladder or switch)
a. Program should have 4 functions named addition(), subtraction(),
multiplication() and division()
b. Should create a class object and call the appropriate function as user prefers
in the main function
import java.util.Scanner;
while (true) {
System.out.println("Menu:");
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
System.out.println("5. Exit");
System.out.print("Enter your choice (1/2/3/4/5): ");
switch (choice) {
case 1:
calculator.addition();
break;
case 2:
calculator.subtraction();
break;
case 3:
calculator.multiplication();
break;
case 4:
calculator.division();
break;
case 5:
System.out.println("Exiting the program.");
scanner.close();
System.exit(0);
default:
System.out.println("Invalid choice. Please select a valid option.");
}
}
}
public void addition() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
double num1 = scanner.nextDouble();
System.out.print("Enter the second number: ");
double num2 = scanner.nextDouble();
double result = num1 + num2;
System.out.println("Result: " + result);
}
if (divisor == 0) {
System.out.println("Division by zero is not allowed.");
} else {
double result = dividend / divisor;
System.out.println("Result: " + result);
}
}
}
18. Grades are computed using a weighted average. Suppose that the written test
counts 70%, lab exams 20% and assignments 10%.
Written test = 81
Lab exams = 68
Assignments = 92
Write a program to find the grade of a student during his academic year.
a. Program should accept the scores for written test, lab exams and
assignments
b. Output the grade of a student (using weighted average)
Eg:
Written test = 55
Lab exams = 73
Assignments = 87
import java.util.Scanner;
scanner.close();
}
}
Eg 1:
Enter the annual income
495000
Income tax amount = 24750.00
Eg 2:
Enter the annual income
500000
Income tax amount = 25000.00
import java.util.Scanner;
scanner.close();
}
return taxAmount;
}
}
20. Write a program to print the following pattern using for loop
2 3
4 5 6
7 8 9 10
public class NumberPattern {
public static void main(String[] args) {
int n = 4;
int num = 1;
21. Write a program to multiply the adjacent values of an array and store it in an
another array
a. Program should accept an array
b. Multiply the adjacent values
c. Store the result into another array
Eg:
1 2 3 4 5
Output
2 6 12 20
import java.util.Scanner;
System.out.println("Output:");
for (int i = 0; i < n - 1; i++) {
System.out.print(resultArray[i] + "\t");
}
scanner.close();
}
}
main()
getArray()
displayArray()
Eg:
1 2
3 4
5 6
7 8
Output:
6 8
10 12
import java.util.Scanner;
System.out.println("Output:");
displayArray(sumArray);
scanner.close();
}
main()
1. Declare an array
2. Call function getArray()
3. Call function displayArray()
getArray()
displayArray()
Eg:
1 2 3
4 5 6
7 8 9
1 2 3
4 5 6
7 8 9
import java.util.Scanner;
arrayObj.getArray(scanner);
arrayObj.displayArray();
scanner.close();
}
}
24. Write a menu driven program in java to calculate the area of a given object.
a. Program should contain two classes
i. Class 1: MyClass
ii. Class 2: Area
b. Class MyClass should inherit class Area and should contain the following
functions
i. main()
ii. circle()
iii. square()
iv. rectangle()
v. triangle()
c. Class Area should contain the following functions to calculate the area of
different objects
i. circle()
ii. square()
iii. rectangle()
iv. triangle()
circle() {
square() {
rectangle() {
}
triangle() {
Class Area{
circle(){
square(){
rectangle() {
triangle() {
Eg 1:
1. Circle
2. Square
3. Rectangle
4. Triangle
2
Output
Eg 2:
1. Circle
2. Square
3. Rectangle
4. Triangle
Output
#include <stdio.h>
#include <math.h>
int main() {
int choice;
do {
printf("Enter your choice:\n");
printf("1. Circle\n");
printf("2. Square\n");
printf("3. Rectangle\n");
printf("4. Triangle\n");
printf("5. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the radius: ");
double radius;
scanf("%lf", &radius);
double circleArea = circle(radius);
printf("Area of the circle is: %lf\n", circleArea);
break;
case 2:
printf("Enter the side length: ");
double side;
scanf("%lf", &side);
double squareArea = square(side);
printf("Area of the square is: %lf\n", squareArea);
break;
case 3:
printf("Enter the length: ");
double length;
scanf("%lf", &length);
printf("Enter the width: ");
double width;
scanf("%lf", &width);
double rectangleArea = rectangle(length, width);
printf("Area of the rectangle is: %lf\n", rectangleArea);
break;
case 4:
printf("Enter the base length: ");
double base;
scanf("%lf", &base);
printf("Enter the height: ");
double height;
scanf("%lf", &height);
double triangleArea = triangle(base, height);
printf("Area of the triangle is: %lf\n", triangleArea);
break;
case 5:
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice. Please enter a valid option.\n");
}
} while (choice != 5);
return 0;
}
25. Write a program to skip two elements after the occurrence of an odd number and
print the array elements in the following pattern
**
*
*
*
****
*
*
*
*
*
*
******
#include <stdio.h>
int main() {
int i = 1, j = 2, k = 1;
while (k <= 3) {
for (int x = 0; x < i; x++) {
if (x == (i - 1)) {
for (int y = 0; y < j; y++) {
printf("* ");
}
} else {
printf("* ");
}
printf("\n");
}
i += 3;
j += 2;
k++;
}
return 0;
}