0% found this document useful (0 votes)
7 views16 pages

Hands on Programming Solution(Giridhar)

The document outlines a series of problem-solving exercises in C programming, covering topics such as searching in arrays, calculating sums of digits, identifying possible words from incomplete words, generating PINs from input numbers, and managing employee records. Each exercise includes a description of the problem, sample inputs and outputs, and corresponding C code implementations. The exercises are categorized by difficulty level and aim to enhance programming skills through practical applications.

Uploaded by

k.s.yogeswar3
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)
7 views16 pages

Hands on Programming Solution(Giridhar)

The document outlines a series of problem-solving exercises in C programming, covering topics such as searching in arrays, calculating sums of digits, identifying possible words from incomplete words, generating PINs from input numbers, and managing employee records. Each exercise includes a description of the problem, sample inputs and outputs, and corresponding C code implementations. The exercises are categorized by difficulty level and aim to enhance programming skills through practical applications.

Uploaded by

k.s.yogeswar3
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/ 16

PROBLEM SOLVING

Exercise No. 3

Topics Covered : Problem Solving Exercise III

Date : 06.11.2024

Solve the following problems

Q.
Question Detail Level
No.

1. You have an array A[] of N students standing in ascending Medium

order of their assigned numbers.


The PT instructor calls out a number R and wants to know if
this student is already in the line.
 If R is present in the array, output the position
(index) of R in A[].
 If R is not present, output the position where R
would fit in to maintain the ascending order.
Sample
Input N: 6
A []: {1,2,3,4,7,9}
R: 3
Sample Output
2
Sample
Input N: 6
A []: {1,2,3,4,7,9}
R: 6
Sample Output
4
Program:
#include<stdio.h>
int order(int n,int arr[],int r)
{
int count=0;

1
PROBLEM SOLVING

for(int i=n-1;i>=0;i--)
{
if(r==arr[i])
return i;
else if(r<arr[i])
{
count++;
}
}
for (int i = n; i > n - count; i--)
{
arr[i] = arr[i - 1];
}
arr[n-count]=r;
return (n-count);
}

int main()
{
int n,position;
printf("Enter the number of elements in array : ");
scanf("%d",&n);
int arr[n+1],r;
printf("Enter the elements(Ascending):");
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("The array is ");
for(int i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
printf("\nEnter the element to be in which index:");
2
PROBLEM SOLVING

scanf("%d",&r);
position=order(n,arr,r);
printf("The element %d is in position %d",r,position);
/*if(position==-1)
{
n++;
printf("The updated array : ");
for(int i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
}
else{
printf("The element %d is in position %d",r,position);
}*/

return 0;
}
Output:
Enter the number of elements in array : 6
Enter the elements(Ascending):1 2 3 4 7 9
The array is 1 2 3 4 7 9
Enter the element to be in which index:3
The element 3 is in position 2

Enter the number of elements in array : 6


Enter the elements(Ascending):1 2 3 4 7 9
The array is 1 2 3 4 7 9
Enter the element to be in which index:6
The element 6 is in position 4
2. Alex has been asked by his teacher to do an assignment Mediu
on sums of digits of number. The assignment requires m
Alex to find the sum of sums of digits of a given number,
as per the method mentioned below.

3
PROBLEM SOLVING

If the given number is 582109, the Sum of Sums of Digits


will be calculated as
=(5 + 8 + 2 + 1 + 0 + 9) + (8 + 2 + 1 + 0 + 9) + (2 + 1 +
0 + 9) +
(1 + 0 + 9) + (0 + 9)+(9)= 25 + 20 + 12 + 10 + 9 + 9 =
85

Alex contacts you to help him write a program for finding


the Sum of Sums of Digits for any given number, using the
above method. Help Alex by writing the code in a function
sumOfSumsOfDigits which takes an integer input
representing the given number.

The function is expected to return the


"SumofSumsofDigits" of input.

Assumptions: For this assignment, let us assume that


the given number will always contain more than 1 digit,
i.e., the given number will always be >9.

Sample Input:
N: 582109
Sample Output:
SumOfSumsOfDigits: 85
Sample Input:
N: 5
Sample Output:
Please Try again!!
Program:
#include<stdio.h>
int main()
{
int num,dig=0;
int rem;
printf("Enter the number:");
scanf("%d",&num);
int temp=num;
if(num>9)
{
while(num!=0)
{
rem=num%10;
dig++;
num/=10;
4
PROBLEM SOLVING

}
num=temp;
rem=0;
//printf("Number : %d - Digit : %d\n",num,dig);
int arr[dig],sum=0;
for(int i=0;i<dig;i++)
{
rem=num%10;
arr[i]=rem;
num/=10;
}
for(int i=0;i<dig;i++)
{
for(int j=0;j<(dig-i);j++)
{
sum+=arr[j];
}
}
printf("Sums of sums of Digit:%d",sum);
}
else{
printf("SIngle digit is Invalid. Please Try again!");
}
}
Output:
Enter the number:582109
Sums of sums of Digit:85
Enter the number:5
Single digit is Invalid. Please Try again!

5
PROBLEM SOLVING

3. Detective Bakshi found a letter containing several words, Mediu


each with one missing character, indicated by an m
underscore (e.g., "Fi_er"). He also found strips of paper
listing groups of possible complete words, separated by
colons (e.g.,"Fever:filer:Filter:Fixer:fiber:fibre:tailor
").Detective Bakshi suspects that each incomplete word
corresponds to one of the possible complete words on
these strips.
To help Detective Bakshi solve the case, you need to write
a function, identifyPossibleWords, that takes two inputs:
1. The incomplete word, containing one
missing character represented by an underscore.
2. A colon-separated list of possible words.
The function should find all words from the list that could
match the incomplete word if the missing character were
filled in. It
should then return these matching words in the specified
format.
Sample Input
Enter the incomplete word: fi_er
Enter the word list (separated by colons):
Fever:filer:Filter:Fixer:fiber:fibre:tailor:offer
Sample Output
Possible words: filer, Fixer, fiber

Program:
#include<stdio.h>
#include<string.h>
int main(){
char str1[255];
printf("Enter the incomplete word: ");
gets(str1);
char str2[255];
int found=0;
char *token;
printf("Enter the word list (separated by colons) :");
scanf("%s",str2);
token=strtok(str2,":");
printf("\nPossible words:");
while(token!=NULL)
{
if(strlen(token)==strlen(str1))
{
found=1;
6
PROBLEM SOLVING

for(int i=0;i<strlen(str1);i++)
{
if(str1[i]!='_' && str1[i]!=token[i])
{
found=0;
break;
}
}
if(found)
{
printf("\n%s",token);
}
}
token = strtok(NULL, ":");
}
return 0;
}
Output:
Enter the incomplete word: fi_er
Enter the word list (separated by colons):
fever:filer:filter:fixer:fiber:fibre:tailor:offer

Possible words:
filer
fixer
fiber

7
PROBLEM SOLVING

4. Secure Assets Private Ltd”, a small company that deals Medium


with lockers has recently started manufacturing digital
locks which can be locked and unlocked using PINs
(passwords). You have been asked to work on the module
that is expected to generate PINs using three input
numbers.
Assumptions: The three given input numbers will always
consist of three digits each i.e. each of them will be in
range is greater than or equal to 100 and is less than or
equal to 999.
100 is less than or equal to input1 is less than or equal to
999 100 is less than or equal to input2 is less than or
equal to 999 100 is less than or equal to input3 is less
than or equal to 999 Below are the rules for generating
the PIN. –
The PIN should be made up of 4 digits.
-The unit(ones) position of the PIN should be the least
of the units position of the three input numbers
-The tens position of the PIN should be the least of the
tens position of the three input numbers
-The hundreds position of the PIN should be the least of
the hundreds position of the three input numbers
-The thousands position of the PIN should be the
maximum of all the digits in the three input numbers
Sample Input
Number1 : 123
Number2 : 582
Number3 : 175
Sample Output
PIN : 8122
Sample Input
Number1 : 190
Number2 : 267
Number3 : 853
Sample Output
PIN=9150
Program:
#include<stdio.h>
int pinones(int o1, int o2, int o3) {
int ones=(o1<o2)?((o1<o3)?o1:o3):((o2<o3)?o2:o3);
8
PROBLEM SOLVING

return ones;
}
int pintens(int t1,int t2,int t3){
int tens = (t1<t2)?((t1<t3)? t1:t3):((t2<t3)?t2:t3);
return tens;
}
int pinhundreds(int h1,int h2,int h3) {
int hundreds=(h1<h2)?((h1<h3)?h1:h3):((h2<h3)?
h2:h3);
return hundreds;
}

int pinthousands(int arr[],int n) {


int max=arr[0];
for (int i=0;i<n;i++) {
if (max<arr[i])
max=arr[i];
}
return max;
}
int calculatepin(int a,int b,int c,int d) {
int res=0;
int temp[4]={a,b,c,d};
for (int i=0;i<4;i++) {
res=(res*10)+temp[i];
}
return res;
}

int main(){
int num1,num2,num3;
int arr1[3],arr2[3],arr3[3],arr[9];
int ones,tens,hundreds,thousands,pin;

printf("Enter Number 1: ");


scanf("%d",&num1);
printf("Enter Number 2: ");
scanf("%d",&num2);
printf("Enter Number 3: ");
scanf("%d",&num3);

9
PROBLEM SOLVING

for(int i=2;i>=0;i--){
arr1[i]=num1%10;
num1/=10;
arr2[i]=num2%10;
num2/=10;
arr3[i]=num3%10;
num3/=10;
}
for(int i=0;i<3;i++) {
arr[i]=arr1[i];
arr[i+3]=arr2[i];
arr[i+6]=arr3[i];
}
ones=pinones(arr1[2],arr2[2],arr3[2]);
tens=pintens(arr1[1],arr2[1],arr3[1]);
hundreds=pinhundreds(arr1[0],arr2[0],arr3[0]);
thousands=pinthousands(arr,9);
printf("\nOnes place: %d",ones);
printf("\nTens place: %d",tens);
printf("\nHundreds place: %d",hundreds);
printf("\nThousands place: %d",thousands);

pin=calculatepin(thousands,hundreds,tens,ones);
printf("\nPIN: %d",pin);

return 0;
}
Output:
Enter Number 1: 123
Enter Number 2: 582
Enter Number 3: 172

Ones place: 2
Tens place: 2
Hundreds place: 1
Thousands place: 8
PIN: 8122

1
0
PROBLEM SOLVING

5. Create a C program to manage employee records within a Mediu


company. The program should allow users to retrieve m
details of employees based on their unique identification
numbers (Emp No) and calculate their total salary
according to specified rules.
Requirements:
1. Structure Definition:
o Define a structure named Employee to hold
the following employee information:
 Employee Number (Emp No): A
string that uniquely identifies each
employee.
 Employee Name: A string containing
the name of the employee.
 Join Date: A string representing the
date the employee joined the company.
 Designation Code: A single
character representing the employee's
designation.
 Department: A string indicating the
department where the employee works.
 Basic Salary: An integer representing
the employee's base salary.
 House Rent Allowance (HRA): An
integer for the employee's housing
allowance.
 Income Tax (IT): An integer
representing the tax deductions.
2. Salary Calculation Rules:
o Implement the following adjustmentsbased
on designation codes:
 'e' (Engineer): Add 20,000 to the total
salary.
 'c' (Consultant): Add 32,000 to the total
salary.
 'k' (Clerk): Add 12,000 to the total salary.
 'r' (Receptionist): Add 15,000 to the total
salary.
 'm' (Manager): Add 40,000 to the total
salary.
3. Input Specification:
o The program should accept an employee
number.
4. Processing Steps:
1
1
PROBLEM SOLVING

o Search through an array of Employee


structures to locate the employee associated
with the provided employee number.

1
2
PROBLEM SOLVING

o If the employee is found:


 Calculate the total salary using the
formula: Total Salary=Basic
Salary+HRA−IT+Salary Adju stment
 Display the following information:
 Employee Number
 Employee Name
 Department
 Designation
 Total Salary
o If the employee is not found, output a
message
indicating the absence of an employee with
the specified number.

Sample Input ( Input the below data as hard coded


values)
{"1001", "Ashish", "01/04/2009", 'e', "R&D", 20000, 8000,
3000},
{"1002", "Sushma", "23/08/2012", 'c', "PM", 30000, 12000,
9000},
{"1003", "Rahul", "12/11/2008", 'k', "Acct", 10000, 8000,
1000},
{"1004", "Chahat", "29/01/2013", 'r', "Front Desk", 12000,
6000,
2000},
{"1005", "Ranjan", "16/07/2005'm', "Engg", 50000, 20000,
20000},
{"1006", "Suman", "01/01/2000", 'e', "Manufacturing",
23000, 9000,
4400},
{"1007", "Tanmay", "12/06/2006", 'c', "PM", 29000, 12000,
10000}
Enter Employee Number: 1002

Sample Output
Employee Number:
1002 Employee
Name: Sushma
Department: PM
Designation: c
Total Salary: 65000
Program:
#include <stdio.h>

1
3
PROBLEM SOLVING

#include <string.h>
#define MAX_EMPLOYEES 100

struct employee {
char empno[10];
char empname[50];
char date[11];
char designation;
char dept[50];
int salary;
int rent;
int tax;
};

void calculate(struct employee emp1, int adj_sal) {


int totalsalary=emp1.salary+emp1.rent-
emp1.tax+adj_sal;
printf("Employee number: %s\n", emp1.empno);
printf("Employee Name: %s\n", emp1.empname);
printf("Department: %s\n", emp1.dept);
printf("Designation: %c\n", emp1.designation);
printf("Total Salary: %d\n", totalsalary);
}

int main() {
struct employee employees[MAX_EMPLOYEES] = {
{"1001", "Ashish", "01/04/2009", 'e', "R&D", 20000,
8000, 3000},
{"1002", "Sushma", "23/08/2012", 'c', "PM", 30000,
12000, 9000},
{"1003", "Rahul", "12/11/2008", 'k', "Acct", 10000,
8000, 1000},
{"1004", "Chahat", "29/01/2013", 'r', "Front Desk",
12000, 6000,2000},
{"1005", "Ranjan", "16/07/2005",'m', "Engg", 50000,
20000,20000},
{"1006", "Suman", "01/01/2000", 'e',
"Manufacturing", 23000, 9000,4400},
{"1007", "Tanmay", "12/06/2006", 'c', "PM", 29000,
12000, 10000}

};

int empcount = 5,found=1;


1
4
PROBLEM SOLVING

char empnum[10];
int adj_sal, tot_sal;

printf("Enter the Employee number: ");


scanf("%s", empnum);

//int found = 0;

for (int i = 0; i < empcount; i++)


{
if (strcmp(empnum, employees[i].empno) == 0)
{
if (employees[i].designation == 'e')
adj_sal = 20000;
else if (employees[i].designation == 'c')
adj_sal = 32000;
else if (employees[i].designation == 'k')
adj_sal = 12000;
else if (employees[i].designation == 'r')
adj_sal = 15000;
else if (employees[i].designation == 'm')
adj_sal = 40000;
else
adj_sal = 0;

//tot_sal = employees[i].salary + adj_sal;


calculate(employees[i], adj_sal);
found=1;
}

}
if(found!=1)
{
printf("The employee is not present");
}

return 0;
}
Output:
Enter the Employee number: 1001
Employee number: 1001
Employee Name: Ashish
Department: R&D
Designation: e
1
5
PROBLEM SOLVING

Total Salary: 45000

Enter the Employee number: 1005


Employee number: 1005
Employee Name: Ranjan
Department: Engg
Designation: m
Total Salary: 90000

Enter the Employee number: 1010


The employee is not present

1
6

You might also like