Bca C Lab Manual 2024-28
Bca C Lab Manual 2024-28
Semester-1
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,z;
printf("Enter the first number : ");
scanf("%d",&x);
printf("Enter the second number : ");
scanf("%d",&y);
printf("Enter the third number : ");
scanf("%d",&z);
if((x>y)&& (x>z))
printf("x = %d is greatest number",x);
if((y>x)&& (y>z))
printf("y = %d is greatest number",y);
if((z>x)&& (z>y))
printf("z = %d is greatest number",z);
return 0;
}
case '-':
z=x-y;
printf("Substraction= %d",z);
break;
case '*':
z=x*y;
printf("Multiplication= %d",z);
break;
case '/':
z=x/y;
printf("Division=%d",z);
break;
case '%':
z=x%y;
printf("Reminder= %d",z);
break;
default :
printf(" invalide opretor select");
}
return (0);
}
Q3. Write a Program to print the sum and product of digits of an integer.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,dig, sum=0,pro=1;
printf("\nEnter any pair integer number :");
scanf("%d",&n);
while(n>0)
{
dig=n%10;
sum+=dig;
pro*=dig;
n=n/10;
}
printf("\nSUM of all Digits is : %d",sum);
printf("\nPRODUCT of all digits: %d",pro);
return (0);
}
#include <stdio.h>
#include <conio.h>
void main()
{
int n, rev = 0, remainder=0;
clrscr();
printf("Enter any pair Integer Number: ");
scanf("%d", &n);
while(n != 0)
{
remainder = n%10;
rev= rev*10 + remainder;
n /= 10;
}
printf("\n Reversed Number is := %d",rev);
return (0);
}
Q5. Write a Program to compute the sum of the first N terms of the following
Sum of Series 1 + 1/2 + 1/3 + 1/4 + ... + 1/N
#include <stdio.h>
#include <conio.h>
double sum(int n);
void main()
{
int n= 5;
printf("Sum is %f", sum(n));
return (0);
}
double sum(int n)
{
double i, s = 0.0;
for (i = 1; i <= n; i++)
s = s + 1/i;
return s;
}
Q6. Write a Program to compute the sum of the first n terms of the following
series S =1-2+3-4+5-6+7...N
#include<stdio.h>
#include<conio.h>
void main()
{
int no,i,sum=0;
printf("Enter a number : ");
scanf("%d",&no);
for(i = 1; i <= no; i++)
{
sum += (i % 2 == 0) ? -i : i;
}
printf("Sum of Series is = %d",sum);
return (0);
}
Q7. Write a function that checks whether a given string is Palindrome or not.
Use this function to find whether the string entered by user is Palindrome or
not.
#include <stdio.h>
#include <string.h>
void main()
{
char input[100], reverse[100];
printf("\nEnter the string : ");
scanf("%s", input);
strcpy(reverse, input);
strrev(reverse);
if(strcmp(input, reverse) == 0 )
printf("\n%s : is a palindrome string", input);
else
printf("\n%s : is not a palindrome string", input);
printf(" \n\n Press any Key to Quit.....");
return (0);
}
Q8. Write a function to find whether a given no. is prime or not. Use the same to
generate the prime numbers less than 100.
#include <stdio.h>
#include <conio.h>
void main()
{
int i,n;
printf("Enter a range value of N: ");
scanf("%d",&n);
printf("\nAll prime numbers are from 1 to %d :\n\n ",n);
for(i=1;i<=n;i++)
{
if(checkPrime(i))
printf("%d \t",i);
}
return (0);
}
#include<conio.h>
#include<stdio.h>
void main()
{
int x, n, sum;
printf("\nEnter the any Number : ");
scanf("%d", &n);
x = 1;
printf("\nThe Factor's of %d are : ", n);
while(x <= n)
{
if(n % x == 0)
printf("%d,", x);
x++;
}
return (0);
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
printf("Enter the First Number: ");
scanf("%d", &a);
printf("Enter the Second Number: ");
scanf("%d", &b);
c=a;
a=b;
b=c;
printf("After swapping: a = %d, \n b = %d", a, b );
return (0);
}
Q11. WAP to print a triangle of stars as follows (take number of lines from user
#include <stdio.h>
#include <conio.h>
void main()
{
int no,i, j, k;
printf("enter value of number of line to take : ");
scanf("%d", &no);
for(i=1;i<=no;i++)
{
for(j=i;j<no;j++)
{
printf(" ");
}
for(k=1;k<(i*2);k++)
{
printf("*");
}
printf("\n");
}
return (0);
}
Q12. Write a Program to perform following actions on an array entered by the user:
a) Print the even-valued elements
b) Print the odd-valued elements
c) Calculate and print the sum and average of the elements of array
d) Print the maximum and minimum element of array
e) Remove the duplicates from the array
f) Print the array in reverse order
The program should present a menu to the user and ask for one of the options. The
menu should also include options to re-enter array and to quit the program.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,choice,minimum,max,location = 1,j=0,k=0,no,n,sum=0,avg=0;
int arr[20],even[20],odd[20],b[20];
printf("\n\nEnter choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\n\nA.Print the even-valued elements");
printf("\nEnter size of array: ");
scanf("%d",&no);
printf("\nEnter any %d elements in Array: ",no);
for(i=0; i<no;i++)
{
scanf("%d",&arr[i]);
}
for(i=0; i<no;i++)
{
if(arr[i]%2==0)
{
even[j]=arr[i];
j++;
}
}
printf("\nEven Elements: ");
for(i=0; i<j ;i++)
{
printf("%d ",even[i]);
}
break;
case 2:
printf("\n\nB.Print the odd-valued elements");
printf("\n\nEnter size of array: ");
scanf("%d",&no);
printf("\n\nEnter any %d elements in Array: ",no);
for(i=0; i<no;i++)
{
scanf("%d",&arr[i]);
}
for(i=0; i<no;i++)
{
if(arr[i]%2!=0)
{
odd[k]=arr[i];
k++;
}
}
printf("\nOdd Elements: ");
for(i=0; i<k; i++)
{
printf("%d ",odd[i]);
}
break;
case 3:
printf("\n\nC. Calculate and print the sum and average of the elements
of array");
printf("\n\nHow many elements you want to enter: ");
scanf("%d",&n);
printf("\nEnter any %d elements in Array: ",n);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("Sum of Array Elements: ");
for(i=0;i<n;i++)
{
sum=sum+arr[i];
avg=sum/arr[i];
}
for(i=0;i<n;i++)
{
}
printf("sum %d \n avg=%d ",sum,avg);
break;
case 4:
printf("\n D. Print the MIN and MAX elements in array\n");
printf("Enter the number of elements in array\n");
scanf("%d",&no);
printf("Enter %d integers\n", no);
for ( i = 0 ; i < no ; i++ )
scanf("%d", &arr[i]);
minimum = arr[0];
for ( i = 1 ; i< no ; i++ )
{
if ( arr[i] < minimum )
{
minimum = arr[i];
location = i+1;
}
}
printf("Minimum element is present at location %d and it's value is
%d.\n", location, minimum);
max = arr[0];
for ( i = 1 ; i< no ; i++ )
{
if ( arr[i] > max)
{
max = arr[i];
location = i+1;
}
}
printf("Max element is present at location %d and it's value is
%d.\n", location, max);
break;
case 5:
printf("\n\nE. Remove the duplicates from the array");
printf("\n\nEnter size of array: ");
scanf("%d",&no);
printf("\n\nEnter any %d elements in array: ",no);
for(i=0;i<no;i++)
{
scanf("%d",&arr[i]);
}
printf(" Remove Duplicate Elements are: ");
for(i=0; i<no; i++)
{
for(j=i+1;j<no;j++)
{
if(arr[i]==arr[j])
{
printf("%d\n",arr[i]);
}
}
}break;
case 6:
printf("\n \n F. Print the array in reverse order\n");
printf("How many elements you want to enter: ");
scanf("%d",&no);
printf("Enter any %d elements in Array: ",no);
for(i=0; i<no ;i++)
{
scanf("%d",&arr[i]);
}
printf("Reverse of Array: ");
for(i=no-1,j=0; i>=0;i--,j++)
{
b[i]=arr[j];
}
for(i=0; i<no ;i++)
{
printf("%d ",b[i]);
}
break;
default:
printf("invalide options");
}
return (0);}
Q13.WAP that prints a table indicating the number of occurrences of each
alphabet in the text entered as command line arguments.
#include <stdio.h>
#include <string.h>
int count(char * string, char * toSearch); // Function declaration
void main()
{
char string[100];
char toSearch[100];
int occur;
printf("Enter any string: ");
gets(string);
printf("Enter charactor to search occurrences: ");
gets(toSearch);
occur = count(string, toSearch);
printf("Total charactor occurrences of '%s': %d times.", toSearch,
occur);
return (0);
}
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,*b,*a,temp;
printf("Enter the first number : ");
scanf("%d",&x);
printf("Enter the second number : ");
scanf("%d",&y);
#include<stdio.h>
#include<conio.h>
void swap(int *a, int *b);
void main()
{
int m,n;
printf("Enter first number : ");
scanf("%d",&m);
printf("Enter second number : ");
scanf("%d",&n);
printf("\n before swap values : m = %d and n = %d",m,n);
swap(&m, &n); // calling swap function by reference
return (0);
}
void swap(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
printf("\n after swap values: m = %d and n = %d", *a, *b);
}
Q16. Write a program which takes the radius of a circle as input from the user,
passes it to another function that computes the area and the circumference of
the circle and displays the value of area and circumference from the main( )
function.
#include<stdio.h>
#include<conio.h>
const float PI = 3.141;
float area(float radius);
float circum(float radius);
void main()
{
float radius;
printf("Enter radius: ");
scanf("%f", &radius);
printf("Area : %.3f\n", area(radius));
printf("Circumference: %.3f\n", circum(radius));
return (0);
}
// return area of a circle
float area(float radius)
{
return PI * radius * radius;
}
// return circumference of a circle
float circum(float radius)
{
return 2 * PI * radius;
}
Q17. Write a program to find sum of n elements entered by the user. To write
this program, allocate memory dynamically using malloc() / calloc() functions
or new operator.
#include<stdio.h>
#include<stdlib.h>
void main()
{
int *ptr,sum=0,i;
// Allocate memory Equivalent to 1 integer
ptr = (int *)malloc(sizeof(int));
for(i=0;i<4;i++)
{
printf("Enter number : ");
scanf("%d",ptr);
sum = sum + (*ptr);
}
printf("Sum of a & b = %d",sum);
return (0);
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
void charAddress(char *string);
void stringLwr(char *s);
void stringUpr(char *s);
int strlength(char *s);
void main()
{
int choice, length, count=0,c=0;
char loc[40], str[50],s[50];
char str1[50], str2[50],i,j;
printf("Enter choice: from 1 to 9=> ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nShow address of each character in string\n\n");
printf("Enter the string: ");
scanf("%s",loc);
charAddress(loc);
break;
case 2:
printf("\n\nConcatenate two strings without using strcat()
function\n\n");
printf("\nEnter first string: ");
scanf("%s",str1);
printf("\nEnter second string: ");
scanf("%s",str2);
for(i=0; str1[i]!='\0'; ++i);
for(j=0; str2[j]!='\0'; ++j, ++i)
{
str1[i]=str2[j];
}
str1[i]='\0';
printf("\nConcatenate two strings are : %s",str1);
break;
case 3:
printf("\n\nConcatenate two strings using strcat() function");
printf("\n\nEnter first string: ");
scanf("%s",str1);
printf("\nEnter second string: ");
scanf("%s",str2);
printf("\nConcatenate two strings are : %s",strcat(str1,str2));
break;
case 4:
printf("\n\nCompare two strings\n");
printf("Enter the First String :");
scanf("%s",str1);
printf("Enter the Second String:");
scanf("%s",str2);
if (strcmp(str1,str2) == 0)
printf("The strings are equal.\n");
else
printf("The strings are not equal.\n");
break;
case 5:
printf("\n\nCalculate length of the string (use pointers)\n\n:");
printf("Enter any string : ");
scanf("%s",str);
length=strlength(str);
printf("The length of String :%s is :%d",str,length);
break;
case 6:
printf("Convert all uppercase characters to lowercase:\n\n");
printf("Enter any string : ");
scanf("%s",str);
stringLwr(str);
printf("\nString is Lowercase : %s",str);
break;
case 7:
printf("Convert all lowercase characters to uppercase\n\n");
printf("Enter any string : ");
scanf("%s",str);
stringUpr(str);
printf("\nString is Uppercase : %s",str);
break;
case 8:
printf("\n\nCalculate number of vowels\n\n");
printf("Enter any string : ");
scanf("%s",s);
while (s[c] != '\0')
{
if (s[c] == 'a' || s[c] == 'A' || s[c] == 'e' || s[c] == 'E' || s[c] == 'i' ||
s[c] == 'I' ||
s[c] =='o' || s[c]=='O' || s[c] == 'u' || s[c] == 'U')
count++;
c++;
}
printf("\nNumber of vowels in the string: %d", count);
break;
case 9:
printf("\nReverse the string:\n\n");
printf("Enter any string: ");
scanf("%s",str);
printf("\n The Reverse String is : %s",strrev(str));
break;
default:
printf("Not a valid options...");
}
return (0);
}
Q19. Given two ordered arrays of integers, write a program to merge the two-
arrays to get an ordered array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],b[10],c[20],i,j,temp;
printf("Enter Elements in 1st Array: ");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
printf("Enter Elements in 2nd Array: ");
for(i=0;i<5;i++)
{
scanf("%d",&b[i]);
}
printf("\nElements of Array After Merge:\n\n ");
for(i=0;i<5;i++)
{
c[i]=a[i];
c[i+5]=b[i];
}
for(i=0;i<10;i++)
{
printf(" %d",c[i]);
}
for(i=0;i<10;i++)
{
for(j=0;j<10-i-1;j++)
{
if(c[j]>c[j+1])
{
temp=c[j];
c[j]=c[j+1];
c[j+1]=temp;
}
}
}
printf(" \nAfter merge data soting\n\n");
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
int ch;
FILE *fp,*fq;
fp=fopen("demo.txt","r");
fq=fopen("output.txt","w");
if(fp==NULL||fq==NULL)
printf("File does not exist..");
else
while((ch=fgetc(fp))!=EOF)
{
fputc(ch,fq);
}
printf("File copied successfully done");
return (0);
}