Part b Practice Programs v1
Part b Practice Programs v1
Institute of Technology
An Autonomous Institution under VTU
Department of Computer Science and Engineering
PRACTICE PROGRAMS
#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;
}
#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;
#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
}
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");
}
PROGRAM:
#include <stdio.h>
void main()
{
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)
{
8. Write a C program to perform linear search operation for a given key element in the
array.
#include <stdio.h>
void main()
{
if (arr[i] == key)
{
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]);
}
#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.
#include <stdio.h>
void main()
{
#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);
}
#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);
}
// using recursion
#include <stdio.h>
int nSum(int n)
{
// base condition to terminate the recursion when N = 0
if (n == 0)
{
return 0;
}
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);
}
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()
{
// 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()
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("%f", &s[i].fees);
if( s[i].fees>s[i+1].fees)
12
{
index=i;
}
}
https://www.programiz.com/c-programming/online-compiler/
13