0% found this document useful (0 votes)
6 views8 pages

4 Marks Questions CP

contains 4 marks questions for the same

Uploaded by

vyaspoorva1
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
0% found this document useful (0 votes)
6 views8 pages

4 Marks Questions CP

contains 4 marks questions for the same

Uploaded by

vyaspoorva1
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
You are on page 1/ 8

4 Marks Questions:

25. What is an algorithm? Write an algorithm to check whether given number is odd or even?

An algorithm is a step-by-step procedure or a set of rules used to solve a specific problem or


perform a specific task. It is a well-defined sequence of instructions that take some input and
produce the desired output.

Algorithm to Check Whether a Given Number is Odd or Even:

Step 1: Start
Step 2: Input a number (let's call it num)
Step 3: Divide the number by 2 and check the remainder
Step 4:
If num % 2 == 0, then
Print "The number is Even"
Else
Print "The number is Odd"
Step 5: End

26. What is the need of algorithm and mention properties of algorithms.

Algorithms are essential in computer science and everyday problem-solving because they:

1. Provide a clear step-by-step solution to a problem.

2. Ensure efficiency by optimizing time and resources.

3. Eliminate ambiguity, making complex tasks easier to understand and implement.

4. Enable automation through programming.

Properties of a Good Algorithm:

1. Finiteness:
The algorithm must terminate after a finite number of steps.

2. Definiteness:
Each step must be clear and unambiguous.

3. Input:
The algorithm should have zero or more well-defined inputs.

4. Output:
It should produce at least one output (result).

5. Effectiveness:
Each operation should be basic enough to be performed exactly and in a finite amount of
time.
27. Write a program to calculate and display the sum of first n natural number using while
loop.

#include <stdio.h>

int main() {

int n, i = 1, sum = 0;

clrscr();

printf("Enter a positive integer: ");

scanf("%d", &n);

while(i <= n) {

sum += i;

i++;

printf("Sum of first %d natural numbers is: %d\n", n, sum);

getch();

28. Write a program in C to display n numbers of natural numbers and their sum.

Same program as above

29. Explain all the storage classes in C with any one example

There are four storage classes in C:

(a) Automatic storage class- Storage: Memory.

Default value: An unpredictable value, often called a garbage value.

Scope: Local to the block in which the variable is defined.

Life: Till the control remains within the block in which the

variable is defined.

(b) Register storage class- Storage: CPU registers.

Default value: Garbage value.

Scope: Local to the block in which the variable is defined.


Life: Till the control remains within the block in which the

variable is defined.

(c) Static storage class- Storage: Memory.

Default value: Zero.

Scope: Local to the block in which the variable is defined.

Life: Value of the variable persists between different

function calls.

(d) External storage class- Storage: Memory.

Default value: Zero.

Scope: Global.

Life: As long as the program’s execution doesn’t come to

an end.

30. Write a program to check if the user entered number is divisible by 10 or not.

#include <stdio.h>

int main() {

int num;

clrscr();

printf("Enter a number: ");

scanf("%d", &num);

if (num % 10 == 0) {

printf("The number %d is divisible by 10.\n", num);

} else {

printf("The number %d is NOT divisible by 10.\n", num);

getch();

}
31. Explain different storage classes in c.

Answer above

32. Discover 4 differentiation between String and Character.

Aspect Character (char) String (char[] or char*)

A single alphabet, digit, or A sequence (array) of characters ending with


1. Definition
symbol \0

2. Size Takes 1 byte Takes multiple bytes (length + 1 for \0)

3. Declaration char ch = 'A'; char str[] = "Hello"; or char *str = "Hello";

4.Representation Enclosed in single quotes 'A' Enclosed in double quotes "Hello"

33. Write a program to find fibonnaci series upto n number using function.

#include <stdio.h>

void printFibonacci(int n) {

int a = 0, b = 1, next, i;

printf("Fibonacci series up to %d terms:\n", n);

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

printf("%d ", a);

next = a + b;

a = b;

b = next;

printf("\n");

int main() {

int n;

clrscr();

printf("Enter the number of terms: ");

scanf("%d", &n);

printFibonacci(n);
getch();

34. Write a program to sort number of an array in ascending order using user defined function.

#include <stdio.h>

void sortArray(int arr[], int n) {

int i, j, temp;

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

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

if (arr[i] > arr[j]) {

// Swap arr[i] and arr[j]

temp = arr[i];

arr[i] = arr[j];

arr[j] = temp; } } } }

int main() {

int arr[100], n, i;

clrscr();

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

scanf("%d", &n);

printf("Enter %d elements:\n", n);

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

scanf("%d", &arr[i]);

sortArray(arr, n);

printf("Sorted array in ascending order:\n");

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

printf("%d ", arr[i]);

printf("\n");

getch(); }
36. Write a program to display information of 10 books using array of structure.

37. Write a program to display information of 10 students using array of structure.

38. Provide an example demonstrating the declaration and initialization of a structure named
“Point”

, Also display the coordinates.

Hint : P(x,y) In mathematics a point is having two coordinates i.e. x and y.

#include <stdio.h>

struct Point { int x; int y;

};

int main() {

struct Point p1 = {10, 20};

printf("The point is: P(%d, %d)\n", p1.x, p1.y);

getch();

39. Differentiate between call by value and call by reference.

Aspect Call by Value Call by Reference

Passes the address (reference) of the


Definition Passes a copy of the actual variable
variable

Effect on Original
Does not change original variable Can modify the original variable
Data

Simple data changes, when original When changes to original data are
Used for
must stay safe required

Function Argument Normal variable (e.g., int x) Pointer (e.g., int *x)

Example Function
change(a); change(&a);
Call

Efficiency Slower for large data (makes copies) Faster for large data (no copies)
40. Differentiate between Static memory allocation and Dynamic memory allocation.

Aspect Static Memory Allocation Dynamic Memory Allocation

Memory is allocated at compile


Definition Memory is allocated at runtime
time

Fixed (cannot be changed during


Memory Size Flexible (can be changed during execution)
execution)

Allocation Uses data type declaration (e.g., Uses memory management functions
Method int arr[10];) (malloc(), calloc(), etc.)

Memory
Allocated in stack Allocated in heap
Location

Deallocation Done automatically by compiler Must be done manually using free()

Example int a[5]; int *a = (int*)malloc(5 * sizeof(int));

41. Write a program to swap two numbers using pointer.

#include <stdio.h>

void swap(int *a, int *b) {

int temp;

temp = *a;

*a = *b;

*b = temp;

int main() {

int num1, num2;

clrscr();

printf("Enter two numbers: ");

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

printf("\nBefore swapping: num1 = %d, num2 = %d\n", num1, num2);

swap(&num1, &num2);

printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);

getch(); }

You might also like