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

Practical Assignment-28

The document contains 22 C programming practice problems involving arrays, loops, functions, pointers, structures and recursion. The problems cover basic concepts like printing "Hello World", arithmetic operators, data input-output, prime number checking, swapping values, as well as more advanced topics such as sorting, searching, recursion, multidimensional arrays, string manipulation etc. Solutions to each problem are provided in the form of short C code snippets.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Practical Assignment-28

The document contains 22 C programming practice problems involving arrays, loops, functions, pointers, structures and recursion. The problems cover basic concepts like printing "Hello World", arithmetic operators, data input-output, prime number checking, swapping values, as well as more advanced topics such as sorting, searching, recursion, multidimensional arrays, string manipulation etc. Solutions to each problem are provided in the form of short C code snippets.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Name – Vishal Kulkarni Subject - DSA

MCA – DS

Practice Assignment

1. Write a C program to print “Hello World”.

#include <stdio.h> int


main() { prin ("Hello
World\n");
return 0;
}

2. Write a C program to implement arithme c operators.

#include <stdio.h>

int main() { int num1, num2;


int sum, difference, product;
float quo ent; int remainder;

// Input two numbers prin


("Enter the first number: ");

scanf("%d", &num1); prin ("Enter

the second number: ");


scanf("%d", &num2);

// Perform arithme c opera ons sum


= num1 + num2;
difference = num1 - num2;
product = num1 * num2;

// Check for division by zero


if (num2 != 0) {
quo ent = (float)num1 / num2; // Cas ng to float for accurate division
remainder = num1 % num2;
} else {

prin ("Division by zero is not allowed.\n");


return 1;
}

// Output the results


prin ("Sum: %d\n", sum);
prin ("Difference: %d\n", difference);
prin ("Product: %d\n", product);
prin ("Quo ent: %.2f\n", quo ent);
("Remainder: %d\n",
prin remainder);

return 0;
}
3. Write a C program that accepts the salary and age from the user and displays the
same on the screen as output.]

#include <stdio.h>

int main() {
float salary;
int age;
prin ("Enter your salary: ");
scanf("%f", &salary);

prin ("Enter your age: ");


scanf("%d", &age);

// Display the input values


prin ("You entered a salary of $%.2f and an age of %d years.\n", salary, age);

return 0;
}

4. Write a C program to check whether number is Prime or Not.

#include<stdio.h> int main(){ int

n,i,m=0,flag=0; prin ("Enter the number

to check prime:"); scanf("%d",&n);

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

if(n%i==0)

prin ("Number is not prime");

flag=1;

break;

}
if(flag==0) prin

("Number is prime");

return 0;

5. Write a C program to swap two number

#include<stdio.h> int main()

int a=10, b=20; prin ("Before

swap a=%d b=%d",a,b); a=a+b;

b=a-b; a=a-b;

prin ("\nA er swap a=%d b=%d",a,b);

return 0;

6. Write a C program to find simple

interest #include<stdio.h> int main()

{ float P , R

, T , SI ;

P =34000; R =30; T = 5; SI =

(P*R*T)/100; prin ("\n\n Simple

Interest is : %f", SI); return (0);

7. Write a C program to check whether

number is posi ve, nega ve or zero

#include<stdio.h> int main()

{
int num; scanf("%d",&num); if(num

== 0) prin ("Neither posi ve nor nega

ve"); else if(num < 0) prin ("Nega

ve");

else prin ("Posi

ve"); return 0;

8. Write a C program to find largest among three

numbers #include <stdio.h> int main() { double n1, n2,

n3; prin ("Enter three different numbers: ");

scanf("%lf %lf %lf", &n1, &n2, &n3); if (n1 >= n2

&& n1 >= n3) prin ("%.2f is the largest number.",

n1); if (n2 >= n1 && n2 >= n3) prin ("%.2f is the

largest number.", n2); if (n3 >= n1 && n3 >= n2)

prin ("%.2f is the largest number.", n3); return 0;

9. Write a C program to find factorial of given number

#include <stdio.h> int main() {

int n, i;

unsigned long long fact =

1; prin ("Enter an integer:

"); scanf("%d", &n);

if (n < 0)

prin ("Error! Factorial of a nega ve number doesn't exist.");

else { for (i = 1; i

<= n; ++i) { fact

*= i;

}
prin ("Factorial of %d = %llu", n, fact);

return 0;

10. Write a C program to LCM of 2

numbers #include <stdio.h> int main() {

int n1, n2, i, gcd, lcm;

prin ("Enter two posi ve integers: ");

scanf("%d %d", &n1, &n2); // loop to

find the GCD for (i = 1; i <= n1 && i

<= n2; ++i) { // check if i is a factor

of both integers if (n1 % i == 0 &&

n2 % i == 0)

gcd = i;

lcm = (n1 * n2) / gcd;

prin ("The LCM of two numbers %d and %d is %d.", n1, n2, lcm);

return 0;

11. Write a C program to print Fibonacci series

#include<stdio.h> int main()

int n1=0,n2=1,n3,i,number; prin

("Enter the number of elements:");

scanf("%d",&number); prin ("\n%d

%d",n1,n2);//prin ng 0 and 1

for(i=2;i<number;++i
{

n3=n1+n2;

prin (" %d",n3);

n1=n2;

n2=n3;

return 0;

12. Write a C program to reverse a number (389). #include

<stdio.h> int main() { int num, reversedNum = 0; prin

("Enter a number: "); scanf("%d", &num);

while (num != 0) { int digit = num %

10; reversedNum = reversedNum * 10 +

digit; num /= 10;

prin ("Reversed number: %d\n", reversedNum);

return 0;

13. Write a C program to check whether a number is

palindrome or not #include<stdio.h> int main()

int n,r,sum=0,temp; prin

("enter the number=");

scanf("%d",&n);

temp=n; while(n>0)

{
r=n%10;

sum=(sum*10)+r;

n=n/10;

if(temp==sum) prin

("palindrome number ");

else

prin ("not palindrome"); return

0;

14. Write a C program to check whether a number is Armstrong or not

#include<stdio.h> int main()

int n,r,sum=0,temp; prin

("enter the number=");

scanf("%d",&n);

temp=n; while(n>0)

r=n%10;

sum=sum+(r*r*r);

n=n/10;

if(temp==sum) prin

("armstrong number ");

else

prin ("not armstrong number"); return

0;

}
15. Write a C Program to Find the Sum of Natural Numbers using

Recursion #include <stdio.h> int addNumbers(int n); int main() { int

num; prin ("Enter a posi ve integer: "); scanf("%d", &num); prin ("Sum

= %d", addNumbers(num));

return 0;

int addNumbers(int n) {

if (n != 0) return n +

addNumbers(n - 1); else

return n;

16. Write a C Program to Calculate the Factorial of a Number Using

Recursion #include<stdio.h> long int mul plyNumbers(int n);

int main() {

int n;

prin ("Enter a posi ve integer: "); scanf("%d",&n);

prin ("Factorial of %d = %ld", n, mul plyNumbers(n));

return 0;

long int mul plyNumbers(int n) {

if (n>=1) return n*mul

plyNumbers(n-1);

else

return 1;

17. Write a C Program to Reverse a Stack using Recursion

#include <stdio.h>

#define MAXSIZE 7
#define TRUE

1 #define

FALSE 0

struct Stack {

int top;

int array[MAXSIZE];

} st;

void ini alize() {

st.top = -1;

} int isFull() {

if(st.top >= MAXSIZE-

1) return TRUE;

else return FALSE;

int isEmpty() {

if(st.top == -1)

return TRUE;

else return

FALSE;

void push(int num) {

if (isFull()) prin

("Stack is Full...\n");

else { st.array[st.top +

1] = num; st.top++;

}
int pop() { if (isEmpty())

prin ("Stack is Empty...\n");

else {

st.top = st.top - 1;

return st.array[st.top+1];

void printStack(){

if(!isEmpty()){ int

temp = pop();

printStack(); prin

(" %d ", temp);

push( temp);

void insertAtBo om(int item) {

if (isEmpty()) {

push(item); } else {

int top = pop();

insertAtBo om(item);

push(top);

void reverse() { if

(!isEmpty()) { int top

= pop(); reverse();

insertAtBo om(top);

}
}

int getSize(){

return st.top+1;

int main() { ini alize(st);

push(1); push(2); push(3);

push(4); push(5); prin

("Original Stack\n");

printStack(); reverse();

prin ("\nReversed Stack\n");

printStack(); return 0;

18. Write a C Program to Print a 2-Dimen onal Array #include<stdio.h>

int main(){ int i=0,j=0; int

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

}}; for(i=0;i<4;i++){

for(j=0;j<3;j++){ prin ("arr[%d] [%d]

= %d \n",i,j,arr[i][j]);

}//end of j

}//end of i

return 0;

19. Write a C Program to Find the Maximum and Minimum in an Array

#include <stdio.h>

#include <conio.h>

int main()

{
int a[1000],i,n,min,max; prin

("Enter size of the array : ");

scanf("%d",&n); prin ("Enter

elements in array : "); for(i=0;

i<n; i++)

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

min=max=a[0];

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

if(min>a[i])

min=a[i];

if(max<a[i])

max=a[i];

prin ("minimum of array is : %d",min);

prin ("\nmaximum of array is : %d",max);

return 0;

20. Write a C Program to Calculate the Average of All the Elements Present in

an Array #include<stdio.h> int main() { int arr[10] =

{2,3,4,54,65,34,23,34,123,80};

int sum=0, average;

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

sum += arr[i];

average = sum/10; prin ("The average of given

numbers : %d", average);


return 0;

21. Write a C Program to Sort the Elements of an Array in Descending Order

#include <stdio.h> int main()

//Ini alize array int arr[] = {5, 2,

8, 7, 1}; int temp = 0; int length

= sizeof(arr)/sizeof(arr[0]); prin

("Elements of original array: \n");

for (int i = 0; i < length; i++) { prin

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

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


for (int j = i+1; j < length; j++) {

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

temp = arr[i]; arr[i] =

arr[j]; arr[j] = temp;

prin ("\n");

prin ("Elements of array sorted in descending order: \n");

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

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

return 0;

22. Write a C Program to Find Common Array Elements


#include<stdio.h>

#include<conio.h>

int main()

int a[10], b[10], c[10], i, j, k=0, x,

count; prin ("Enter 10 elements for array

A: "); for(i=0; i<10; i++)

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

elements for array B: "); for(i=0; i<10;

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

i<10; i++)

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

if(a[i]==b[j])

count = 0;
for(x=0; x<k; x++)

if(a[i]==c[x])

count++;

}
if(count==0)

c[k] = a[i];

k++;

prin ("\nArray C (Contains Common Elements from two given Array):\n");


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

prin ("%d ", c[i]);

getch(); return 0;

23. Write a C Program to Copy All the Elements of One Array to Another

Array #include <stdio.h> int main()

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

length = sizeof(arr1)/sizeof(arr1[0]);

int arr2[length]; for (int i = 0; i <

length; i++) { arr2[i] = arr1[i];

prin ("Elements of original array: \n");

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

prin ("%d ", arr1[i]);

prin ("\n"); prin ("Elements of

new array: \n"); for (int i = 0; i <

length; i++) { prin ("%d ",

arr2[i]);

return 0;

24. Write a C Program to check if the string is palindrome or not

#include <stdio.h>

#include <string.h>

int main()

{
char str[] = { "madam" }; int l = 0;

int h = strlen(str) - 1; while (h > l) { if

(str[l++] != str[h--]) { prin ("%s

is not a palindrome\n", str);

return 0;

prin ("%s is a palindrome\n", str); return 0;

25. How to Return a Pointer from a Func on in C

#include <stdio.h>

#include < me.h>

int * getRandom( )

{ sta c int r[10];

int i;

srand( (unsigned) me( NULL ) );

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

{ r[i] = rand();

prin ("%d\n", r[i] );

return r;

int main ()

{ int *p;

int i;

p = getRandom();

for ( i = 0; i < 10; i++ ) { prin

("*(p + [%d]) : %d\n", i, *(p + i) );

}
return 0;

26. C Program to Check if a String is a Palindrome using Pointers

#include <stdio.h> void

isPalindrome(char* string)

char *ptr, *rev; ptr =

string; while (*ptr !=

'\0') {

++ptr;

--ptr;

for (rev = string; ptr >= rev;) {

if (*ptr == *rev) {

--ptr;

rev++;

} else

break;

if (rev > ptr)

prin ("String is Palindrome");

else prin ("String is not a

Palindrome");

int main()

char str[1000] =

"madam";
isPalindrome(str);

return 0;

27. C Program to Sort an Array using

Pointers #include <stdio.h> void sort(int n,

int* ptr)
{

int i, j, t;

// Sort the numbers using pointers

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

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

if (*(ptr + j) < *(ptr + i)) {

t = *(ptr + i);

*(ptr + i) = *(ptr + j);

*(ptr + j) = t;

// print the numbers

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

prin ("%d ", *(ptr + i));

int main()

int n = 5; int arr[] =

{ 0, 23, 14, 12, 9 }; sort(n,

arr); return 0;

}
28. C Program to Store Informa on of employee using

Structure #include <stdio.h> struct Employee

{ int id, age,

salary;

char name[30], designa on[30], department[30];

};

int main()

struct Employee e; prin ("Enter the

id of the Employee: "); scanf("%d",

&e.id);

prin ("Enter the age of the Employee: ");

scanf("%d", &e.age);

prin ("Enter the name of the Employee: ");

getchar();

fgets(e.name, 30, stdin);

prin ("Enter the designa on of the Employee: ");

fgets(e.designa on, 30, stdin);

prin ("Enter the department of the Employee: ");

fgets(e.department, 30, stdin);

prin ("Enter the salary of the Employee: ");

scanf("%d", &e.salary);

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


prin ("Employee Id: %d\n", e.id);

prin ("Employee Name: %s", e.name);

prin ("Employee age: %d\n", e.age);

prin ("Employee designa on: %s", e.designa on);


prin ("Employee department: %s", e.department);
prin ("Employee salary: %d\n", e.salary);

return 0;

29. C Program to Store employee Records as Structures and Sort them by Name

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

struct Student {

char*

name; int

id; char age;

};

int comparator(const void* p, const void* q)

return strcmp(((struct Student*)p)->name,

((struct Student*)q)->name);

int main()

int i = 0, n = 5; struct

Student arr[n]; // Get

the students data


arr[0].id = 1;

arr[0].name = "vishal";

arr[0].age = 21;

arr[1].id = 2;

arr[1].name =

"shreeyash";

arr[1].age = 21;

arr[2].id = 3;

arr[2].name = "anant";

arr[2].age = 23;

arr[3].id = 4;

arr[3].name = "harshal";

arr[3].age = 23;

arr[4].id = 5;

arr[4].name = "omkar";

arr[4].age = 23;

prin ("Unsorted Student Records:\n");

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

prin ("Id = %d, Name = %s, Age = %d \n", arr[i].id,

arr[i].name, arr[i].age);

// Sort the structure qsort(arr, n,

sizeof(struct Student), comparator); prin

("\n\nStudent Records sorted by Name:\n");


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

prin ("Id = %d, Name = %s, Age = %d \n", arr[i].id,

arr[i].name, arr[i].age);

return 0;

30. Write a C program to store the names and scores of 5 students in a structure array. Sort
the structure array in descending order of scores. Display the top 3 scores

#include <stdio.h>

#include <string.h>

struct Student {

char name[50];

int score;

};
void sortStudentsByScore(struct Student students[], int n) {

struct Student temp;

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

= i + 1; j < n; j++) { if (students[i].score

< students[j].score) { temp =

students[i]; students[i] = students[j];

students[j] = temp;

int main() { int n = 5; //

Number of students struct

Student students[n];
// Input the names and scores of 5 students

for (int i = 0; i < n; i++) { prin ("Enter the

name of student %d: ", i + 1); scanf("%s",

students[i].name); prin ("Enter the score of

student %d: ", i + 1); scanf("%d",

&students[i].score);

sortStudentsByScore(students, n);

// Display the top 3 scores

prin ("\nTop 3 Scores:\n"); for

(int i = 0; i < 3 && i < n; i++) {

prin ("%s - Score: %d\n", students[i].name, students[i].score);

return 0;

You might also like