0% found this document useful (0 votes)
54 views22 pages

Anshul c2

The document contains the code submissions of Anshul Lohani for various programming problems. It includes 14 programs with inputs and outputs to perform tasks like swapping numbers using call by value and reference, finding factorial and Fibonacci series using recursion, linear search and bubble sort of arrays, checking if a number is Armstrong, prime or palindrome, calculator using switch case, and printing patterns. The objective is listed before each program.

Uploaded by

Sanjay Bajeli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views22 pages

Anshul c2

The document contains the code submissions of Anshul Lohani for various programming problems. It includes 14 programs with inputs and outputs to perform tasks like swapping numbers using call by value and reference, finding factorial and Fibonacci series using recursion, linear search and bubble sort of arrays, checking if a number is Armstrong, prime or palindrome, calculator using switch case, and printing patterns. The objective is listed before each program.

Uploaded by

Sanjay Bajeli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

NAME – ANSHUL LOHANI

COURSE – BCA
ROLL NO. – 2292020
SECTION – B

OBJECTIVE– Write a program to swap two numbers using

 call by value

INPUT:-
#include<stdio.h>
void swap(int,int);
int main()
{
int a=10,b=20;
printf("Value before swapping: a=%d, b=%d\n",a,b);
swap(a,b);
return 0;
}
void swap(int x,int y)
{
int t;
t=x;
x=y;
y=t;
printf("After swapping(Using Call by Value), values are: a=%d, b=%d",x,y);
}

OUTPUT:-
NAME – ANSHUL LOHANI
COURSE – BCA
ROLL NO. – 2292020
SECTION – B

OBJECTIVE– Write a program to swap two numbers using

 call by reference

INPUT:-

#include<stdio.h>
void swap(int*,int*);
int main()
{
int c=30,d=40;
printf("Values before swapping: c=%d, d=%d\n",c,d);
swap(&c,&d);
return 0;
}
void swap(int*x,int*y)
{
int t;
t=*x;
*x=*y;
*y=t;
printf("After swapping(Using Call by Reference), values are: c=%d, d=%d",*x,*y);
}

OUTPUT:-
NAME – ANSHUL LOHANI
COURSE – BCA
ROLL NO. – 2292020
SECTION – B

OBJECTIVE– Write a program to find the factorial of a given number using recursion

INPUT:-
#include<stdio.h>
long int mult(int n);
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, mult(n));
return 0;
}
long int mult(int n)
{
if (n>=1)
return n*mult(n-1);
else
return 1;
}

OUTPUT:-
NAME – ANSHUL LOHANI
COURSE – BCA
ROLL NO. – 2292020
SECTION – B

OBJECTIVE– Write a program to find the fibonacci series using recursion

INPUT:-
#include<stdio.h>
void Fibonacci(int n){
static int n1=0,n2=1,n3;
if(n>0)
{
n3 = n1 + n2;
n1 = n2;
n2 = n3;
printf("%d ",n3);
Fibonacci(n-1);
}
}
int main()
{
int n;
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("Fibonacci Series: ");
printf("%d %d ",0,1);
Fibonacci(n-2);
return 0;
}

OUTPUT:-
NAME – ANSHUL LOHANI
COURSE – BCA
ROLL NO. – 2292020
SECTION – B

OBJECTIVE– Write a program to search an element of an array using the linear search
technique

INPUT:-
#include <stdio.h>
int main()
{
int arr[10],search,i,n;
printf("Enter the number of elements in the array: ");
scanf("%d",&n);
printf("Enter the elements of the array: ",n);
for (i=0;i<n;i++ )
scanf("%d",&arr[i]);
printf("Enter the element to search: ");
scanf("%d",&search);
for (i=0;i<n;i++)
{
if (arr[i]==search)
{
printf("%d is present at location %d.\n",search,i+1);
break;
}
}
if (i==n)
printf("%d is not present in the array\n",search);
return 0;
}

OUTPUT:-
NAME – ANSHUL LOHANI
COURSE – BCA
ROLL NO. – 2292020
SECTION – B

OBJECTIVE– Write a program to sort the elements of an array using Bubble Sort
INPUT:-
#include<stdio.h>
int main()
{
int arr[100],i,n,d,swap;
printf("Enter the number of elements you want to add in an array: ");
scanf("%d",&n);
printf("Enter the integers of the array: \n",n);
for (i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
for (i=0;i<n-1;i++)
{
for (d=0;d<n-i-1;d++)
{
if (arr[d]>arr[d+1])
{
swap=arr[d];
arr[d]=arr[d+1];
arr[d+1]=swap;
}
}
}
printf("After Sorting: \n");
for (i=0;i<n;i++)
printf("%d ",arr[i]);
return 0;
}
OUTPUT:-
NAME – ANSHUL LOHANI
COURSE – BCA
ROLL NO. – 2292020
SECTION – B

OBJECTIVE– Write a program to find the reverse of an array

INPUT:-

#include <stdio.h>
int main()
{
int arr[10];
int size, i;
printf("Enter the size of the array: ");
scanf("%d", &size);
printf("Enter the elements in array: \n");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}
printf("Array in reverse order: \n");
for(i=size-1;i>=0;i--)
{
printf("%d ",arr[i]);
}
return 0;
}

OUTPUT:-
NAME – ANSHUL LOHANI
COURSE – BCA
ROLL NO. – 2292020
SECTION – B

OBJECTIVE– Write a program to find whether the number is an Armstrong number or not

INPUT:-

#include <stdio.h>
int main()
{
int num,ori,rem,res=0;
printf("Enter a three-digit integer: ");
scanf("%d", &num);
ori=num;

while (ori!= 0)
{
rem=ori%10;
res+=rem*rem*rem;
ori/=10;
}
if (res==num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);
return 0;
}

OUTPUT:-
NAME – ANSHUL LOHANI
COURSE – BCA
ROLL NO. – 2292020
SECTION – B

OBJECTIVE– Write a program to find the Maximum and Minimum element of an array and
then swap them
INPUT:-
#include<stdio.h>
#include<conio.h>
void main()
{ int a[10],n,i,max,min,maxpos,minpos,temp;
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("Enter the elements in array: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
max=a[0];
min=a[0];
maxpos=0;
minpos=0;
for(i=1;i<n;i++)
{ if(a[i]>max)
{ max=a[i];
maxpos=i; }
if(a[i]<min)
{ min=a[i];
minpos=i; }
}
temp=a[maxpos];
a[maxpos]=a[minpos];
a[minpos]=temp;
printf("Maximum element: %d\n",max);
printf("Minimum element: %d\n",min);
printf("Swapping of Maximum and Minimum element: ");
for(i=0;i<n;i++)
{ printf("%d ",a[i]); }
getch(); }

OUTPUT:-
NAME – ANSHUL LOHANI
COURSE – BCA
ROLL NO. – 2292020
SECTION – B
NAME – ANSHUL LOHANI
COURSE – BCA
ROLL NO. – 2292020
SECTION – B

OBJECTIVE– Write a program to convert decimal number into binary number


INPUT:-
#include <stdio.h>
long dec_to_bin(long n)
{
long bin = 0;
long dec = n;
long i = 1;
while (dec > 0)
{
bin += (dec % 2) * i;
dec /= 2;
i *= 10;
}
return bin;
}
int main(void)
{
long n;
printf("Enter a decimal number: ");
scanf("%ld", &n);
printf("Binary equivalent of %ld: %ld\n", n, dec_to_bin(n));
return 0;
}

OUTPUT:-
NAME – ANSHUL LOHANI
COURSE – BCA
ROLL NO. – 2292020
SECTION – B

OBJECTIVE– Write a program to find a year is a leap year or not using logical operators
INPUT:-
#include<stdio.h>
int main()
{
int year;
printf("Enter a Year: ");
scanf("%d", &year);
if( (year%100==0 && year%400==0) || (year%100!=0 && year%4==0) )
{
printf("%d is a Leap Year\n", year);
}
else
{
printf("%d is not a Leap Year\n", year);
}
return 0;
}

OUTPUT:-
NAME – ANSHUL LOHANI
COURSE – BCA
ROLL NO. – 2292020
SECTION – B

OBJECTIVE– Write a program to check whether a number is a Prime Number or not


INPUT:-
#include<stdio.h>
int main()
{
int n,i,f=0;
printf("Enter a positive integer: ");
scanf("%d",&n);
if (n==0||n==1)
f=1;
for(i=2;i<=n/2;++i)
{
if (n%i==0)
{
f=1;
break;
}
}
if (f==0)
printf("%d is a prime number", n);
else
printf("%d is not a prime number", n);
return 0;
}

OUTPUT:-
NAME – ANSHUL LOHANI
COURSE – BCA
ROLL NO. – 2292020
SECTION – B

OBJECTIVE– Write a program to check whether a number is Palindrome number or not


INPUT:-
#include<stdio.h>
int main()
{
int i,p,r,s=0;
printf("Enter any Number: ");
scanf("%d",&p);
for(i=p;i>0;)
{
r=i%10;
s=s*10+r;
i=i/10;
}
if(s==p)
printf("%d is a Palindrome Number",p);
else
printf("%d is not a Palindrome Number",p);
return 0;
}

OUTPUT:-
NAME – ANSHUL LOHANI
COURSE – BCA
ROLL NO. – 2292020
SECTION – B

OBJECTIVE– Write a menu driven program for calculator using switch case
INPUT:-
#include <stdio.h>
int main(){
char ch;
int a,b,calc;
printf("Enter the Operator (+, -, *, /): ");
scanf("%c", &ch);
printf("Enter two Operands: \n");
scanf("%d%d",&a,&b);
switch(ch)
{
case '+':
calc=a+b;
break;
case '-':
calc=a-b;
break;
case '*':
calc=a*b;
break;
case '/':
calc=a/b;
break;
}
printf("Calculation: %d",calc);
return 0;
}

OUTPUT:-
NAME – ANSHUL LOHANI
COURSE – BCA
ROLL NO. – 2292020
SECTION – B

OBJECTIVE– Write a program to print the following patterns:


a: 1 b: 1
12 22
123 333
1234 4444
12345 55555
INPUT:-
#include<stdio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}
printf("\n");
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
}
printf("\n");
}
return 0;
}

OUTPUT:-
NAME – ANSHUL LOHANI
COURSE – BCA
ROLL NO. – 2292020
SECTION – B

OBJECTIVE– Write a program to find sum and average of N natural numbers


INPUT:-
#include<stdio.h>
int main()
{
int n,i,sum=0,avg=0;
printf("Enter the Natural Number: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+i;
avg=sum/n;
}
printf("Sum of N Natural Numbers: %d\n",sum);
printf("Average of N Natural Numbers: %d\n",avg);
return 0;
}
OUTPUT:-
NAME – ANSHUL LOHANI
COURSE – BCA
ROLL NO. – 2292020
SECTION – B

OBJECTIVE– Write a program to find the sum of two matrices


INPUT:-
#include<stdio.h>
int main()
{ int i,j,a[10][10],b[10][10],c[10][10];
int row1,col1,row2,col2,row3,col3;
printf("Enter the rows and columns of the First matrix: ");
scanf("%d%d",&row1,&col1);
printf("Enter the rows and columns of Second matrix: ");
scanf("%d%d",&row2,&col2);
if(row1!=row2 || col1!=col2)
{ printf("You cannot add both the matrices because the entered rows and columns do not
match "); }
printf("Enter the values of First matrix: ");
for(i=0;i<row1;i++)
{ for(j=0;j<col1;j++)
{ scanf("%d",&a[i][j]); } }
printf("Now enter the values of Second matrix: ");
for(i=0;i<row2;i++)
{ for(j=0;j<col2;j++)
{ scanf("%d",&b[i][j]); } }
printf("Sum of the entered two matrices: \n");
for(i=0;i<row2;i++)
{ for(j=0;j<col2;j++)
{ c[i][j]=a[i][j]+b[i][j];
printf("%d",c[i][j]); }
printf("\n"); }
return 0; }
OUTPUT:-
NAME – ANSHUL LOHANI
COURSE – BCA
ROLL NO. – 2292020
SECTION – B

OBJECTIVE– Write a program to find the multiplication of two matrices


INPUT:-
#include<stdio.h>
int main()
{ int i,j,k,a[10][10],b[10][10],c[10][10];
int row1,col1,row2,col2,row3,col3;
printf("Enter the rows and columns of the First matrix: ");
scanf("%d%d",&row1,&col1);
printf("Enter the rows and columns of Second matrix: ");
scanf("%d%d",&row2,&col2);
if(col1!=row2)
{ printf("You cannot multiply both the matrices because the entered rows and columns do not
match "); }
else
{ row3=row1;
col3=col2;
printf("Enter the values of First matrix: ");
for(i=0;i<row1;i++)
{ for(j=0;j<col1;j++)
{ scanf("%d",&a[i][j]); } }
printf("Now enter the values of Second matrix: ");
for(i=0;i<row2;i++) {
for(j=0;j<col2;j++)
{ scanf("%d",&b[i][j]); } }
printf("Multiplication of the entered two matrices: \n");
for(i=0;i<row3;i++)
{ for(j=0;j<col3;j++)
{ int sum=0;
for(k=0; k<row3; k++)
{ sum=sum+a[i][k] * b[k][j];
c[i][j] = sum; }
printf("%d",c[i][j]); }
printf("\n"); } }
return 0; }
OUTPUT:-
NAME – ANSHUL LOHANI
COURSE – BCA
ROLL NO. – 2292020
SECTION – B

OBJECTIVE– Write a program to find the sum and average of an array


INPUT:-

#include<stdio.h>
int main()
{
int n,a[10],i,sum=0,avg=0;
printf("Enter the number of elements you want to add in the arary: ");
scanf("%d",&n);
printf("Enter the elements of the array: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum=sum+a[i];
avg=sum/n;
}
printf("Sum of array: %d\n",sum);
printf("Average of array: %d",avg);
return 0;
}

OUTPUT:-
NAME – ANSHUL LOHANI
COURSE – BCA
ROLL NO. – 2292020
SECTION – B

OBJECTIVE– Write a program to traverse the elements of 1-D Array


INPUT:-

#include<stdio.h>
void main()
{
int i, size;
int arr[]={1, -9, 17, 4, -3};
size=sizeof(arr)/sizeof(arr[0]);
printf("The array elements are: ");
for(i=0;i<size;i++)
{
printf("\narr[%d]= %d", i, arr[i]);
}
}

OUTPUT:-
NAME – ANSHUL LOHANI
COURSE – BCA
ROLL NO. – 2292020
SECTION – B

You might also like