BCA C Practical
BCA C Practical
BCA C Practical
2)#include <stdio.h>
#include <math.h>
void main()
{
int a,b,c,d;
float x1,x2;
Sample Output:
#include <stdio.h>
#include <string.h>
printf("\n");
}
//main program
int main()
{
unsigned int number=12345;
//function calling
extractDigits(number);
return 0;
}
Output
12345
#include <stdio.h>
int main() {
while (n != 0) {
remainder = n % 10;
reverse = reverse * 10 + remainder;
n /= 10;
}
return 0;
}
Output
Enter an integer: 2345
Reversed number = 5432
#include<stdio.h>
int main()
{
int n,sum=0,m;
printf("Enter a number:");
scanf("%d",&n);
while(n>0)
{
m=n%10;
sum=sum+m;
n=n/10;
}
printf("Sum is=%d",sum);
return 0;
}
Output:
Enter a number:654
Sum is=15
Enter a number:123
Sum is=6
#include <stdio.h>
int main() {
int n, i;
printf("Enter an integer: ");
scanf("%d", &n);
for (i = 1; i <= 10; ++i) {
printf("%d * %d = %d \n", n, i, n * i);
}
return 0;
}
Output
Enter an integer: 9
9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90
#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("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)
printf("armstrong number ");
else
printf("not armstrong number");
return 0;
}
Output:
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
return 0;
}
Output
int main ()
{
// declare integer variables
int n, temp, rev = 0, digit, sum_of_digits = 0;
3)
float x1 = n;
// Main function
int main()
{
float n = 90;
cal_sin(n);
return 0;
}
Output:
int main(void)
{
int num, choice, base;
while(1)
{
printf("Select conversion: \n\n");
printf("1. Decimal to binary. \n");
printf("2. Decimal to octal. \n");
printf("3. Decimal to hexadecimal. \n");
printf("4. Exit. \n");
switch(choice)
{
case 1:
base = 2;
break;
case 2:
base = 8;
break;
case 3:
base = 16;
break;
case 4:
printf("Exiting ...");
exit(1);
default:
printf("Invalid choice.\n\n");
continue;
}
convert_to_x_base(num, base);
printf("\n\n");
}
// base condition
if (num == 0)
{
return;
}
else
{
rem = num % base; // get the rightmost digit
convert_to_x_base(num/base, base); // recursive call
if(base == 16 && rem >= 10)
{
printf("%c", rem+55);
}
else
{
printf("%d", rem);
}
}
}
Select conversion:
1. Decimal to binary.
2. Decimal to octal.
3. Decimal to hexadecimal.
4. Exit.
Select conversion:
1. Decimal to binary.
2. Decimal to octal.
3. Decimal to hexadecimal.
4. Exit.
1. Decimal to binary.
2. Decimal to octal.
3. Decimal to hexadecimal.
4. Exit.
5)
6)GCD
#include <stdio.h>
int hcf(int n1, int n2);
int main() {
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("G.C.D of %d and %d is %d.", n1, n2, hcf(n1, n2));
return 0;
}
FIBINOCI SERIES
#include<stdio.h>
int main()
{
int first=0, second=1, i, n, sum=0;
printf("Enter the number of terms: ");
scanf("%d",&n);
//accepting the terms
printf("Fibonacci Series:");
for(i=0 ; i<n ; i++)
{
if(i <= 1)
{
sum=i;
}
//to print 0 and 1
else
{
sum=first + second;
first=second;
second=sum;
//to calculate the remaining terms.
//value of first and second changes as new term is printed.
}
printf(" %d",sum)
}
return 0;
}
7)
So I'm coding in C, and I need to come up with code that will take n numbers from the user, and find
their minimum, maximum, average, and sum of squares for for their values. So far I have the average and
sum of squares portion, but the minimum and maximum is biting me.
Keep in mind I'm at a very rudimentary level, and I have not reached arrays yet. All I know are logical
operators, functions, loops, and the use of the stdlib.h, math.h, and stdio.h libraries.
This is what I have so far. The average function gave me a lot of problems when I tried to put float and
double during compiling, so multiply it by a 1.0 fixed that. I have everything, just the minimum and
maximum. I keep getting the last entry as my maximum, and a 0 for my minimum.
#include<stdio.h>
int main()
{
float average;
int i, n, count=0, sum=0, squaresum=0, num, min, max;
while(count<n)
{
min=0;
max=0;
if(num>max)
max=num;
if(num<min)
min=num;
scanf_s("%d",&num);
sum = sum+num;
squaresum = squaresum + (num*num);
count++;
}
average = 1.0*sum/n;
return(0);
}
8)
printf("\n");
}
Output :
1 2 3 4 5 6
Reversed array is
6 5 4 3 2 1
REMOVE DUPLICATE
#include <stdio.h>
#include <conio.h>
int main ()
{
// declare local variables
int arr[20], i, j, k, size;
9)
#include<stdio.h>
#include<stdlib.h>
// menu-driven
do
{
// menu to choose the operation
printf("\nChoose the matrix operation,\n");
printf("----------------------------\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Transpose\n");
printf("5. Exit\n");
printf("----------------------------\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add(a, b, c);
printf("Sum of matrix: \n");
display(c);
break;
case 2:
subtract(a, b, c);
printf("Subtraction of matrix: \n");
display(c);
break;
case 3:
multiply(a, b, c);
printf("Multiplication of matrix: \n");
display(c);
break;
case 4:
printf("Transpose of the first matrix: \n");
transpose(a, c);
display(c);
printf("Transpose of the second matrix: \n");
transpose(b, c);
display(c);
break;
case 5:
printf("Thank You.\n");
exit(0);
default:
printf("Invalid input.\n");
printf("Please enter the correct input.\n");
}
}while(1);
return 0;
}
Output:-
First Matrix:
567
8 9 10
312
Second Matrix:
123
456
789
10)
#include<stdio.h>
#include<string.h>
void concat(char[], char[]);
int main() {
char s1[50], s2[30];
printf("\nEnter String 1 :");
gets(s1);
printf("\nEnter String 2 :");
gets(s2);
concat(s1, s2);
printf("\nConcated string is :%s", s1);
return (0);
}
void concat(char s1[], char s2[]) {
int i, j;
i = strlen(s1);
for (j = 0; s2[j] != '\0'; i++, j++) {
s1[i] = s2[j];
}
s1[i] = '\0';
}
Output
12)#include <stdio.h>
int main()
{
char str[100];//input string with size 100
scanf("%[^~]",&str);//scanf formatting
for(int i=0;str[i]!='\0';i++)
{
if(str[i] == ' ')
{
words++;
}
else if(str[i] == '\n')
{
newline++;
words++;//since with every next line new words start. corner case 1
}
else if(str[i] != ' ' && str[i] != '\n'){
characters++;
}
}
if(characters > 0)//Corner case 2,3.
{
words++;
newline++;
}
printf("Total number of words : %d\n",words);
printf("Total number of lines : %d\n",newline);
printf("Total number of characters : %d\n",characters);
return 0;
}
Input : HELLO STUDENTS
HOW R U
Output : Total number of words : 5
Total number of lines : 2
Total number of characters : 18
13)
#include <stdio.h>
#include <stdlib.h> // For exit()
int main()
{
FILE *fptr;
char filename[100], c;
// Open file
fptr = fopen(filename, "r");
if (fptr == NULL)
{
printf("Cannot open file \n");
exit(0);
}
fclose(fptr);
return 0;
}
OUT PUT
Enter the filename to open
a.txt
/*Contents of a.txt*/