100% found this document useful (1 vote)
130 views9 pages

PSUC Lab Exam Solutions in C

The document is the question paper for a computer science exam assessing problem solving skills in C programming. It contains 3 questions, each with multiple parts: 1. The first question involves writing programs to check if digits in a 4-digit number are the same, and to calculate the sum of terms in a mathematical series without using library functions. 2. The second question involves writing programs to remove consecutive blank spaces in a string using a function, and to separate positive and negative numbers from a 2D array into 1D arrays. 3. The third question involves writing a program using structures to store employee information with ID validation, and display details of employees in the same department based on user input.

Uploaded by

L Pircy
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
100% found this document useful (1 vote)
130 views9 pages

PSUC Lab Exam Solutions in C

The document is the question paper for a computer science exam assessing problem solving skills in C programming. It contains 3 questions, each with multiple parts: 1. The first question involves writing programs to check if digits in a 4-digit number are the same, and to calculate the sum of terms in a mathematical series without using library functions. 2. The second question involves writing programs to remove consecutive blank spaces in a string using a function, and to separate positive and negative numbers from a 2D array into 1D arrays. 3. The third question involves writing a program using structures to store employee information with ID validation, and display details of employees in the same department based on user input.

Uploaded by

L Pircy
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
  • Problem 1: Program to Check Digits of a Number
  • Problem 2: Sum of N Terms Using Library
  • Problem 3: Array Separation of Numbers
  • Problem 5: Palindrome Number Program
  • Problem 4: Employee Data Management Using Structures
  • Problem 6: Series Display with Flowchart
  • Problem 7: Number System Conversion
  • Problem 8: Array Element Reverse

Registration Number: …………………………………………..

Faculty Of Engineering
[Link]-All Branches
II Semester
Even Semester End Term Examination: 2021-22
CS1001 – Problem Solving Using Computers
(Closed Book)

Duration: 2 Hours Max. Marks: 40

Instructions:
• Answer any FIVE questions.
• Missing data, if any, may be assumed suitably.
• Calculator is not allowed

Solution and Marking Scheme

1. a. Write a program in C to check whether all the digits of a four-digit [4]


number are same or not.
Example:
Input: Enter a four-digit number = 5555
Output: Digits are same
0.5 marks for correct input from user
Sol’s-
#include <stdio.h> 3 marks for correct logic ( for each digit
identify and comparison)
int main() 0.5 marks for correct display of output
{
int n, item, flag=0,digit,len;
printf("Enter a four digit number");
scanf("%d", &n);
item=n%10;
len = 0;
while(n>0)
{
digit=n%10;
if (digit==item)
{
flag=1;

}
else
{
flag=0;
break;
}
n=n/10;

Page 1|9
Registration Number: …………………………………………..
len++;
}
if(flag==1)
{
printf("All digits of given %d digit number are same", len);
}
else
{
printf("All digits of given %d digit number are not same", len);
}

return 0;
}

b. Write a C program to find the sum of ‘N’ terms of the given series [4]
without using math.h library
3 + 17 + 55 + ……. +(2N3+1)

SOL’S
1 mark for correct input from user
#include <stdio.h> 2 marks for correct logic for addition

int main() 1 mark for correct display of output


{
int n, sum=0,i;
printf("Enter the value of N term");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+(2*i*i*i+1);
}
printf("The Sum of N Terms=%d",sum);
return 0;
}

2. a. Write a program in C (Using Function) to replace two or more [4]


consecutive blanks in a string by a single blank.
The function prototype for replacing the two or more consecutive
blanks with single blank is as below:
void replace (char str[]);
1.5 marks for correct logic for blank space
#include<string.h> checking
void replace(char *p) 1.5 marks for correct logic for replacing
{ multiple blank spaces by single space.
if (NULL== P)
return; 1 mark for overall correct syntax and library,
library functions, etc.
Page 2|9
Registration Number: …………………………………………..
int n = 0;

for(int i=0; i<strlen(p);++i)

if(p[i] != ‘ ‘)
p[n++]=p[i];

p[n] = ‘\0’;
}
int main()
{
char str[] = “A B C D E”;
printf(“before removing space %s”, str);
replace(str);
printf(“after removing space %s”, str);

b. Write a program in C to copy all the positive and negative numbers [4]
present in a two-dimensional array of ‘N’ integers into two separate
one dimensional arrays.
0.5 mark for correctly reading the 2D array
as input from the user
Program source code:
2 marks for identifying positive and
#include<stdio.h> negative numbers and storing them in
void PrintArray(int a[], int Size); respective 1D array

int main() 0.5 marks for display of output


{
1 mark for overall correct syntax and library,
int Size, i,j, a[10][10];
loops, etc.
int Positive[10], Negative[10];
int Positive_Count = 0, Negative_Count = for
0.5 marks 0, display
r, c; of output.

printf("\n Please Enter the Size row and column of an Array : ");
scanf("%d%d", &r,&c);

printf("\nPlease Enter the Array Elements : ");


for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
{
scanf("%d", &a[i][j]);
}
}

for(i = 0; i < r; i ++)


{
for(j = 0; j < c; j ++)
{

Page 3|9
Registration Number: …………………………………………..
if(a[i][j] >= 0)
{
Positive[Positive_Count] = a[i][j];
Positive_Count++;
}
else
{
Negative[Negative_Count] = a[i][j];
Negative_Count++;
}
}
}

printf("\n Total Number of Positive Numbers in this Array = %d ",


Positive_Count);
printf("\n Array Elements in Positive Array : ");
PrintArray(Positive, Positive_Count);

printf("\n Total Number of Negative Numbers in this Array = %d ",


Negative_Count);
printf("\n Array Elements in Negative Array : ");
PrintArray(Negative, Negative_Count);
return 0;
}

void PrintArray(int a[], int Size)


{
int i;
for(i = 0; i < Size; i++)
{
printf("%d \t ", a[i]);
}
printf("\n");
}

3. Write a program in C (using structures) to store the following [8]


information about ‘N’ Employees of a University:
Emp_ID, Emp_Name, Emp_Department, Emp_Salary
Since no two employees can have same employee id therefore a
validation check must be applied, such that only unique values are
stored.
Also, based on the user choice, the program must have the provision
of displaying the details of all the employees of same department.
1 mark for structure declaration and definition
Program source code: 1 mark for taking input from user
#include <stdio.h>
2.5 marks for id validation check
#include <stdlib.h>
#include <string.h> 2.5 marks for display of output

1 mark for overall correct syntax and library, loops,


Page 4|9
etc.
Registration Number: …………………………………………..

typedef struct
{
int emp_id;
char emp_name[30];
char emp_dept[30];
double salary;
}Employee;
int n=2;
Employee employees[n];
void input(int i)
{
fflush(stdin);
//Name
printf("Name of employee: ");
scanf("%[^\n]s",employees[i].emp_name);
//department of employee
fflush(stdin);
printf("Department of employee: ");
scanf("%[^\n]s",employees[i].emp_dept);
fflush(stdin);
//Salary
printf("Salary: ");
scanf("%lf",&employees[i].salary);
//to consume extra '\n' input
char ch = getchar();
printf("\n");
}

int main()
{
//number of employees

int id_check=0;
char dept[30];

//array to store structure values of all employees

//Taking each employee detail as input


printf("Enter %d Employee Details \n \n",n);
for(int i=0; i<n; i++)
{
printf("Employee %d:- \n",i+1);
//ID
printf("Id: ");
scanf("%d",&employees[i].emp_id);
for(int j=0; j<i; j++)

Page 5|9
Registration Number: …………………………………………..
{
id_check=employees[i].emp_id;
if(i==j)
{
input(i);
}
else if(id_check==employees[j].emp_id)
{
i--;
printf("emp id is same please enter again");
break;
}
else
{
input(i);
}
}
}
printf("enter the department to you want to display the information\n");
scanf("%[^\n]s",dept);
//Displaying Employee details
printf("-------------- Details of the employee's ---------------\n");
for(int i=0; i<n; i++)
{
if(strcmp(dept,employees[i].emp_dept)==0)
{
printf("Id \t: ");
printf("%d \n",employees[i].emp_id);
printf("Name \t: ");
printf("%s \n",employees[i].emp_name);
printf("Department \t: ");
printf("%s \n",employees[i].emp_dept);
printf("Salary \t: ");
printf("%.2lf \n",employees[i].salary);

printf("\n");

}
}

return 0;
}

4. a. Write a C program to input an integer number from the keyboard and [4]
perform the following operations:
(i) Check whether the number is palindrome number or not.
(ii) Print total no. of digits of the respective number.

Page 6|9
Registration Number: …………………………………………..
Example:
Input: 151
Output:
151 is a palindrome number
Number of digits of 151 = 3
Program source code:

// program of palindrom number

// program of palindrom number


1.5 marks for palindrome check

1.5 marks for total number of digits


#include<stdio.h>
1 mark for input-output and overall
int main() correct syntax and library, loops, etc.
{
int n,n1,mod,rev=0, count=0;
printf("enter a number \n");
scanf("%d", &n);
n1=n;
while(n>0)
{
mod=n%10;
rev=rev*10+mod;
n=n/10;
count++;
}
if(n1==rev)
{
printf(" %d is number is palindrom ", n1);
}
else
{
printf("%d id number is not palindrom", n1);
}
printf("\n no of digits in the number is=%d",count);
return 0;
}

b. Draw a flow chart to display N terms of the series: [4]


{1, 8, 27, 64 ……….. n3)
and find the sum of the series
1.5 marks for correct logic for display of series

1.5 marks for correct logic for calculation of sum of series

1 mark for correct symbols of flowchart

Page 7|9
Registration Number: …………………………………………..

5. Perform the following conversions from one number system to [1x8=8]


another. Show all the steps of conversion:
(i) (212.8375)10 → ( ? )16 ans: (D4.D66)16 1 mark for each part if answer is
(ii) (1067.5)10 → ( ? )8 ans: (2053.4)8 correct and all calculation steps
(iii) (DC)16 → ( ? )10 ans: (220)10 are shown.
(iv) (10110.111)2 → ( ? )10 ans: (22.875)10
If calculation part is missing,
(v) (1011101.0111)2 → ( )8 ans: (135.34)8
award zero marks.
(vi) (108.5)3 → ( ? )5 ans: 1 mark to all
(vii) (B2C1)16 → ( ? )6 ans: (551505)6
(viii) (11110.101)2 → ( ? )3 ans: (1010.12121)3

6. Write a program in C to replace each element of a ‘N’ integer 1-D array [8]
with its reverse using pointer. Write separate function for reading,
displaying and replacing the element with its reverse.
Please note that elements of the array must be accessed using
pointers.

Example:
Input Array:
179 489 76 304 56

Output Array:
971 984 67 403 65

#include <stdio.h>

void read_value(int p[],int n)

Page 8|9
Registration Number: …………………………………………..
{
int i,*a; 2 marks for read function
a=p;
printf("enter the elements\n"); 2 marks for display function
for (i=0;i<n;i++)
2 marks for reverse function
{
scanf("%d",a+i); 2 mark for function call in main
} function, pointer usage, display of
} result and overall correct syntax and
library, loops, etc.
void display(int q[],int n)
{
int i,*b;
b=q;
printf("The Elements after the reverse of values\n");
for(i=0;i<n;i++)
{
printf("%d\n",*(b+i));
}
}

void reverse (int r[],int n)


{
int i,sum,digit,*c;
c=r;
for(i=0;i<n;i++)
{
sum=0;
while(*(c+i)>0)
{
digit=*(c+i)%10;
sum=sum*10+digit;
*(c+i)=*(c+i)/10;
}
*(c+i)=sum;
}
}
int main()
{
int arr[10],n,i;
printf("enter the number of elements");
scanf("%d",&n);
read_value(arr,n);
reverse(arr,n);
display(arr,n);
return 0;
}

Page 9|9

Registration Number: …………………………………………..                      
P a g e  1 | 9
Registration Number: …………………………………………..                      
P a g e  2 | 9
Registration Number: …………………………………………..                      
P a g e  3 | 9
Registration Number: …………………………………………..                      
P a g e  4 | 9
Registration Number: …………………………………………..                      
P a g e  5 | 9
Registration Number: …………………………………………..                      
P a g e  6 | 9
Registration Number: …………………………………………..                      
P a g e  7 | 9
Registration Number: …………………………………………..                      
P a g e  8 | 9
Registration Number: …………………………………………..                      
P a g e  9 | 9

You might also like