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

Practical Questions

The document contains a series of C programming exercises covering various topics such as defining constants, dynamic memory allocation, string manipulation, recursion, and file handling. Each exercise includes code snippets that demonstrate specific programming concepts like calculating the perimeter of a circle, reversing strings, checking for prime numbers, and performing matrix operations. The exercises are designed to help learners practice and understand fundamental programming techniques.

Uploaded by

kiranwarkad10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views16 pages

Practical Questions

The document contains a series of C programming exercises covering various topics such as defining constants, dynamic memory allocation, string manipulation, recursion, and file handling. Each exercise includes code snippets that demonstrate specific programming concepts like calculating the perimeter of a circle, reversing strings, checking for prime numbers, and performing matrix operations. The exercises are designed to help learners practice and understand fundamental programming techniques.

Uploaded by

kiranwarkad10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

// Q1: Define PI using macro and find perimeter

#include <stdio.h>

#define PI 3.14159

int main() {

float radius, perimeter;

printf("Enter radius: ");

scanf("%f", &radius);

perimeter = 2 * PI * radius;

printf("Perimeter = %.2f\n", perimeter);

return 0;

// Q2: Copy non-zero elements, sum them (Dynamic Memory)

#include <stdio.h>

#include <stdlib.h>

int main() {

int n, i, *a, *b, count = 0, sum = 0;

printf("Enter number of elements: ");

scanf("%d", &n);

a = malloc(n * sizeof(int));

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

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

if (a[i] != 0) count++;

b = malloc(count * sizeof(int));
int j = 0;

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

if (a[i] != 0) {

b[j++] = a[i];

sum += a[i];

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

free(a); free(b);

return 0;

// Q3: Structure for student details

#include <stdio.h>

struct Student {

int roll;

char name[50];

float marks;

};

int main() {

struct Student s;

printf("Enter roll, name and marks: ");

scanf("%d %s %f", &s.roll, s.name, &s.marks);

printf("Roll: %d\nName: %s\nMarks: %.2f\n", s.roll, s.name, s.marks);

return 0;
}

// Q4: Reverse a string

#include <stdio.h>

#include <string.h>

int main() {

char str[100];

printf("Enter string: ");

gets(str);

strrev(str);

printf("Reversed: %s\n", str);

return 0;

// Q5: Length of string without strlen()

#include <stdio.h>

int main() {

char str[100];

int i = 0;

printf("Enter string: ");

gets(str);

while (str[i] != '\0') i++;

printf("Length = %d\n", i);

return 0;

}
// Q6: Count vowels in string

#include <stdio.h>

int main() {

char str[100];

int i, count = 0;

printf("Enter string: ");

gets(str);

for (i = 0; str[i] != '\0'; i++) {

if (str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'||

str[i]=='A'||str[i]=='E'||str[i]=='I'||str[i]=='O'||str[i]=='U') {

count++;

printf("Vowels = %d\n", count);

return 0;

// Q7: Concatenate two strings

#include <stdio.h>

#include <string.h>

int main() {

char a[100], b[100];

gets(a); gets(b);

strcat(a, b);
printf("Result: %s\n", a);

return 0;

// Q8: Compare two strings

#include <stdio.h>

#include <string.h>

int main() {

char a[100], b[100];

gets(a); gets(b);

if (strcmp(a, b) == 0) printf("Equal\n");

else printf("Not equal\n");

return 0;

// Q9: Copy string

#include <stdio.h>

#include <string.h>

int main() {

char a[100], b[100];

gets(a);

strcpy(b, a);

printf("Copied string: %s\n", b);

return 0;

}
// Q10: Palindrome string

#include <stdio.h>

#include <string.h>

int main() {

char str[100], rev[100];

gets(str);

strcpy(rev, str);

strrev(rev);

if (strcmp(str, rev) == 0) printf("Palindrome\n");

else printf("Not Palindrome\n");

return 0;

// Q1 to Q10 are already above

// Q11: Swap using pointers

#include <stdio.h>

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

int temp = *a;

*a = *b;

*b = temp;

int main() {

int x = 10, y = 20;

swap(&x, &y);
printf("x=%d y=%d\n", x, y);

return 0;

// Q12: Pointer to pointer

#include <stdio.h>

int main() {

int a = 5, *p, **pp;

p = &a;

pp = &p;

printf("Value: %d\n", **pp);

return 0;

// Q13: Pointer with array

#include <stdio.h>

int main() {

int a[5] = {1,2,3,4,5}, *p = a, i;

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

printf("%d ", *(p+i));

return 0;

// Q14: Factorial using recursion


#include <stdio.h>

int fact(int n) {

return (n <= 1) ? 1 : n * fact(n - 1);

int main() {

int n;

scanf("%d", &n);

printf("Factorial = %d\n", fact(n));

return 0;

// Q15: Fibonacci using recursion

#include <stdio.h>

int fib(int n) {

if (n <= 1) return n;

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

int main() {

int n;

scanf("%d", &n);

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

printf("%d ", fib(i));

return 0;

}
// Q16: GCD using recursion

#include <stdio.h>

int gcd(int a, int b) {

return b == 0 ? a : gcd(b, a % b);

int main() {

int x, y;

scanf("%d %d", &x, &y);

printf("GCD = %d\n", gcd(x, y));

return 0;

// Q17: Reverse number using recursion

#include <stdio.h>

int rev(int n, int r) {

if (n == 0) return r;

return rev(n/10, r*10 + n%10);

int main() {

int n;

scanf("%d", &n);

printf("Reverse = %d\n", rev(n, 0));

return 0;

}
// Q18: Even or Odd using function

#include <stdio.h>

void check(int n) {

if(n%2 == 0) printf("Even\n");

else printf("Odd\n");

int main() {

int n;

scanf("%d", &n);

check(n);

return 0;

// Q19: Prime check

#include <stdio.h>

int isPrime(int n) {

if (n <= 1) return 0;

for (int i = 2; i <= n/2; i++)

if (n % i == 0) return 0;

return 1;

int main() {

int n;

scanf("%d", &n);

if (isPrime(n)) printf("Prime\n");
else printf("Not Prime\n");

return 0;

// Q20: Armstrong number

#include <stdio.h>

int isArmstrong(int n) {

int temp = n, sum = 0;

while (n > 0) {

int d = n % 10;

sum += d * d * d;

n /= 10;

return temp == sum;

int main() {

int n;

scanf("%d", &n);

if (isArmstrong(n)) printf("Armstrong\n");

else printf("Not Armstrong\n");

return 0;

// Q21: Sum of digits

#include <stdio.h>
int sumDigits(int n) {

int sum = 0;

while (n > 0) {

sum += n % 10;

n /= 10;

return sum;

int main() {

int n;

scanf("%d", &n);

printf("Sum = %d\n", sumDigits(n));

return 0;

// Q22: Count digits

#include <stdio.h>

int countDigits(int n) {

int count = 0;

while (n != 0) {

count++;

n /= 10;

return count;

}
int main() {

int n;

scanf("%d", &n);

printf("Digits = %d\n", countDigits(n));

return 0;

// Q23: Swap using function

// (Same as Q11)

// Q24: Sort array ascending

#include <stdio.h>

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

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

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

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

int t = a[i]; a[i] = a[j]; a[j] = t;

int main() {

int a[100], n;

scanf("%d", &n);

for (int i = 0; i < n; i++) scanf("%d", &a[i]);

sort(a, n);

for (int i = 0; i < n; i++) printf("%d ", a[i]);


return 0;

// Q25: Matrix addition

#include <stdio.h>

int main() {

int a[2][2], b[2][2], c[2][2];

for (int i=0;i<2;i++) for (int j=0;j<2;j++) scanf("%d", &a[i][j]);

for (int i=0;i<2;i++) for (int j=0;j<2;j++) scanf("%d", &b[i][j]);

for (int i=0;i<2;i++) for (int j=0;j<2;j++) c[i][j] = a[i][j] + b[i][j];

for (int i=0;i<2;i++) {

for (int j=0;j<2;j++) printf("%d ", c[i][j]);

printf("\n");

return 0;

// Q26: Matrix multiplication

#include <stdio.h>

int main() {

int a[2][2], b[2][2], c[2][2] = {0};

for (int i=0;i<2;i++) for (int j=0;j<2;j++) scanf("%d", &a[i][j]);

for (int i=0;i<2;i++) for (int j=0;j<2;j++) scanf("%d", &b[i][j]);

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

for (int j=0;j<2;j++)


for (int k=0;k<2;k++)

c[i][j] += a[i][k] * b[k][j];

for (int i=0;i<2;i++) {

for (int j=0;j<2;j++) printf("%d ", c[i][j]);

printf("\n");

return 0;

// Q27: Sum of diagonal

#include <stdio.h>

int main() {

int a[2][2], sum = 0;

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

for (int j=0;j<2;j++) {

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

if (i == j) sum += a[i][j];

printf("Diagonal sum = %d\n", sum);

return 0;

// Q28: File read/write

#include <stdio.h>

int main() {
FILE *f = fopen("data.txt", "w");

fprintf(f, "Hello file!\n");

fclose(f);

f = fopen("data.txt", "r");

char ch;

while ((ch = fgetc(f)) != EOF)

putchar(ch);

fclose(f);

return 0;

// Q29: Command line arguments

#include <stdio.h>

int main(int argc, char *argv[]) {

printf("Arg 1 = %s\n", argv[1]);

return 0;

You might also like