0% found this document useful (0 votes)
21 views6 pages

Programming_Questions_and_Solutions

Programing

Uploaded by

Arjun Chaurasia
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
21 views6 pages

Programming_Questions_and_Solutions

Programing

Uploaded by

Arjun Chaurasia
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 6

Q1. What is an Array?

Explain Declaration and Initialization of 1-D and 2-D Array

An array is a collection of elements of the same data type stored at contiguous memory locations.

It allows efficient access and manipulation of data using indices.

Declaration and Initialization of a 1-D Array

- Declaration: data_type array_name[size];

- Initialization: int arr[5] = {1, 2, 3, 4, 5};

Declaration and Initialization of a 2-D Array

- Declaration: data_type array_name[rows][columns];

- Initialization:

int matrix[2][3] = {

{1, 2, 3},

{4, 5, 6}

};

Q2. Program to Find Largest and Smallest Number in an Array

#include <stdio.h>

int main() {

int arr[100], n, i, largest, smallest;

printf("Enter number of elements: ");

scanf("%d", &n);

printf("Enter elements: ");

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

scanf("%d", &arr[i]);
largest = smallest = arr[0];

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

if(arr[i] > largest)

largest = arr[i];

if(arr[i] < smallest)

smallest = arr[i];

printf("Largest: %d\n", largest);

printf("Smallest: %d\n", smallest);

return 0;

Q3. Program to Find Sum of Each Row in a Matrix

#include <stdio.h>

int main() {

int matrix[10][10], rows, cols, i, j;

printf("Enter rows and columns: ");

scanf("%d %d", &rows, &cols);

printf("Enter elements of matrix:\n");

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

for(j = 0; j < cols; j++)

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

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

int sum = 0;

for(j = 0; j < cols; j++)


sum += matrix[i][j];

printf("Sum of row %d: %d\n", i + 1, sum);

return 0;

Q4. What is a Pointer? Write a C Program to Perform Arithmetic Operations Using Pointers

Pointers are variables that store the memory address of another variable.

#include <stdio.h>

int main() {

int a, b, *p1, *p2;

printf("Enter two numbers: ");

scanf("%d %d", &a, &b);

p1 = &a;

p2 = &b;

printf("Sum: %d\n", *p1 + *p2);

printf("Difference: %d\n", *p1 - *p2);

printf("Product: %d\n", (*p1) * (*p2));

printf("Quotient: %d\n", *p1 / *p2);

return 0;

Q5. Explain the Relationship Between Array and Pointer and Write a C Program to Find Sum of

Array Using Pointer

- The name of an array acts as a pointer to the first element.


- Example: If int arr[5] = {1, 2, 3, 4, 5};, then arr is equivalent to &arr[0].

#include <stdio.h>

int main() {

int arr[100], n, *ptr, sum = 0;

printf("Enter number of elements: ");

scanf("%d", &n);

printf("Enter elements: ");

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

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

ptr = arr;

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

sum += *(ptr + i);

printf("Sum of array: %d\n", sum);

return 0;

Q6. Write a C Program to Find Factorial Using Recursion

#include <stdio.h>

int factorial(int n) {

if(n == 0 || n == 1)

return 1;

return n * factorial(n - 1);

}
int main() {

int n;

printf("Enter a number: ");

scanf("%d", &n);

printf("Factorial of %d: %d\n", n, factorial(n));

return 0;

Q7. Write a C Program to Find nth Term of Fibonacci Series Using Recursion

#include <stdio.h>

int fibonacci(int n) {

if(n == 0)

return 0;

if(n == 1)

return 1;

return fibonacci(n - 1) + fibonacci(n - 2);

int main() {

int n;

printf("Enter the term: ");

scanf("%d", &n);

printf("Fibonacci term %d: %d\n", n, fibonacci(n));

return 0;

Q8. What is a Structure? Display Employee Details Using Structure


A structure is a user-defined data type that allows grouping variables of different data types under

one name.

#include <stdio.h>

struct Employee {

int id;

char name[50];

float salary;

};

int main() {

struct Employee emp;

printf("Enter Employee ID: ");

scanf("%d", &emp.id);

printf("Enter Employee Name: ");

scanf("%s", emp.name);

printf("Enter Employee Salary: ");

scanf("%f", &emp.salary);

printf("\nEmployee Details:\n");

printf("ID: %d\n", emp.id);

printf("Name: %s\n", emp.name);

printf("Salary: %.2f\n", emp.salary);

return 0;

You might also like