0% found this document useful (0 votes)
2 views

C_Programming_QA

The document provides a comprehensive overview of C programming, including types of operators, program structure, and example programs for various tasks such as calculating area and circumference of a circle, summing integers, and checking for prime numbers. It also covers calculating mean and median for an array and initializing a 2D array with its memory map. Each section includes code snippets to illustrate the concepts discussed.

Uploaded by

sanjaysiva0904
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

C_Programming_QA

The document provides a comprehensive overview of C programming, including types of operators, program structure, and example programs for various tasks such as calculating area and circumference of a circle, summing integers, and checking for prime numbers. It also covers calculating mean and median for an array and initializing a 2D array with its memory map. Each section includes code snippets to illustrate the concepts discussed.

Uploaded by

sanjaysiva0904
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

C Programming Questions and Answers

1) Different Types of Operators in C:


- Arithmetic Operators: +, -, *, /, %
- Relational Operators: ==, !=, >, <, >=, <=
- Logical Operators: &&, ||, !
- Assignment Operators: =, +=, -=, *=, /=
- Increment/Decrement Operators: ++, --
- Bitwise Operators: &, |, ^, ~, <<, >>
- Conditional (Ternary) Operator: ? :
- Sizeof Operator

2) Structure of a C Program:
#include <stdio.h>

int main() {
// Variable declarations
// Code/statements
return 0;
}

3) Develop C Program
i) To find area and circumference of circle

#include <stdio.h>
#define PI 3.14

int main() {
float radius, area, circumference;
printf("Enter radius: ");
scanf("%f", &radius);

area = PI * radius * radius;


circumference = 2 * PI * radius;

printf("Area = %.2f\n", area);


printf("Circumference = %.2f\n", circumference);
return 0;
}

ii) To find sum of 100 integers

#include <stdio.h>

int main() {
int sum = 0;
for(int i = 1; i <= 100; i++) {
sum += i;
}
printf("Sum of 100 integers = %d\n", sum);
return 0;
}

4) Develop C Program
i) To find sum of the digits of a number

#include <stdio.h>

int main() {
int num, sum = 0, digit;
printf("Enter a number: ");
scanf("%d", &num);

while(num > 0) {
digit = num % 10;
sum += digit;
num /= 10;
}

printf("Sum of digits = %d\n", sum);


return 0;
}

ii) To find sum of all odd and even numbers between 1 and 100

#include <stdio.h>

int main() {
int evenSum = 0, oddSum = 0;
for(int i = 1; i <= 100; i++) {
if(i % 2 == 0)
evenSum += i;
else
oddSum += i;
}
printf("Sum of even numbers = %d\n", evenSum);
printf("Sum of odd numbers = %d\n", oddSum);
return 0;
}

iii) To check whether a number is prime or not

#include <stdio.h>

int main() {
int num, isPrime = 1;
printf("Enter a number: ");
scanf("%d", &num);

if(num <= 1) isPrime = 0;


for(int i = 2; i <= num/2; i++) {
if(num % i == 0) {
isPrime = 0;
break;
}
}

if(isPrime)
printf("%d is a prime number.\n", num);
else
printf("%d is not a prime number.\n", num);
return 0;
}

5) Write a C Program to Calculate Mean and Median for an Array


#include <stdio.h>

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


for(int i=0; i<n-1; i++) {
for(int j=i+1; j<n; j++) {
if(arr[i] > arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}

int main() {
int n;
float mean, median;
printf("Enter number of elements: ");
scanf("%d", &n);

int arr[n], sum = 0;


for(int i = 0; i < n; i++) {
printf("Element %d: ", i+1);
scanf("%d", &arr[i]);
sum += arr[i];
}

mean = (float)sum / n;
sort(arr, n);

if(n % 2 == 0)
median = (arr[n/2 - 1] + arr[n/2]) / 2.0;
else
median = arr[n/2];

printf("Mean = %.2f\n", mean);


printf("Median = %.2f\n", median);
return 0;
}

6) Describe with Example


i) Initializing a 2D array

int arr[2][3] = {
{1, 2, 3},
{4, 5, 6}
};

ii) Memory Map of a 2D array

Stored in row-major order in memory:


arr[0][0], arr[0][1], arr[0][2], arr[1][0], arr[1][1], arr[1][2]

For the above array, memory layout looks like:


[1][2][3][4][5][6]

You might also like