KEMBAR78
Week 0.2 - Assignments | PDF | Computer Engineering | Computer Science
0% found this document useful (0 votes)
18 views56 pages

Week 0.2 - Assignments

The document contains a series of programming assignments in C, each with a specific task such as finding the smallest of three numbers, calculating the frequency of elements in an array, and implementing an ATM functionality. Each assignment includes a complete code snippet that demonstrates how to achieve the given task. The document serves as a practical guide for learning various programming concepts and techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views56 pages

Week 0.2 - Assignments

The document contains a series of programming assignments in C, each with a specific task such as finding the smallest of three numbers, calculating the frequency of elements in an array, and implementing an ATM functionality. Each assignment includes a complete code snippet that demonstrates how to achieve the given task. The document serves as a practical guide for learning various programming concepts and techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 56

Assignments

1. Write a program to find smallest of three numbers using ternary operator

#include <stdio.h>

int main() {
int num1, num2, num3, smallest;

printf("Enter three numbers: ");


scanf("%d %d %d", &num1, &num2, &num3);

smallest = (num1 < num2) ? ((num1 < num3) ? num1 : num3) : ((num2 < num3) ?
num2 : num3);

printf("The smallest number is: %d\n", smallest);

return 0;
}

2. Write a program to find the frequency of each element in the array

#include <stdio.h>

int main() {
int arr[100], freq[100];
int n;

printf("Enter the number of elements in the array: ");


scanf("%d", &n);

printf("Enter the elements of the array:\n");


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
freq[i] = -1;
}

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


int count = 1;

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


if (arr[i] == arr[j]) {
count++;
freq[j] = 0;
}
}

if (freq[i] != 0) {
freq[i] = count;
}
}

printf("Frequency of elements in the array:\n");


for (int i = 0; i < n; i++) {
if (freq[i] != 0) {
printf("%d occurs %d times\n", arr[i], freq[i]);
}
}

return 0;
}

3. Write a program to find Second Largest Number in an Array

#include <stdio.h>

int main() {
int arr[100];
int n;

printf("Enter the number of elements in the array: ");


scanf("%d", &n);

if (n < 2) {
printf("At least two elements are required to find the second largest number.\n");
return 1;
}

printf("Enter the elements of the array:\n");


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

int firstMax, secondMax;

if (arr[0] > arr[1]) {


firstMax = arr[0];
secondMax = arr[1];
} else {
firstMax = arr[1];
secondMax = arr[0];
}

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


if (arr[i] > firstMax) {
secondMax = firstMax;
firstMax = arr[i];
} else if (arr[i] > secondMax && arr[i] != firstMax) {
secondMax = arr[i];
}
}

printf("The second largest number in the array is: %d\n", secondMax);

return 0;
}

4. Program to Remove Duplicate Element in an array

#include <stdio.h>

int main() {
int arr[100], uniqueArr[100];
int n, i, j, k = 0, isDuplicate;

printf("Enter the number of elements in the array: ");


scanf("%d", &n);

printf("Enter the elements of the array:\n");


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

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


isDuplicate = 0;
for (j = 0; j < k; j++) {
if (arr[i] == uniqueArr[j]) {
isDuplicate = 1;
break;
}
}

if (!isDuplicate) {
uniqueArr[k++] = arr[i];
}
}

printf("Array with duplicate elements removed:\n");


for (i = 0; i < k; i++) {
printf("%d ", uniqueArr[i]);
}
printf("\n");

return 0;
}
5. Write a program to find all subsets of a string.

#include <stdio.h>
#include <string.h>

void subString(char str[], int n) {


for (int t = 1; t <= n; t++) {
for (int i = 0; i <= n - t; i++) {
int j = i + t - 1;
for (int k = i; k <= j; k++) {
printf("%c", str[k]);
}

printf("\n");
}
}
}

int main() {
char str1[100];
printf("Enter a string: ");
scanf("%s", str1);

printf("All the substrings of the above string are:\n");


subString(str1, strlen(str1));

return 0;
}
6. Write a program to find maximum and minimum occurring character in a string

#include <stdio.h>
#include <string.h>
#define MAX_CHAR 256

void findMaxMinChar(char str[]) {


int count[MAX_CHAR] = {0};
int len = strlen(str);

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


count[str[i]]++;
}

char maxChar = str[0];


char minChar = str[0];

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


if (count[str[i]] > count[maxChar]) {
maxChar = str[i];
}
if (count[str[i]] < count[minChar]) {
minChar = str[i];
}
}

printf("Maximum occurring character: %c\n", maxChar);


printf("Minimum occurring character: %c\n", minChar);
}

int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);

findMaxMinChar(str);

return 0;
}

7. Write a program to print the prime numbers upto a limit.

#include <stdio.h>
#include <stdbool.h>

bool isPrime(int n) {
if (n <= 1) {
return false;
}
if (n <= 3) {
return true;
}
if (n % 2 == 0 || n % 3 == 0) {
return false;
}
for (int i = 5; i * i <= n; i = i + 6) {
if (n % i == 0 || n % (i + 2) == 0) {
return false;
}
}
return true;
}

void printPrimesUpToLimit(int limit) {


if (limit >= 2) {
printf("Prime numbers up to %d are:\n", limit);
printf("2 ");
}

for (int i = 3; i <= limit; i += 2) {


if (isPrime(i)) {
printf("%d ", i);
}
}

printf("\n");
}
int main() {
int limit;

printf("Enter a limit to find prime numbers up to that limit: ");


scanf("%d", &limit);

printPrimesUpToLimit(limit);

return 0;
}

8. Create a program to check whether the given number is an Armstrong Number

#include <stdio.h>
#include <math.h>

int countDigits(int number) {


int count = 0;
while (number != 0) {
number /= 10;
count++;
}
return count;
}

_Bool isArmstrong(int number) {


int originalNumber = number;
int n = countDigits(number);
int sum = 0;

while (number != 0) {
int digit = number % 10;
sum += pow(digit, n);
number /= 10;
}

return sum == originalNumber;


}

int main() {
int num;

printf("Enter a number to check if it's an Armstrong number: ");


scanf("%d", &num);

if (isArmstrong(num)) {
printf("%d is an Armstrong number.\n", num);
} else {
printf("%d is not an Armstrong number.\n", num);
}

return 0;
}
9. Create the functionality of ATM
a. To withdraw the money, we simply get the withdrawal amount from the user
and remove that amount from the total balance and print the successful
message.
b. To deposit the money, we simply get the deposit amount from the user, add it
to the total balance and print the successful message.
c. To check balance, we simply print the total balance of the user.
d. Exit

#include <stdio.h>

void withdraw(float *balance, float amount) {


if (amount > 0 && amount <= *balance) {
*balance -= amount;
printf("Withdrawal successful. Current balance: %.2f\n", *balance);
} else {
printf("Invalid withdrawal amount or insufficient balance.\n");
}
}

void deposit(float *balance, float amount) {


if (amount > 0) {
*balance += amount;
printf("Deposit successful. Current balance: %.2f\n", *balance);
} else {
printf("Invalid deposit amount.\n");
}
}

void checkBalance(float balance) {


printf("Your current balance is: %.2f\n", balance);
}

int main() {
float balance = 1000.00;
int choice;
float amount;

printf("Welcome to the ATM\n");

while (1) {
printf("\nOptions:\n");
printf("1. Withdraw Money\n");
printf("2. Deposit Money\n");
printf("3. Check Balance\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the withdrawal amount: ");
scanf("%f", &amount);
withdraw(&balance, amount);
break;

case 2:
printf("Enter the deposit amount: ");
scanf("%f", &amount);
deposit(&balance, amount);
break;

case 3:
checkBalance(balance);
break;

case 4:
printf("Thank you for using the ATM. Goodbye!\n");
return 0;

default:
printf("Invalid choice. Please select a valid option.\n");
}
}

return 0;
}
10. Write a program to Find Square Root of a Number

#include <stdio.h>
#include <math.h>

int main() {
double num, result;

printf("Enter a number: ");


scanf("%lf", &num);

if (num >= 0) {
result = sqrt(num);
printf("The square root of %.2lf is %.2lf\n", num, result);
} else {
printf("Invalid input. Square root of a negative number is undefined.\n");
}

return 0;
}

11. Write a program with different functions for the following operations in array
a. Find Smallest element
b. Find Largest element
c. Sum of all elements
d. Number of elements

#include <stdio.h>

int findSmallest(int arr[], int size) {


int smallest = arr[0];
for (int i = 1; i < size; i++) {
if (arr[i] < smallest) {
smallest = arr[i];
}
}
return smallest;
}

int findLargest(int arr[], int size) {


int largest = arr[0];
for (int i = 1; i < size; i++) {
if (arr[i] > largest) {
largest = arr[i];
}
}
return largest;
}

int calculateSum(int arr[], int size) {


int sum = 0;
for (int i = 0; i < size; i++) {
sum += arr[i];
}
return sum;
}

int findNumElements(int arr[], int size) {


return size;
}

int main() {
int arr[] = {35, 9, 47, 56, 70, 2, 81};
int size = sizeof(arr) / sizeof(arr[0]);

int smallest = findSmallest(arr, size);


printf("Smallest element: %d\n", smallest);

int largest = findLargest(arr, size);


printf("Largest element: %d\n", largest);

int sum = calculateSum(arr, size);


printf("Sum of all elements: %d\n", sum);
int numElements = findNumElements(arr, size);
printf("Number of elements: %d\n", numElements);

return 0;
}

12. Write a program to check weather the sum of digits and product of digits of a
number is same

#include <stdio.h>

int sumOfDigits(int num) {


int sum = 0;
while (num != 0) {
sum += num % 10;
num /= 10;
}
return sum;
}

int productOfDigits(int num) {


int product = 1;
while (num != 0) {
product *= num % 10;
num /= 10;
}
return product;
}

int main() {
int number;

printf("Enter a number: ");


scanf("%d", &number);

int sum = sumOfDigits(number);


int product = productOfDigits(number);

if (sum == product) {
printf("The sum of digits and the product of digits are the same.\n");
} else {
printf("The sum of digits and the product of digits are not the same.\n");
}

return 0;
}

13. Write a program that consists a class named ‘student’, it should have following
characteristics

a. Name
b. School (same for all students - thus create as static)
c. Grade
d. Mark
e. A function to check if student is passed or not
The program should ask for
a. Add student
b. Search student - it should show details including pass status
c. Display all students
d. Exit

#include <stdio.h>
#include <stdbool.h>
#include <string.h>

struct Student {
char name[50];
char school[50];
int grade;
float mark;
bool isPassed;
};

void checkPassStatus(struct Student *student) {


student->isPassed = (student->mark >= 40.0);
}

void addStudent(struct Student students[], int *count) {


if (*count < 100) {
struct Student newStudent;
printf("Enter student name: ");
scanf("%s", newStudent.name);

strcpy(newStudent.school, "G H S School Ulikkal");

printf("Enter student grade: ");


scanf("%d", &newStudent.grade);

printf("Enter student mark: ");


scanf("%f", &newStudent.mark);

checkPassStatus(&newStudent);

students[*count] = newStudent;
(*count)++;

printf("Student added successfully!\n");


} else {
printf("Maximum number of students reached.\n");
}
}

void searchStudent(struct Student students[], int count) {


char searchName[50];
printf("Enter student name to search: ");
scanf("%s", searchName);

bool found = false;

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


if (strcmp(students[i].name, searchName) == 0) {
printf("Student Details:\n");
printf("Name: %s\n", students[i].name);
printf("School: %s\n", students[i].school);
printf("Grade: %d\n", students[i].grade);
printf("Mark: %.2f\n", students[i].mark);
printf("Pass Status: %s\n", students[i].isPassed ? "Passed" : "Failed");
found = true;
break;
}
}

if (!found) {
printf("Student not found.\n");
}
}

void displayAllStudents(struct Student students[], int count) {


if (count > 0) {
printf("List of all students:\n");
for (int i = 0; i < count; i++) {
printf("Student %d:\n", i + 1);
printf("Name: %s\n", students[i].name);
printf("School: %s\n", students[i].school);
printf("Grade: %d\n", students[i].grade);
printf("Mark: %.2f\n", students[i].mark);
printf("Pass Status: %s\n", students[i].isPassed ? "Passed" : "Failed");
}
} else {
printf("No students in the system.\n");
}
}

int main() {
struct Student students[100];
int studentCount = 0;

int choice;

do {
printf("\nMenu:\n");
printf("1. Add Student\n");
printf("2. Search Student\n");
printf("3. Display All Students\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
addStudent(students, &studentCount);
break;
case 2:
searchStudent(students, studentCount);
break;
case 3:
displayAllStudents(students, studentCount);
break;
case 4:
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice. Please select a valid option.\n");
}
} while (choice != 4);

return 0;
}
14. Create the following patterns:

#include <stdio.h>
int main()
{
int rows = 5;
for (int i = 0; i < rows; i++) {
for (int j = 0; j <= i; j++) {
printf("%d ", j + 1);
}
printf("\n");
}
return 0;
}
#include <stdio.h>
int main()
{
int rows = 5;
for (int i = 0; i < rows; i++) {

for (int j = 0; j < rows - i; j++) {


printf("%d ", j + 1);
}
printf("\n");
}
}

#include <stdio.h>
int main() {
int rows, i, j;
for (i = 1; i <= 5; i++) {
for (j = 1; j <= i; j++) {
if (j == 1 || j == i || i == 5) {
printf("%d ",j);
} else {
printf(" ");
}
}

printf("\n");
}
return 0;
}

#include <stdio.h>
int main() {
int i, space, rows, k = 0, count = 0, count1 = 0;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (space = 1; space <= rows - i; ++space) {
printf(" ");
++count;
}
while (k != 2 * i - 1) {
if (count <= rows - 1) {
printf("%d ", i + k);
++count;
} else {
++count1;
printf("%d ", (i + k - 2 * count1));
}
++k;
}
count1 = count = k = 0;
printf("\n");
}
return 0;
}

#include <stdio.h>
int main() {
int n=5;
int i, j;
for(i=1;i<=n;i++){
for(j=1;j<=n-i;j++){
printf(" ");
}
for(j=1;j<=i;j++){
if(j==1||i==n||j==i){
printf("%2d",j);
}else{
printf(" ");
}
}
printf("\n");
}
}

#include <stdio.h>
int main()
{
int i, j, n, k = 0;
printf("Enter number of rows : ");
scanf("%d",&n);
for(i = 1; i <= n; i++)
{
for(j = i; j <= n; j++)
{
if (i == 1 || j == i || j == n)
printf(" %d ", j);
else
printf(" ");
}
printf("\n");
}
return 0;
}

15. Create the following patterns:


#include <stdio.h>

int main() {
int n = 6;

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


for (int j = 3; j <= i; j++) {
printf("%d", i);
}
printf("\n");
}

for (int i = n - 1; i >= 1; i--) {


for (int j = 3; j <= i; j++) {
printf("%d", i);
}
printf("\n");
}

return 0;
}

#include<stdio.h>
int main()
{
int i,j;
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
if(j<i)
printf("%d*",i);
else
printf("%d",i);
}
printf(" \n");
}
for(i=4;i>=1;i--)
{
for(j=1;j<=i;j++)
{
if(j<i)
printf("%d*",i);
else
printf("%d",i);
}
printf(" \n");
}
return 0;
}

#include <stdio.h>
#include <conio.h>
int main()
{
int n,i,temp,j,k=1,l,m;
scanf("%d",&n);

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


{
for(j=1 ; j<=i ; j++)
{
printf("%d ",k);
k++;
if(i!=j)
printf("*");

}
printf("\n");
}
k-= n;
temp=k;
for(i=n ; i>=1; i--)
{
for(j=1 ; j<=i ; j++)
{
printf("%d ",temp);
temp++;
if(i!=j)
printf("*");
}
printf("\n");
k-= (i-1);
temp=k;

}
}
#include <stdio.h>

int main()
{
int i, j, N;

printf("Enter number of rows : ");


scanf("%d", &N);
printf("*\n");

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


{
for(j=1; j<=i; j++)
{
if(j == 1)
printf("* ");
printf(" %d ", j);
}
for(j=i-1; j>=1; j--)
{
printf(" %d ", j);
}
printf("* ");
printf("\n");
}

for(i=N-1; i>=1; i--)


{
for(j=1; j<=i; j++)
{
if(j == 1)
printf("* ");
printf(" %d ", j);
}
for(j=i-1; j>=1; j--)
{
printf(" %d ", j);
}
printf("* ");
printf("\n");
}
printf("* \n ");
return 0;
}

#include<stdio.h>

int main()
{
int n,i,j,u1,u2;
printf("Enter the number of lines: ");
scanf("%d",&n);
if(n%2!=0)
{
u1=(n-1)/2;
u2=3*n/2-1;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i+j==u1||i-j==u1||j-i==u1||i+j==u2||i==u1||j==u1)
printf("*");
else
printf(" ");
}
printf("\n");
}
}
else
printf("\nPlease give correct input (no. of lines should be odd)");
}
#include <stdio.h>
int main()
{
int n;
printf("Enter the number of rows in your pattern: ");
scanf("%d",&n);
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
if(i == 1 || i == n || j == 1 || j == n - i + 1 || i == j || j == n)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}

#include <stdio.h>
int main() {
int n;
printf("Enter the number of lines: ");
scanf("%d", &n);

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


for (int j = 1; j <= i; j++) {
printf("*");
}

for (int j = 1; j <= 2 * (n - i); j++) {


printf(" ");
}

for (int j = 1; j <= i; j++) {


printf("*");
}

printf("\n");
}

for (int i = n; i >= 1; i--) {


for (int j = 1; j <= i; j++) {
printf("*");
}

for (int j = 1; j <= 2 * (n - i); j++) {


printf(" ");
}
for (int j = 1; j <= i; j++) {
printf("*");
}

printf("\n");
}

return 0;
}

#include <stdio.h>
int main()
{
int i, j, n;
scanf("%d", &n);
int num1 = n / 2 * 3;

printf("Right Arrow\n");
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
if(i == n / 2 || j - i == n / 2 || i + j == num1)
printf("*");
else
printf(" ");
}
printf("\n");
}
printf("Left Arrow\n");
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
if(i == n / 2 || i - j == n / 2 || i + j == n / 2)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
#include <stdio.h>
int main()
{
int n;
printf("Enter the number of rows in your pattern: ");
scanf("%d",&n);
for(int i = n; i >= 1; i--) {
for(int j = 1; j <= i - 1; j++) {
printf(" ");
}
for(int k = 1; k <= n; k++) {
printf("*");
}
printf("\n");
}
return 0;
}

#include <stdio.h>
int main()
{
int n;
printf("Enter the number of rows in your pattern: ");
scanf("%d",&n);
for(int i = n; i >= 1; i--) {
for(int j = 1; j <= i - 1; j++) {
printf(" ");
}
for(int k = 1; k <= n; k++) {
if(i == 1 || i == n || k == 1 || k == n)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
16. Given a string, S of size N, and a number of rows R, the task is to print the given
string in a vertical zigzag fashion with respect to the given number of rows as shown in the
examples.

a. Input:
S=“123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmno
pqrstuvwxyz”, R = 9

Output:

b. Input: S = “AttentionReaders!Don’tStopLearning!HappyLearning!”, R = 12
Output:

#include <stdio.h>
#include <string.h>

void zigzag(const char* s, int rows) {


int length = strlen(s);
int interval = 2 * rows - 2;

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


int step = interval - 2 * i;
for (int j = i; j < length; j = j + interval) {
printf("%c", s[j]);

if (step > 0 && step < interval && step + j < length) {
for (int k = 0; k < (interval - rows - i); k++) {
printf(" ");
}

printf("%c", s[j + step]);

for (int k = 0; k < i - 1; k++) {


printf(" ");
}
} else {
for (int k = 0; k < (interval - rows); k++) {
printf(" ");
}
}
}
printf("\n");
}
}
int main() {

const char* s = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefgh"


"ijklmnopqrstuvwxyz";
int rows = 9;

zigzag(s, rows);

return 0;
}
#include <stdio.h>
#include <string.h>

void zigzag(const char* s, int rows) {


int length = strlen(s);
int interval = 2 * rows - 2;

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


int step = interval - 2 * i;
for (int j = i; j < length; j = j + interval) {
printf("%c", s[j]);

if (step > 0 && step < interval && step + j < length) {
for (int k = 0; k < (interval - rows - i); k++) {
printf(" ");
}

printf("%c", s[j + step]);

for (int k = 0; k < i - 1; k++) {


printf(" ");
}
} else {
for (int k = 0; k < (interval - rows); k++) {
printf(" ");
}
}
}
printf("\n");
}
}
int main() {

const char* s = "AttentionReaders!Don’tStopLearning!HappyLearning!";


int rows = 12;

zigzag(s, rows);

return 0;
}

17. Write a program to delete all the multiples of 5 in an array. Print the array
elements in the following pattern.

__XXXX
_XXXX
X
__XXX
_XXX
X
X
__XX
_XX
X
X
X
__X
_X
X
X
X
X

18. Write a C program to draw the following pattern

X
_XX
X
X
__XXXX
X
X
X
___XXXXXXXX

19. Merge two arrays & remove duplicate elements from the array. Print the array
elements in the following pattern.

***
*
*
*
******
*
*
*
*
*
*
*********

20. Given a string. Find the length of the string. If the string length is odd print the
following pattern

X X
X X
X
X X
X X

If the string length is even, print the following pattern

X X
X X
X X
X X
X X
X X

#include <stdio.h>
#include <string.h>

int main() {
char input[100];

printf("Enter a string: ");


scanf("%s", input);

int length = strlen(input);

if (length % 2 == 1) {
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++) {
if (i == j || j == length - 1 - i) {
printf("X");
} else {
printf(" ");
}
}
printf("\n");
}
} else {
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++) {
if (i == j || j == length - 1 - i) {
printf("X");
} else {
printf(" ");
}
}
printf("\n");
}
}

return 0;
}

21. Write a C program to find the second largest element of an array(Let it be ‘n’) and
print ‘n’ number of * in the following pattern.
**********
*
********
*
*
******
*
*
*
****
*
*
*
*
**
*
*
*
*
*

22. Write a program in C to find even numbers in an array. Print the even integers in
following pattern

X
XX
_X
XXXX
__X
XXXXXX
___X
XXXXXXXX
____X

You might also like