Here, We provide Programming For Problem Solving GTU Paper Solution Winter 2023. Read the Full PPS GTU Paper Solution Winter 2023 given below.
(a) Write an algorithm to check whether the entered number is Even
or Odd.
Step 1: START.
Step 2: Enter the Number to Check.
Step 3: if Number is divisible by 2 then the Number is Even.
Step 4: Else Number is Odd.
Step 5: Print the Output.
Step 6: STOP.
(b) Draw the flowchart to find the factorial of a number given by
user
(c) Briefly explain different components of Computer system.
- Input Unit:
- The input unit takes all the data received by the computer.
- The input unit comprises different devices such as a mouse, keyboard, scanner, etc
- All of these devices act as intermediaries between the users and the computer.
2. Central Processing Unit (CPU):
- The Central Processing Unit or CPU is known as the brain of the computer. Just
like the human brain controls all human activities, the CPU also takes care of all the
tasks. - The CPU is responsible for performing all the arithmetic and logical operations
within the computer. - The CPU comprises two parts- ALU (Arithmetic Logic Unit) and CU (Control
Unit). - ➢ Arithmetic Logic Unit (ALU)
▪ It is the fundamental building block of the central processing unit (CPU)
of a computer.
▪ ALUs perform addition, subtraction, and multiplication using sequences
of logic gates.
➢ Control Unit (CU):
▪ The Control Unit is the part of the computer’s central processing unit
(CPU), which directs the operation of the processor.
▪ It controls data flow inside the processor
➢ Memory Unit:
▪ The memory unit is responsible for transferring information to other units
of the computer when needed.
▪ Data and instructions are stored in memory units which are required for
processing. - An output device is a hardware component of a computer system that displays
- information to users. Monitor, Printer, Speakers Headphones, Projector, GPS and
- Plotter are some output devices of computer.
- It retrieves and presents the result of the inserted input data from the computer
system and further translates that data into human-understandable language.
(a) Give the output of following C code.
Int main(){
Printf(“%d”,15<2);
Printf(“%d”,15&&2);
Printf(“%d”,7%10);
Return0; }
Output
017
(b) Demonstrate the use of bitwise operator with an example.
- OR operator (|) takes two numbers as operands and does OR on every bit of two numbers
- AND operator (&) takes two numbers as operands and does AND on every bit of two numbers.
- XOR (^) takes two numbers as operands and does XOR on every bit of two numbers.
- left shift (<<) takes two numbers, right shifts the bits of the first operand, and the second operand decides the number of places to shift.
- right shift (>>) takes two numbers, right shifts the bits of the first operand, and the second operand decides the number of places to shift.
- NOT (~) takes one number and inverts all bits of it.
// C Program to demonstrate use of bitwise operators
include<stdio.h>
int main()
{
int a = 5, b = 9;
printf("a = %d, b = %d\n", a, b);
//AND operation
printf("a&b = %d\n", a & b);
// OR operation
printf("a|b = %d\n", a | b);
// XOR operation
printf("a^b = %d\n", a ^ b);
// NOT operation
printf("~a = %d\n", a = ~a);
// left shift
printf("b<<1 = %d\n", b << 1);
// right shift printf("b>>1 = %d\n", b >> 1);
return 0;
}
(c) Explain C tokens in detail.
Identifiers
These are used to name the arrays, functions, structures, variables, etc. The
identifiers are user-defined words . These can consist of lowercase letters,
uppercase letters, digits, or underscores, but the starting letter should always be
either an alphabet or an underscore.
Keywords
The keywords as the reserved or pre-defined words that hold their own
importance. It means that every keyword has a functionality of its own. Since the
keywords are basically predefined words that the compilers use, thus we cannot
use them as the names of variables.
Operators
The operators in C are the special symbols that we use for performing various
functions. Operands are those data items on which we apply the operators.
o Unary Operator
o Binary Operator
o Ternary Operator
Strings
The strings in C always get represented in the form of an array of characters. We
have a ‘\0′ null character at the end of any string- thus, this null character
represents the end of that string.
Special Characters
All of them hold a special meaning that we cannot use for any other purpose
o ()Simple brackets
o [ ] Square brackets
o , Comma
o {} Curly braces
o * Asterisk
o # Hash/preprocessor
o . Period
o ~ Tilde
Constant
Constant is basically a value of a variable that does not change throughout a
program. The constants remain the same, and we cannot change their value
whatsoever.
(c) Briefly explain different storage classes used in C with appropriate example.
1.Automatic Storage Class
- It is a default storage class and also known as local variables.
- The variables defined using auto storage.
- The scope of an auto variable is limited with the particular block only. Once the control goes out of the block, the access is destroyed.
- A keyword auto is used to define an auto storage class. By default, an auto variable contains a garbage value.
- Example,
#include<stdio.h>
int main() {
Increment();
Increment();
Increment();
}
Increment(){
auto int i=1;
printf("%d",i);
i = i +1
}
2.Extern Storage Class
- It is a global variable.
- Extern storage class is used when we have global functions or variables which are shared between two or more files.
- Keyword extern is used to declaring a global variable
- These variables are accessible throughout the program.
- example
#include<stdio.h>
int a = 10;
int main( )
{
extern int b;
printf("Value of a = %d and b = %d \n", a, b);
return 0;
}
3.Static Storage Class
- It is a local variable which is capable of returning a value even when control is transferred to the function call.
- The static variables are used within function/ file as local static variables. They can also be used as a global variable
- static variable has a default initial value zero and is initialized only once in its lifetime.
- Example
#include<stdio.h>
void increment()
{
static int i;
i++;
printf("%d\t",i);
}
int main()
{
increment();
increment();
increment();
return 0;
}
4.Register Storage Class
- It is a variable which is stored inside a Register.
- You can use the register storage class when you want to store local variables within functions or blocks in CPU registers instead of RAM to have quick access to these variables.
- The keyword register is used to declare a register storage class. The variables declared using register storage class has lifespan throughout the program.
- Register has faster access than that of the main memory.
- The variables declared using register storage class has no default value and are often declared at the beginning of a program.
- example
#include<stdio.h>
int main()
{
register int i=2;
printf("%d",i);
}
(a) Demonstrate the use of ternary operator with an example.
- The conditional operator in C is kind of similar to the if-else statement as it follows the same algorithm as of if-else statement but the conditional operator takes less space and helps to write the if-else statements in the shortest way possible. It is also known as the ternary operator.
- Syntax:
variable = (condition) ? Expression2 : Expression3;
- Example
#include <stdio.h>
int main()
{
int m = 5, n = 4;
(m > n) ? printf("m is greater than n that is %d > %d",m, n)
: printf("n is greater than m that is %d > %d",n, m);
return 0;
}
(b)Give the output of following C codes.
Output:
1) terminate without any output.
2) 10
(c) Write a C program to print following pattern using loop.
5
4 4
3 3 3
2 2 2 2
1 1 1 1 1
#include <stdio.h>
int main()
{
int i, j, N;
printf("Enter N: ");
scanf("%d", &N);
for(i=1; i<=N; i++)
{
// Logic to print numbers
for(j=1; j<=i; j++)
{
printf("%d", (N - i + 1));
}
printf("\n");
}
return 0;
}
(a) Differentiate between break and continue.
Break statement | continue statement |
The Break statement is used to exit from the loop constructs. | The continue statement is not used to exit from the loop constructs. |
The break statement is usually used with the switch statement, and it can also use it within the while loop, do-while loop, or the for-loop. | The continue statement is not used with the switch statement, but it can be used within the while loop, do-while loop, or for-loop. |
When a break statement is encountered then the control is exited from the loop construct immediately. | When the continue statement is encountered then the control automatically passed from the beginning of the loop statement. |
Syntax: break; | Syntax: continue; |
Break statements uses switch and label statements. | It does not use switch and label statements. |
Leftover iterations are not executed after the break statement. | Leftover iterations can be executed even if the continue keyword appears in a loop. |
(b)Demonstrate the use of forward jump and backward jump with an example.
The control moves unconditionally to another area of the program when a Jump Statement is satisfied.
goto statement is a type of Jump statement in c.
The goto statement is identical to the continue statement in the jump statement.
The goto statement is of two type
-Forward jump
-Backward jump
Forward Jump :
• When goto statement is placed before the label in the program, control transfers to the label and some statements are skipped. Such type of jump is called forward jump.
• Syntax:
goto label:
//block of statements
Lebel;
//block of statements
# include <stdio.h>
void main ()
{
int num =20;
if (num%2==0)
goto even;
else
goto odd;
even:
printf(“%d is even”,num);
odd:
printf(“%d is odd”,num);
}
Backward jump :
• When goto statement is placed after the label, control jumps backward direction and some statement are repeated. Such type of go to jump is called backward jump.
• Syntex:
label:
//block of statements
goto lebel;
//block of statements
#include <stdio.h>
int main()
{
int num, i=1;
printf(“Enter the number whose table you want to print?”);
scanf(“%d”,&num);
table:
printf(“%d * %d = %d\n”,num,i,num*i);
i++;
if(i<=10)
goto table;
}
(c) Explain else… if ladder with an example.
- It is used when there are more than two possible action based on different conditions.
if (Condition1)
{
Statement1;
}
else if(Condition2)
{
Statement2;
}
.
.
.
else if(ConditionN)
{
StatementN;
}
else
{
Default_Statement;
}
#include<stdio.h>
void main ()
{
int a,b,c,d;
printf("Enter the values of a,b,c,d: ");
scanf("%d%d%d%d",&a,&b,&c,&d);
if(a>b && a>c && a>d)
{
printf("%d is the largest",a);
}
else if(b>c && b>a && b>d)
{
printf("%d is the largest",b);
}
else if(c>d && c>a && c>b)
{
printf("%d is the largest",c);
}
else
{
printf("%d is the largest",d);
}
}
(a) Give the significance of puts(), getchar(), getch().
• puts()
(a) puts() is a function defined in header <stdio.h> that prints strings character by character until the NULL character is encountered.
(b) The puts() function prints the newline character at the end of the output string.
• getchar
(a) getchar is a standard library function that takes a single input character from standard input.
(b) It is defined inside the <stdio.h> header file.
• getch()
(a) getch() is a nonstandard function and is present in conio.h header file.
(b) getch() also reads a single character from the keyboard. But it does not use any buffer, so the entered character is immediately returned without waiting for the enter key
(b) Differentiate between call by value and call by reference.
Call By value | Call by reference |
While calling a function, we pass the values of variables to it. Such functions are known as “Call By Values”. | While calling a function, instead of passing the values of variables, we pass the address of variables(location of variables) to the function known as “Call By References |
In this method, the value of each variable in the calling function is copied into corresponding dummy variables of the called function. | In this method, the address of actual variables in the calling function is copied into the dummy variables of the called function. |
With this method, the changes made to the dummy variables in the called function have no effect on the values of actual variables in the calling function. | With this method, using addresses we would have access to the actual variables and hence we would be able to manipulate them. |
In call-by-values, we cannot alter the values of actual variables through function calls. | In call by reference, we can alter the values of variables through function calls. |
Values of variables are passed by the Simple technique. | Pointer variables are necessary to define to store the address values of variables. |
This method is preferred when we have to pass some small values that should not change | This method is preferred when we have to pass a large amount of data to the function. |
Call by value is considered safer as original data is preserved | Call by reference is risky as it allows direct modification in original data |
(c) Write a C program to check whether two strings are same or not.
#include <stdio.h>
#include <string.h>
int main(int argc, char const *argv[ ])
{
char string 1[] = {"Hello world"};
char string2[] = {"Hello Duniya"};
//using function strcmp() to compare the two strings
if (strcmp(string1, string2) == 0)
{
printf("Yes 2 strings are same");
}
else
{
printf("No, 2 strings are not same");
}
return 0;
}
(a) Give the output of following C code.
int main(){
int val = 20, *p;
p=& val
printf(“%d %d %d”, val, * p sizeof(p));
return 0;
}
Output:
20 20 4
(b) Demonstrate the use of recursion with an example.
• Recursion is the process of defining a problem (or the solution to a problem) in terms of (a simpler version of) itself.
• It is a method of programming where a function calls itself directly or indirectly.
• Example
#include <stdio.h>
// Recursive function to calculate factorial
int factorial(int n) {
// Base case: factorial of 0 or 1 is 1
if (n == 0 || n == 1) {
return 1;
} else {
// Recursive case: n! = n * (n-1)!
return n * factorial(n - 1);
}
}
int main() {
// Example: calculating the factorial of 5
int result = factorial(5);
printf("Factorial of 5 is: %d\n", result);
return 0;
}
In this example, the factorial function is defined to calculate the factorial of a given number n. The base case checks if n is 0 or 1, in which case the factorial is 1. Otherwise, the function calls itself with the argument (n – 1) to calculate the factorial recursively.
(c) Write a C program to find sum of digits for a given number using the concept of User Defined Function (UDF). (Hint: For number 3278, sum of digits is 3 + 2 + 7 + 8 = 20 ).
#include <stdio.h>
int main()
{
int num, sum=0;
/* Input a number from user */
printf("Enter any number to find sum of its digit: ");
scanf("%d", &num);
/* Repeat till num becomes 0 */
while(num!=0)
{
/* Find last digit of num and add to sum */
sum += num % 10;
/* Remove last digit from num */
num = num / 10;
}
printf("Sum of digits = %d", sum);
return 0;
}
(a) Differentiate between structure and union.
Structure | Union |
The keyword struct is used to define a structure | The keyword union is used to define a union. |
When a variable is associated with a structure, the compiler allocates the memory for each member. The size of structure is greater than or equal to the sum of sizes of its members. | when a variable is associated with a union, the compiler allocates the memory by considering the size of the largest memory. So, size of union is equal to the size of largest member. |
Each member within a structure is assigned unique storage area of location. | Memory allocated is shared by individual members of union. |
Altering the value of a member will not affect other members of the structure. | Altering the value of any of the member will alter other member values. |
Individual member can be accessed at a time. | Only one member can be accessed at a time. |
Several members of a structure can initialize at once. | Only the first member of a union can be initialized. |
(b) Briefly explain any two file handling functions with an example.
- fopen
- fopen is used to open a file. It takes two parameters: the name of the file to be opened and the mode in which to open the file (e.g., read, write, append).
- Example:
FILE *filePointer;
filePointer = fopen("example.txt", "w");
if (filePointer == NULL) {
printf("Error opening the file.\n");
return 1;
}
// Perform operations on the file
fclose(filePointer);
- fprintf
- fprintf is used to write formatted data to a file. It is similar to printf but writes the output to the specified file instead of the console.
- Example-
FILE *filePointer;
filePointer = fopen("example.txt", "w");
if (filePointer == NULL)
{
printf("Error opening the file.\n");
return 1;
}
fprintf(filePointer, "This is an example line in the file.\n");
fclose(filePointer);
(c) Write a C program to find maximum and minimum from an array of 10 elements.
#include <stdio.h>
int main()
{
int arr1[10];
int i, mx, mn;
// Prompt user for input
printf("\n\nFind maximum and minimum element in an array \n");
// Input elements for the array
printf("Input 10 elements in the array :\n");
for (i = 0; i < 9; i++)
{
printf("element - %d : ", i);
scanf("%d", &arr1[i]);
}
// Initialize max (mx) and min (mn) with the first element of the array
mx = arr1[0];
mn = arr1[0];
// Traverse the array to find the maximum and minimum elements
for (i = 1; i < 9; i++)
{
// Update mx if the current element is greater
if (arr1[i] > mx)
{
mx = arr1[i];
}
// Update mn if the current element is smaller
if (arr1[i] < mn)
{
mn = arr1[i];
}
}
// Print the maximum and minimum elements
printf("Maximum element is : %d\n", mx);
printf("Minimum element is : %d\n\n", mn);
return 0;
}
(a)
Give the output of following C code.
int main() {
int arr r[3][2] = {1, 2, 3, 4, 5, 6} ;
printf(“%d %d %d”, arr[0][1], arr[1][0], arr[2][1] );
return 0;
}
Output:
2 3 6
(b) Briefly explain memory management functions.
- Memory management is the process of controlling and coordinating a computer’s main memory.
- Memory Management functions:
- calloc()
Dynamically allocates an array of memory blocks of a specified type.
- free()
Dynamically de-allocates memory at runtime.
- malloc()
Allocates a block of memory in the heap, but does not initialize.
- realloc()
Reallocates a block of memory that was previously allocated.
(c) Write C program to copy one file to other.
#include <stdio.h>
#include <stdlib.h> // For exit()
int main()
{
FILE *fptr1, *fptr2;
char filename[100], c;
printf("Enter the filename to open for reading \n");
scanf("%s", filename);
// Open one file for reading
fptr1 = fopen(filename, "r");
if (fptr1 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
printf("Enter the filename to open for writing \n");
scanf("%s", filename);
// Open another file for writing
fptr2 = fopen(filename, "w");
if (fptr2 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
// Read contents from file
c = fgetc(fptr1);
while (c != EOF)
{
fputc(c, fptr2);
c = fgetc(fptr1);
}
printf("\nContents copied to %s", filename);
fclose(fptr1);
fclose(fptr2);
return 0;
}
Solved By : Hetvi Boricha - IT (GEC Bhavnagar)