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

Part b Practice Programs v1

The document contains a series of C programming exercises designed for students at B.N.M. Institute of Technology. It includes tasks such as swapping numbers, finding the largest of three numbers, calculating electricity bills, implementing various algorithms (like bubble sort and binary search), and working with strings and structures. Each exercise is accompanied by sample code and instructions for execution.

Uploaded by

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

Part b Practice Programs v1

The document contains a series of C programming exercises designed for students at B.N.M. Institute of Technology. It includes tasks such as swapping numbers, finding the largest of three numbers, calculating electricity bills, implementing various algorithms (like bubble sort and binary search), and working with strings and structures. Each exercise is accompanied by sample code and instructions for execution.

Uploaded by

Raj Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

B.N.M.

Institute of Technology
An Autonomous Institution under VTU
Department of Computer Science and Engineering

PRACTICE PROGRAMS

1. Write a C Program for the following.


a. To Swap Two Numbers using temporary variable.
b. To Swap Two Numbers without using temporary variable.
c. To Swap Two Numbers without using bitwise operators.

// To Swap Two Numbers using temporary variable.


#include <stdio.h>
int main()
{
int num1, num2, temp;
printf(“Enter two numbers:\n “);
scanf(“%d %d”,&num1, &num2);
temp = num1;
num1 = num2;
num2 = temp;
printf(“After swapping: num1 = %d, num2 = %d\n”, num1, num2);
return 0;
}

// To Swap Two Numbers without using temporary variable.

#include <stdio.h>
int main()
{
int num1, num2, temp;
printf(“Enter two numbers: “);
scanf(“%d %d”, &num1, &num2);
printf(“Before Swapping\n NUM1 = %d \n NUM2 = %d\n”, num1, num2);
num1= num1+num2;
num2 = num1- num2;
num1= num1- num2;
printf(“After Swapping: NUM1 = %d \n NUM2 = %d\n”, num1, num2);
return 0;
}

// To Swap Two Numbers without using bitwise operators.

#include<stdio.h>
void main()
{
int a, b;
printf("Enter the values of a and b:");
scanf("%d %d", &a, &b);
printf(“Before Swapping \n Value of A=%d and B=%d before swap",a,b );
a= a^b; //a bitwise XOR with b and store it in first number
b= a^b; // a bitwise XOR with b and store it in second number
a= a^b; // a bitwise XOR with b and store it in first number
printf(“After Swapping \n Value of A=%d and B=%d after swap",a,b);
}

2. Write and Execute a C program to find the largest of three numbers using nested
if else.
#include <stdio.h>
void main()
{
int a,b,c; printf("Enter three numbers separated by space: ");
scanf("%d %d %d", &a,&b,&c);
if (a> b)
{
if (a>c)
printf("The largest number is: %d\n", a);
else printf("The largest number is: %d\n",c);
}
else
{
if (b>c)
printf("The largest number is: %d\n", b);
else printf("The largest number is: %d\n", c);
}
}

3. Write a C program to input electricity unit charge and calculate the total
electricity bill according to the given condition:
• For first 50 units Rs. 0.50/unit
• For next 100 units Rs. 0.75/unit
• For next 100 units Rs. 1.20/unit
• For unit above 250 Rs. 1.50/unit
An additional surcharge of 20% is added to the bill.

PROGRAM:
/** * C program to calculate total electricity bill */
#include <stdio.h>
int main()
{
int unit;
float amt, total_amt, sur_charge;

/* Input unit consumed from user */


printf("Enter total units consumed: ");
scanf("%d", &unit);
/* Calculate electricity bill according to given conditions */
if(unit <= 50)
{
amt = unit * 0.50;
}
else if(unit <= 150)
{
amt = 25 + ((unit-50) * 0.75);
}
else if(unit <= 250)
{
amt = 100 + ((unit-150) * 1.20);
}
else
{
amt = 220 + ((unit-250) * 1.50);
}

/* Calculate total electricity bill after adding surcharge */


sur_charge = amt * 0.20;
total_amt = amt + sur_charge;
printf("Electricity Bill = Rs. %.2f", total_amt);
return 0;
}

4. Write a C program using menu-driven approach to perform addition, subtraction,


multiplication and division using a switch-case statement. Take care of the divide
by zero error by checking for a zero denominator. Include an option to exit the
program.

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

int choice;
float num1, num2;
while(1)
{

printf("\nMenu:\n");
printf("1. Add\n");
printf("2. Subtract\n");
printf("3. Multiply\n");
printf("4. Divide\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
if (choice == 5)
{

printf("Exiting...\n");
break; // Exit the loop and terminate the program
}

printf("Enter two numbers: ");


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

switch (choice)
{
case 1:
printf("Result: %.2f\n", num1 + num2);
break;
case 2:
printf("Result: %.2f\n", num1 - num2);
break;
case 3:
printf("Result: %.2f\n", num1 * num2);
break;
case 4:
if (num2 != 0)
printf("Result: %.2f\n", num1 / num2);
else
printf("Error! Division by zero.\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}

5. Write C program to find a given number is palindrome or not. Display the


appropriate result.

PROGRAM:
#include <stdio.h>
void main()
{

int num, rev= 0, rem, temp;


printf("Enter an integer: ");
scanf("%d", &num);
temp= num;
while (num != 0)
{

rem = num % 10;


rev = rev * 10 + rem;
num /= 10;
}

if (temp== rev)
printf("%d is a palindrome.\n",temp);
else
printf("%d is not a palindrome.\n",temp);
}

6. Write a C program to find whether the given number is prime or not. Display
the appropriate result.

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

int num, i;
int isPrime = 1;
printf("Enter a positive integer: ");
scanf("%d", &num);
for (i = 2; i < num; i++)
{

if (num % i == 0)
{

isPrime = 0;
break;
}

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

7. Write and Execute a C program to generate Fibonacci series for a given number.

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

int i, n, t1 = 0, t2 = 1, fib;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (i = 1; i <= n; ++i)
{

printf("%d, ", t1);


fib = t1 + t2;
t1 = t2;
t2 = fib;
}

8. Write a C program to perform linear search operation for a given key element in the
array.

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

int n, key, found = 0;


int arr[10];
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter %d numbers:\n", n);
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("Enter the number to search: ");
scanf("%d", &key);
for (int i = 0; i < n; i++)
{

if (arr[i] == key)
{

printf("Number found at position %d.\n", i + 1);


found = 1;
break;
}

if (!found)
printf("Number not found in the list.\n");
}

9. Write a C program to implement bubble sort algorithm and display the sorted
elements in a particular order.

#include<stdio.h>
void main()
{
int n,i,a[50],j,temp;
printf("Enter the number of elements in array\n");
scanf("%d",&n);
printf("Enter the elements of array \n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if (a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf("The sorted array is\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}

10. Write a C program to implement binary search algorithm to check whether a


given key element is present in an array.

#include<stdio.h>
void main()
{
int a[100],n,key,i,low,high,mid,flag=0;
printf("Enter size of array \n");
scanf("%d",&n);
printf("Please enter array elements in ascending order \n");
for(i=0; i<n; i++)
{
printf("Enter element \n");
scanf("%d",&a[i]);
}
printf("Enter element to be search \n");
scanf("%d",&key);
low =0;
high =n-1;
while(low <= high)
{
mid=(beg+end)/2;
if(a[mid]== key)
{
printf("Element found at %d position \n",mid + 1);
flag=1;
break;
}
if(a[mid]> key)
high =mid-1;
else
low=mid+1;
}
if(!flag)
printf("No such element is found \n");
}

11. Write and Execute a C program to read and display 2D array and perform
transpose of a (mxn) matrix.

#include <stdio.h>
void main()
{
int m, n, A[20][20], i, j;
printf("Enter the order of matrix (rows and columns): ");
scanf("%d%d", &m, &n);
printf("Enter the elements of the matrix:\n");
for(i = 0; i < m; i++)
for(j = 0; j < n; j++)
scanf("%d", &A[i][j]);
printf("Matrix A:\n");
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
printf("%d\t", A[i][j]);
printf("\n");
}
printf("Transpose of Matrix A:\n");
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
printf("%d\t", A[j][i]);
printf("\n");
}
}

12. Write a C program for the following without using builtin library function.
a. To read and display the string and its length.
b. To copy one string into another string.
c. To concatenate two strings into a third string.

// To read and display the string and its length.

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

char str[10]; int i = 0;


printf("enter the string\n");
scanf("%[^\n]", str);
printf("String: %s\n", str);
while(str[i] != '\0')
i++;
printf("Length of string '%s' is %d", str, i);
}

// To copy one string into another string.

#include<stdio.h>
void main()
{
char str1[30], str2[30];
int i;
printf("Enter string:\n");
gets(str1);
for(i=0;str1[i]!='\0';i++)
{
str2[i] = str1[i];
}
str2[i] = '\0';
printf("Copied string is: %s", str2);
}

// To concatenate two strings into a third string


#include<stdio.h>
void main()
{
char str1[50], str2[50];
int i, len=0;
printf("Enter first string:\n");
gets(str1);
printf("Enter second string:\n");
gets(str2);
/* Calculating length of first string */
for(i=0;str1[i]!='\0';i++)
{
len++;
}
/* Concatenating second string to first string */
for(i=0;str2[i]!='\0';i++)
{
str1[len+i] = str2[i];
}
str1[len+i]='\0';
printf("Concatenated string is: %s", str1);
}
13. Write a C program for the following

a. To find factorial of a number using recursive functions.

b. To find the sum of first N natural numbers using recursive functions.

// C pgm to find factorial of a number using recursive functions.

#include<stdio.h>
int fact(int n); /* Function Definition */

void main()
{
int num, res;
printf("Enter positive integer: ");
scanf("%d",&num);
res = fact(num);
printf("%d! = %d" ,num ,res);
}

int fact(int n) /* Function Definition */


{
if(n <= 0)
{
return(1);
}
else
{
return(n * fact (n-1));
}
}

// C Program to calculate the sum of first N natural numbers using


recursive functions

// using recursion
#include <stdio.h>

int nSum(int n)
{
// base condition to terminate the recursion when N = 0
if (n == 0)
{
return 0;
}

// recursive case / recursive call


int res = n + nSum(n - 1);

return res;
}

void main()
{
int n = 5;
int sum;
printf(“ Enter a Number : \n”);
scanf(“%d”,&n);
sum = nSum(n);
printf("Sum of First %d Natural Numbers: %d", n, sum);
}

14. Write a C program to swap given two numbers using:

i) Call by value
ii) Call by reference

// Call by value
void swapByValue(int a, int b) //call by value
{ int temp;
temp = a;
a = b;
b = temp;
printf("x = %d, y = %d\n", x, y);
}

void main()
{

int x = 10, y = 20;


printf("Before swapByValue: x = %d, y = %d\n", x, y);
swapByValue(x, y);
printf("After swapByValue: x = %d, y = %d\n", x, y);
}

// Call by reference
void swapByRef(int *a, int *b) //call by reference
{

int temp;
temp = *a;
*a = *b;
*b = temp;
}

void main()
{
int x = 10, y = 20;
printf("Before swapByRef: x = %d, y = %d\n", x, y);
swapByRef(&x, &y);
printf("After swapByRef: x = %d, y = %d\n", x, y);
}
15. Write and Execute a C program to define a structure Student with fields studentId,
name and fees. Create an array of 5 students, read their details, and display the
details of all students. Also display the name of student paying highest fees.

#include <stdio.h>
struct Student

{ int studId;

char name[50];
float fees;
};

void main()

{ struct Student s[20];

int n, i, index;
printf("Enter the number of students: ");
scanf("%d", &n);
printf("\nEnter details of %d students:\n", n);
for (i = 0; i < n; i++)
{
printf("\nStudent %d\n", i + 1);
printf("Enter Student ID: ");
scanf("%d", &s[i].studId);
printf("Enter Name: ");

scanf(" %s", s[i].name);

printf("Enter Student FEES: ");

scanf("%f", &s[i].fees);

printf("\nDetails of all students:\n");


printf("Student ID \t Student Name \t Fees \n “);

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


{
printf("%d \t \t %s \t\t %f \n", s[i].studId, s[i].name, s[i].fees);
}

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


{

if( s[i].fees>s[i+1].fees)

12
{
index=i;
}
}

printf(“Name of Student paying highest fees is : %s \n”, s[index].name);


}
NOTE:
LINK TO EXECUTE PROGRAMS USING ONLINE C COMPILER

https://www.programiz.com/c-programming/online-compiler/

13

You might also like