Q1. Write an algorithm, draw a corresponding flowchart and write an interactive program to convert a decimal number to its hexadecimal equivalent.

Answer : -

Algorithm

  1. Input decimal number
  2. Divide the decimal number by 16 and write down the remainder in Hexadecimal format in a character array.
  3. Divide the decimal number again by 16. [Treat the division as an integer division.]
  4. Repeat step 2 and 3 until decimal number is 0.
  5. Print the Hexadecimal values present in the character array from last to first.


Flowchart




















Programming

#include<stdio.h>

int main()
{
long number, remainder, i, pos = -1;
char hexa_decimal[10];
printf("\n Enter the Decimal Number : ");
scanf("%ld",&number);
while (number != 0)
{
remainder = number % 16;
if(remainder < 10)
{
hexa_decimal[++pos] = 48 + remainder;
}
else
{
hexa_decimal[++pos] = 55 + remainder;
}
number = number / 16;
}
printf("\n Equivalent Hexadecimal Number = ");
for (i=pos; i>=0; i--)
{
printf("%c", hexa_decimal[i]);
}
return 0;
}

Output :
Enter the Decimal Number : 42482

Equivalent Hexadecimal Number = A5F2




Q2. Write a interactive C program to find the MINIMUM and MAXIMUM (value) array elements in a given 3X3 matrix.

Answer : -

#include<stdio.h>

int main()
{
int matrix[3][3], i, j, max, min;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\n Enter the Value of Matrix[%d][%d] : ", i, j);
scanf("%d",&matrix[i][j]);
}
}
max = matrix[0][0];
min = matrix[0][0];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(max < matrix[i][j])
{
max = matrix[i][j];
}
if(min > matrix[i][j])
{
min = matrix[i][j];
}
}
}
printf("\n Maximum Value = %d", max);
printf("\n Minimum Value = %d", min);
return 0;
}

Output :
Enter the Value of Matrix[0][0] : 4

Enter the Value of Matrix[0][1] : 8

Enter the Value of Matrix[0][2] : 12

Enter the Value of Matrix[1][0] : 3

Enter the Value of Matrix[1][1] : 6

Enter the Value of Matrix[1][2] : 9

Enter the Value of Matrix[2][0] : 5

Enter the Value of Matrix[2][1] : 10

Enter the Value of Matrix[2][2] : 15

Maximum Value = 15
Minimum Value = 3




Q3. Write the following functions that :

  • Calculate simple interest.
  • Calculate compound interest.

Write a C (main) program to provide the above functions as options to the user using switch statement and performs the functions accordingly.

Answer : -

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

int main()
{
int time, choice, n;
float amount, interest_rate, interest_amount, total_amount;

printf("\n Enter the Principal Amount : ");
scanf("%f",&amount);
printf("\n Enter the Yearly Interest Rate : ");
scanf("%f",&interest_rate);
printf("\n Enter the Time Period in Year : ");
scanf("%d",&time);

printf("\n Press 1 to Calculate Simple Interest");
printf("\n Press 2 to Calculate Compound Interest");
printf("\n Enter your choice : ");
scanf("%d",&choice);

switch(choice)
{
case 1:
interest_amount = (amount * time * interest_rate) / 100;
printf("\n Simple Interest of Rs. %f for %d Years is Rs. %f", amount, time, interest_amount);
break;

case 2:
printf("\n Enter the Number of Times the given Interest is Compounded : ");
scanf("%d",&n);
total_amount = amount * pow((1 + (interest_rate / (100 * n))),(n * time));
interest_amount = total_amount - amount;
printf("\n Compound Interest of Rs. %f for %d Years is Rs. %f", amount, time, interest_amount);
break;
}
return 0;
}

Output 1 :
Enter the Principal Amount : 10000

Enter the Yearly Interest Rate : 5.0

Enter the Time Period in Year : 5

Press 1 to Calculate Simple Interest
Press 2 to Calculate Compound Interest
Enter your choice : 1

Simple Interest of Rs. 10000.000000 for 5 Years is Rs. 2500.000000

Output 2 :
Enter the Principal Amount : 10000

Enter the Yearly Interest Rate : 5.0

Enter the Time Period in Year : 5

Press 1 to Calculate Simple Interest
Press 2 to Calculate Compound Interest
Enter your choice : 2

Enter the Number of Times the given Interest is Compounded : 5

Compound Interest of Rs. 10000.000000 for 5 Years is Rs. 2824.317383




Q4. Write the following string functions that :

  • Replace a character in a given string with a character suggested by the user.
  • Convert the given string into uppercase.
  • Convert the alternate character into upper case.
  • Check each and every character in the string and display whether it is an alphabet, digit or special character.

Write a C (main) program to provide the above string functions as options to the user using switch statement and perform the functions accordingly.

Answer : -

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

int main()
{
char string[100], c1, c2;
int choice, i, count = 0;

printf("\n Enter the String : ");
gets(string);

printf("\n Press 1 to replace a Character in a given String with a Character suggested by the User");
printf("\n Press 2 to convert the given String into Upper Case");
printf("\n Press 3 to convert the alternate Character into Upper Case");
printf("\n Press 4 to check each and every Character in the String and display whether it is an Alphabet, Digit or Special Character");
printf("\n Enter your choice : ");
scanf("%d",&choice);

switch(choice)
{
case 1:
printf("\n Enter the Character you want to replace : ");
fflush(stdin);
scanf("%c",&c1);
printf("\n Enter the replacement Character suggested by You : ");
fflush(stdin);
scanf("%c",&c2);
for(i=0; string[i]!='\0'; i++)
{
if(string[i] == c1)
{
string[i] = c2;
count++;
}
}
if(count == 0)
printf("\n Character Not Found");
else
printf("\n New String - %s", string);
break;

case 2:
for(i=0; string[i]!='\0'; i++)
{
string[i] = toupper(string[i]);
}
printf("\n New String - %s", string);
break;

case 3:
for(i=0; string[i]!='\0'; i++)
{
if(i%2 == 0)
{
string[i] = toupper(string[i]);
}
else
{
string[i] = tolower(string[i]);
}
}
printf("\n New String - %s", string);
break;

case 4:
for(i=0; string[i]!='\0'; i++)
{
if(isalpha(string[i]))
printf("\n %c - Alphabet", string[i]);
else
if(isdigit(string[i]))
printf("\n %c - Digit", string[i]);
else
if(isspace(string[i]))
printf("\n %c - Space", string[i]);
else
printf("\n %c - Special Character", string[i]);
}
break;
}
return 0;
}

Output 1 :
Enter the String : DATABASE

Press 1 to replace a Character in a given String with a Character suggested by the User
Press 2 to convert the given String into Upper Case
Press 3 to convert the alternate Character into Upper Case
Press 4 to check each and every Character in the String and display whether it is an Alphabet, Digit or Special Character
Enter your choice : 1

Enter the Character you want to replace : A

Enter the replacement Character suggested by You : *

New String - D*T*B*SE


Output 2 :
Enter the String : West Bengal

Press 1 to replace a Character in a given String with a Character suggested by the User
Press 2 to convert the given String into Upper Case
Press 3 to convert the alternate Character into Upper Case
Press 4 to check each and every Character in the String and display whether it is an Alphabet, Digit or Special Character
Enter your choice : 2

New String - WEST BENGAL


Output 3 :
Enter the String : Debabrata

Press 1 to replace a Character in a given String with a Character suggested by the User
Press 2 to convert the given String into Upper Case
Press 3 to convert the alternate Character into Upper Case
Press 4 to check each and every Character in the String and display whether it is an Alphabet, Digit or Special Character
Enter your choice : 3

New String - DeBaBrAtA


Output 4 :
Enter the String : (A+B) $5

Press 1 to replace a Character in a given String with a Character suggested by the User
Press 2 to convert the given String into Upper Case
Press 3 to convert the alternate Character into Upper Case
Press 4 to check each and every Character in the String and display whether it is an Alphabet, Digit or Special Character
Enter your choice : 4

(-Special Character
A-Alphabet
+-Special Character
B-Alphabet
)-Special Character
-Space
$-Special Character
5-Digit




Q5. Write a program to search a given string among the available strings, using Binary Search.

Answer : -

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

int main()
{
char string[20];
/* In Binary Search array must be Sorted in Ascending or Descending order */
char array[][10] = {"Bangalore", "Chennai", "Delhi", "Kolkata", "Mumbai"};
int start_index = 0, end_index = 4, mid_index, count = 0;

printf("\n Enter the String you Want to Search : ");
gets(string);

while(start_index <= end_index)
{
mid_index = (start_index + end_index) / 2;
if(strcmp(array[mid_index], string) == 0)
{
count = 1;
break;
}
else
if(strcmp(array[mid_index], string) < 0)
{
start_index = mid_index + 1;
}
else
if(strcmp(array[mid_index], string) > 0)
{
end_index = mid_index - 1;
}
}
if(count == 0)
printf("\n String Not Found");
else
printf("\n String Found");
return 0;
}

Output 1 :
Enter the String you Want to Search : Kolkata
String Found

Output 2 :
Enter the String you Want to Search : Srinagar
String Not Found




Q6. Using structures concept in C programming, write a program to calculate the daily wages for each worker (if 7 workers are employed in an iron and hardware shop) at an hourly basis of Rs.100/- (with a constraint that each worker may be allowed maximum upto 4 hours only per day). It should display the name of the worker, date and total wages for that day.

Answer : -

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

/* Structure Declaration */
struct worker
{
char name[25];
int hours;
int wages;
};

int main()
{
int i;
char date[12];
/* Structure Array Declaration */
struct worker w[7];

/* Declared 7 Workers name who worked in an Iron and Hardware Shop */
strcpy(w[0].name, "DEBABRATA PANCHADHYAY");
strcpy(w[1].name, "PRANKRISHNA BANIK");
strcpy(w[2].name, "SWARUP PAUL");
strcpy(w[3].name, "SOMERAJ DEY");
strcpy(w[4].name, "DEBARGHYA CHAKRABORTY");
strcpy(w[5].name, "RITEN SAMADDER");
strcpy(w[6].name, "NAVONIL BHATTACHARYA");

printf("\n Enter the Date [DD-MM-YYYY] : ");
gets(date);

/* Input Total Working Hours for each Worker */
for(i=0;i<7;i++)
{
do
{
printf("\n Enter the Total Working Hours of %s : ", w[i].name);
scanf("%d",&w[i].hours);
if(w[i].hours <= 4)
{
w[i].wages = w[i].hours * 100;
}
else
{
printf("\n Wrong Input - Allow maximum upto 4 Hours per day for each Worker");
}
}
while(w[i].hours > 4);
}

/* Display the Wages pay to each Worker */
printf("\n\n\n Date - %s", date);
for(i=0;i<7;i++)
{
printf("\n\n Name - %s", w[i].name);
printf("\n Wages - %d", w[i].wages);
}
return 0;
}

Output :
Enter the Date [DD-MM-YYYY] : 30-08-2020

Enter the Total Working Hours of DEBABRATA PANCHADHYAY : 3

Enter the Total Working Hours of PRANKRISHNA BANIK : 4

Enter the Total Working Hours of SWARUP PAUL : 4

Enter the Total Working Hours of SOMERAJ DEY : 2

Enter the Total Working Hours of DEBARGHYA CHAKRABORTY : 4

Enter the Total Working Hours of RITEN SAMADDER : 8

Wrong Input - Allow maximum upto 4 Hours per day for each Worker
Enter the Total Working Hours of RITEN SAMADDER : 2

Enter the Total Working Hours of NAVONIL BHATTACHARYA : 3


Date - 30-08-2020

Name - DEBABRATA PANCHADHYAY
Wages - 300

Name - PRANKRISHNA BANIK
Wages - 400

Name - SWARUP PAUL
Wages - 400

Name - SOMERAJ DEY
Wages - 200

Name - DEBARGHYA CHAKRABORTY
Wages - 400

Name - RITEN SAMADDER
Wages - 200

Name - NAVONIL BHATTACHARYA
Wages - 300




Q7. Using pointers, find the sum of all the elements of a 3X3 matrix.

Answer : -

#include<stdio.h>

int main()
{
int matrix[3][3], i, j, sum = 0, *p;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\n Enter the Value of Matrix[%d][%d] : ", i, j);
scanf("%d",&matrix[i][j]);
}
}
p = &matrix[0][0];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
sum = sum + *(p + i*3 + j);
}
}
printf("\n Answer = %d", sum);
return 0;
}

Output :
Enter the Value of Matrix[0][0] : 4

Enter the Value of Matrix[0][1] : 8

Enter the Value of Matrix[0][2] : 12

Enter the Value of Matrix[1][0] : 3

Enter the Value of Matrix[1][1] : 6

Enter the Value of Matrix[1][2] : 9

Enter the Value of Matrix[2][0] : 5

Enter the Value of Matrix[2][1] : 10

Enter the Value of Matrix[2][2] : 15

Answer = 72




Q8. Using file handling, write a C program :

a) To generate 10 records for MCA 1st semester students and store them in stu.dat along with appropriate fields.

Answer : -

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

struct result
{
char subject_code[10];
int marks;
};

struct student
{
char enrolment_no[10];
char name[25];
struct result sem_I[7];
};

int main()
{
int i;
FILE *stream;
struct student s;

strcpy(s.sem_I[0].subject_code, "MCS-011");
strcpy(s.sem_I[1].subject_code, "MCS-012");
strcpy(s.sem_I[2].subject_code, "MCS-013");
strcpy(s.sem_I[3].subject_code, "MCS-014");
strcpy(s.sem_I[4].subject_code, "MCS-015");
strcpy(s.sem_I[5].subject_code, "MCSL-016");
strcpy(s.sem_I[6].subject_code, "MCSL-017");

stream = fopen("D:\\stu.dat","w");
if(stream == NULL)
{
printf("\nError! Can not open output file");
}
else
{
for(i=1;i<=10;i++)
{
printf("\nEnter the Student Enrolment Number : ");
fflush(stdin);
gets(s.enrolment_no);
printf("\nEnter the Student Name : ");
fflush(stdin);
gets(s.name);
printf("\nEnter the Marks obtain in MCS-011 : ");
scanf("%d",&s.sem_I[0].marks);
printf("Enter the Marks obtain in MCS-012 : ");
scanf("%d",&s.sem_I[1].marks);
printf("Enter the Marks obtain in MCS-013 : ");
scanf("%d",&s.sem_I[2].marks);
printf("Enter the Marks obtain in MCS-014 : ");
scanf("%d",&s.sem_I[3].marks);
printf("Enter the Marks obtain in MCS-015 : ");
scanf("%d",&s.sem_I[4].marks);
printf("Enter the Marks obtain in MCSL-016 : ");
scanf("%d",&s.sem_I[5].marks);
printf("Enter the Marks obtain in MCSL-017 : ");
scanf("%d",&s.sem_I[6].marks);
fwrite(&s, sizeof(s), 1, stream);
}
fclose(stream);
}
return 0;
}

Output :
Enter the Student Enrolment Number : 105508022

Enter the Student Name : Debabrata Panchadhyay

Enter the Marks obtain in MCS-011 : 40
Enter the Marks obtain in MCS-012 : 56
Enter the Marks obtain in MCS-013 : 58
Enter the Marks obtain in MCS-014 : 69
Enter the Marks obtain in MCS-015 : 62
Enter the Marks obtain in MCSL-016 : 85
Enter the Marks obtain in MCSL-017 : 70

Enter the Student Enrolment Number : 105508024

Enter the Student Name : Prankrishna Banik

Enter the Marks obtain in MCS-011 : 60
Enter the Marks obtain in MCS-012 : 51
Enter the Marks obtain in MCS-013 : 54
Enter the Marks obtain in MCS-014 : 68
Enter the Marks obtain in MCS-015 : 64
Enter the Marks obtain in MCSL-016 : 75
Enter the Marks obtain in MCSL-017 : 65

Enter the Student Enrolment Number : 105508026

Enter the Student Name : Swarup Paul

Enter the Marks obtain in MCS-011 : 92
Enter the Marks obtain in MCS-012 : 76
Enter the Marks obtain in MCS-013 : 84
Enter the Marks obtain in MCS-014 : 89
Enter the Marks obtain in MCS-015 : 87
Enter the Marks obtain in MCSL-016 : 98
Enter the Marks obtain in MCSL-017 : 94

Enter the Student Enrolment Number : 105508028

Enter the Student Name : Someraj Dey

Enter the Marks obtain in MCS-011 : 68
Enter the Marks obtain in MCS-012 : 64
Enter the Marks obtain in MCS-013 : 62
Enter the Marks obtain in MCS-014 : 75
Enter the Marks obtain in MCS-015 : 68
Enter the Marks obtain in MCSL-016 : 89
Enter the Marks obtain in MCSL-017 : 81

Enter the Student Enrolment Number : 105508030

Enter the Student Name : Debarghya Chakraborty

Enter the Marks obtain in MCS-011 : 45
Enter the Marks obtain in MCS-012 : 56
Enter the Marks obtain in MCS-013 : 52
Enter the Marks obtain in MCS-014 : 53
Enter the Marks obtain in MCS-015 : 58
Enter the Marks obtain in MCSL-016 : 67
Enter the Marks obtain in MCSL-017 : 62

Enter the Student Enrolment Number : 105508032

Enter the Student Name : Riten Samadder

Enter the Marks obtain in MCS-011 : 98
Enter the Marks obtain in MCS-012 : 86
Enter the Marks obtain in MCS-013 : 92
Enter the Marks obtain in MCS-014 : 95
Enter the Marks obtain in MCS-015 : 84
Enter the Marks obtain in MCSL-016 : 99
Enter the Marks obtain in MCSL-017 : 91

Enter the Student Enrolment Number : 105508034

Enter the Student Name : Navonil Bhattacharya

Enter the Marks obtain in MCS-011 : 82
Enter the Marks obtain in MCS-012 : 85
Enter the Marks obtain in MCS-013 : 74
Enter the Marks obtain in MCS-014 : 89
Enter the Marks obtain in MCS-015 : 71
Enter the Marks obtain in MCSL-016 : 82
Enter the Marks obtain in MCSL-017 : 90

Enter the Student Enrolment Number : 105508036

Enter the Student Name : Arindam Kundu

Enter the Marks obtain in MCS-011 : 90
Enter the Marks obtain in MCS-012 : 81
Enter the Marks obtain in MCS-013 : 84
Enter the Marks obtain in MCS-014 : 83
Enter the Marks obtain in MCS-015 : 76
Enter the Marks obtain in MCSL-016 : 90
Enter the Marks obtain in MCSL-017 : 92

Enter the Student Enrolment Number : 105508038

Enter the Student Name : Manirash Das

Enter the Marks obtain in MCS-011 : 74
Enter the Marks obtain in MCS-012 : 56
Enter the Marks obtain in MCS-013 : 64
Enter the Marks obtain in MCS-014 : 63
Enter the Marks obtain in MCS-015 : 58
Enter the Marks obtain in MCSL-016 : 60
Enter the Marks obtain in MCSL-017 : 42

Enter the Student Enrolment Number : 105508040

Enter the Student Name : Payel Chatterjee

Enter the Marks obtain in MCS-011 : 85
Enter the Marks obtain in MCS-012 : 67
Enter the Marks obtain in MCS-013 : 52
Enter the Marks obtain in MCS-014 : 69
Enter the Marks obtain in MCS-015 : 58
Enter the Marks obtain in MCSL-016 : 80
Enter the Marks obtain in MCSL-017 : 50




b) To read the data from the file stu.dat (created above) and compute the total marks and average marks and display the grade(assumptions can be made).

Answer : -

#include<stdio.h>

struct result
{
char subject_code[10];
int marks;
};

struct student
{
char enrolment_no[10];
char name[25];
struct result sem_I[7];
};

int main()
{
FILE *stream;
struct student s;
int i, total_marks, average_marks;

stream = fopen("D:\\stu.dat","r");
if(stream == NULL)
{
printf("\nError! Can not open output file");
}
else
{
while(fread(&s, sizeof(s), 1, stream))
{
printf("\n\n Enrolment No - %s", s.enrolment_no);
printf("\n Name - %s", s.name);
total_marks = 0;
for(i=0;i<7;i++)
{
total_marks = total_marks + s.sem_I[i].marks;
}
average_marks = total_marks / 7;
printf("\n Total Marks - %d", total_marks);
printf("\n Average Marks - %d", average_marks);

if(average_marks >= 40 && average_marks < 60)
printf("\n Grade - D");
else
if(average_marks >= 60 && average_marks < 70)
printf("\n Grade - C");
else
if(average_marks >= 70 && average_marks < 80)
printf("\n Grade - B");
else
if(average_marks >= 80 && average_marks < 90)
printf("\n Grade - A");
else
if(average_marks >= 90 && average_marks <= 100)
printf("\n Grade - E");
}
fclose(stream);
}
return 0;
}

Output :
Enrolment No - 105508022
Name - Debabrata Panchadhyay
Total Marks - 440
Average Marks - 62
Grade - C

Enrolment No - 105508024
Name - Prankrishna Banik
Total Marks - 437
Average Marks - 62
Grade - C

Enrolment No - 105508026
Name - Swarup Paul
Total Marks - 620
Average Marks - 88
Grade - A

Enrolment No - 105508028
Name - Someraj Dey
Total Marks - 507
Average Marks - 72
Grade - B

Enrolment No - 105508030
Name - Debarghya Chakraborty
Total Marks - 393
Average Marks - 56
Grade - D

Enrolment No - 105508032
Name - Riten Samadder
Total Marks - 645
Average Marks - 92
Grade - E

Enrolment No - 105508034
Name - Navonil Bhattacharya
Total Marks - 573
Average Marks - 81
Grade - A

Enrolment No - 105508036
Name - Arindam Kundu
Total Marks - 596
Average Marks - 85
Grade - A

Enrolment No - 105508038
Name - Manirash Das
Total Marks - 417
Average Marks - 59
Grade - D

Enrolment No - 105508040
Name - Payel Chatterjee
Total Marks - 461
Average Marks - 65
Grade - C