Labels
- 2-3Tree (1)
- 2-3Tree@ADS(EXP3) (1)
- ADS (6)
- ADS@Exp1 (1)
- ADS@Exp2 (1)
- ADS@Exp3 (1)
- ADSLAB (1)
- AVL TREE (2)
- AVL-Tree@ADS(EXP2) (1)
- B-Tech (1)
- CNS (1)
- cp for area of triangle (1)
- cpexp1a (1)
- CSE (2)
- CSE FOSS (1)
- CUT) (1)
- DAA (1)
- DECIMAL TO BINARY (1)
- FOSS (4)
- FOSS LAB SYLLABUS (1)
- FOSS@EXP2(CAT (1)
- FOSS@EXP3 (1)
- HASHING (2)
- JNTUK@ADS LAB (3)
- JNTUKR13REGULATION (1)
- ONES COMPLEMENT (1)
- PASTE (1)
- R13 FOSS LAB (1)
- SED (FOSS command) (1)
- SHELL (foss COMMAND) (1)
- TWO'S COMPLEMENT (1)
a)
|
Write a C Program to calculate the
area of triangle using the formula
area = ( s (s-a) (s-b)(s-c))1/2 where
s= (a+b+c)/2
|
Program:
[bkrishna@localhost cp1]$ vi areatri.c
#include<stdio.h>
#include<math.h>
main()
{
float a,b,c,s,area;
printf("Enter sides of triangle
(a,b,c values) :");
scanf("%f%f%f",&a,&b,&c);
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("Area of the traingle
(whose sides are %f %f %f) is: %f\n",a,b,c,area);
}
|
|
Output:
[bkrishna@localhost cp1]$ cc areatri.c
-lm
[bkrishna@localhost cp1]$ ./a.out
Enter sides of triangle (a,b,c values)
:3 4 5
Area of the traingle (whose sides are
3.000000 4.000000 5.000000) is: 6.000000
|
b)
|
Write a C program to find the largest
of three numbers using ternary operator.
|
Program:
[bkrishna@localhost cp1]$ vi bignum.c
#include<stdio.h>
void main()
{
int a,b,c,big ;
printf("Enter three numbers :");
scanf("%d%d%d",&a,&b,&c);
big=(a>b)?((a>c)?a:c):((b>c)?b:c);
printf("\nThe biggest number among %d
%d %d is : %d\n",a,b,c,big);
}
|
|
Output:
[bkrishna@localhost cp1]$ cc bignum.c
[bkrishna@localhost cp1]$ ./a.out
Enter three numbers :5 2 -1
The biggest number among 5 2 -1 is : 5
|
c)
|
Write a C
Program to swap two numbers without using a temporary variable.
|
Program:
[bkrishna@localhost cp1]$ vi swap.c
#include<stdio.h>
main()
{
int a,b;
printf("Enter two integers to
swap:");
scanf("%d%d",&a,&b);
printf("Before swaping a=%d b=%d
\n",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("after swaping a=%d b=%d \n", a,b);
}
|
|
Output:
[bkrishna@localhost cp1]$ cc swap.c
[bkrishna@localhost cp1]$ ./a.out
Enter two integers to swap:
7 8
Before swaping a=7 b=8
after swaping a=8 b=7
|
Exe: 2
|
a)
|
2’s complement of a number is obtained
by scanning it from right to left and complementing all the bits after the
first appearance of a 1. Thus 2’s complement of 11100 is 00100. Write a C
program to find the 2’s complement of a binary number.
|
b)
|
Write a C
program to find the roots of a quadratic equation.
|
|
c)
|
Write a C program, which takes two
integer operands and one operator form the user, performs the operation and
then prints the result. (Consider the operators +,-,*, /, % and use Switch Statement)
|
2’s
complement of a number is obtained by scanning it from right to left and
complementing all the bits after the first appearance of a 1. Thus 2’s
complement of 11100 is 00100. Write a C program to find the 2’s complement of
a binary number.
|
|
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int a[10],i,n;
clrscr();
printf("Enter no of bits \n");
scanf("%d",&n);
printf("Enter binary numbers
\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++) {
if(a[i]==0)
a[i]=1;
else
a[i]=0;
}
for(i=n-1;i>=0;i--) {
if(a[i]==0) {
a[i]=1;
break;
}
else {
a[i]=0;
if(a[i-1]==0) {
a[i-1]=1;
break;
}
}
}
printf("The complement form is
\n");
for(i=0;i<n;i++)
printf("%d",a[i]);
getch();
}
|
output:
Enter
no of bits
5
Enter
binary numbers
1
1
1
0
0
The
complement form is
00100
|
Write
a C program to find the roots of a quadratic equation.
|
|
Program:
#include
<stdio.h>
#include
<math.h>
/*
This is needed to use sqrt() function.*/
int
main()
{
float a, b, c, determinant, r1,r2, real,
imag;
printf("Enter coefficients a, b and c:
");
scanf("%f%f%f",&a,&b,&c);
determinant=b*b-4*a*c;
if (determinant>0)
{
r1= (-b+sqrt(determinant))/(2*a);
r2= (-b-sqrt(determinant))/(2*a);
printf("Roots are: %.2f and
%.2f",r1 , r2);
}
else if (determinant==0)
{
r1 = r2 = -b/(2*a);
printf("Roots are: %.2f and
%.2f", r1, r2);
}
else
{
real= -b/(2*a);
imag = sqrt(-determinant)/(2*a);
printf("Roots are: %.2f+%.2fi and
%.2f-%.2fi", real, imag, real, imag);
}
return 0;
}
|
Output:
Enter
coefficients a, b and c: 2.3
4
5.6
Roots
are: -0.87+1.30i and -0.87-1.30i
Enter
coefficients a, b and c: 4
1
0
Roots
are: 0.00 and -0.25
|
Write
a C program, which takes two integer operands and one operator form the user,
performs the operation and then prints the result. (Consider the operators
+,-,*, /, % and use Switch Statement)
|
Program:
[bkrishna@localhost
~]$ vi switch.c
#include<stdio.h>
main()
{
[bkrishna@localhost
~]$ cat switch.c
#include<stdio.h>
main()
{
char
op;
int a,b;
printf("Enter
two operands:");
scanf("%d%d",&a,&b);
printf("\nEnter
operator:");
scanf("
%c",&op);
switch(op)
{
case
'+':printf("\nSum of %d and %d
numbers is: %d",a,b,a+b);
break;
case
'-':printf("\nSubtraction of %d and %d numbers is: %d",a,b,a-b);
break;
case
'*':printf("\nProduct of %d and %d
numbers is: %d",a,b,a*b);
break;
case
'/':printf("\nQuotient of %d and %d numbers is: %d",a,b,a/b);
break;
case
'%':printf("\nReminder of %d and %d numbers is: %d",a,b,a%b);
break;
default:printf("please enter correct
operator");
break;
}
}
|
Output:
[bkrishna@localhost
~]$ cc switch.c
[bkrishna@localhost
~]$ ./a.out
Enter
two operands:4 6
Enter
operator:*
Product
of 4 and 6 numbers is: 24
|
Exe:
3
|
a)
|
Write a C program to find the sum of
individual digits of a positive integer and find the reverse of the given
number.
|
b)
|
A Fibonacci sequence is defined as
follows: the first and second terms in the sequence are 0 and 1. Subsequent
terms are found by adding the preceding two terms in the sequence. Write a C
program to generate the first n terms of the sequence.
|
|
c)
|
Write a C program to generate all the
prime numbers between 1 and n, where n is a value supplied by the user
|
a)
|
Write a C program to find the sum of
individual digits of a positive integer and find the reverse of the given
number.
|
||
Program:
[bkrishna@localhost
cp3]$ vi sumrever.c
#include<stdio.h>
main()
{
int
num,n,sum,reverse,remainder;
printf("Enter
the number:\n");
scanf("%d",&num);
sum=0;
reverse=0;
if(num<0)
printf("\nThe
given number is not valid");
else
{
n=num;
while(num!=0)
{
remainder=num%10;
num=num/10;
sum=sum+remainder;
reverse=reverse*10+remainder;
}
printf("\nSum of individual digits of a
%d is %d",n,sum);
printf("\n%d revers number is
%d",n,reverse);
if(n==reverse)
printf("\n%d
is palandrome number\n",reverse);
else
printf("\n
%d is not a palandrome number\n",n);
}}
|
|||
Output:
|
b)
|
A Fibonacci sequence is defined as
follows: the first and second terms in the sequence are 0 and 1. Subsequent
terms are found by adding the preceding two terms in the sequence. Write a C
program to generate the first n terms of the sequence.
|
Program:
#include<stdio.h>
void
main()
{
int
n,f1=0,f2=1,f3,i;
printf("Enter
range of fibonacci series\n");
scanf("%d",&n);
printf("Fibonacci
series upto %d is: ",n);
if(n==1)
printf("%d \n",f1);
else
if(n==2) printf("%d %d \n",f1,f2);
else
if(n>2)
{
printf("%d
%d ",f1,f2);
i=2;
while(i<=n){
f3=f1+f2;
printf("%d
",f3);
f1=f2;
f2=f3;
i++;
}
}
else
printf("Enter valid number");
}
|
|
Output:
[bkrishna@localhost
cp3]$ cc fib.c
[bkrishna@localhost
cp3]$ ./a.out
Enter
range of fibonacci series
1
Fibonacci
series upto 1 is: 0
[bkrishna@localhost
cp3]$ ./a.out
Enter
range of fibonacci series
2
Fibonacci
series upto 2 is: 0 1
[bkrishna@localhost
cp3]$ ./a.out
Enter
range of fibonacci series
6
Fibonacci series upto 6 is: 0 1 1 2 3 5 8
|
c)
|
Write a C program to generate all the
prime numbers between 1 and n, where n is a value supplied by the user
|
Program:
[bkrishna@localhost
cp3]$ vi prime.c
#include<stdio.h>
#include<stdlib.h>
void main()
{
int
n,i,fact,j;
printf("Enter
the number:");
scanf("%d",&n);
if(n<2)
{
printf("There
are no prime numbers up to the given number");
exit(1);
}
printf("The
prime numbers upto %d are:",n);
for(i=1;i<=n;i++)
{
fact=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
fact++;
}
if(fact==2)
printf("\t%d",i);
}
printf("\n");
}
|
|
Output:
[bkrishna@localhost
cp3]$ cc prime.c
[bkrishna@localhost
cp3]$ ./a.out
Enter
the number:11
The prime numbers upto 11 are: 2
3 5 7
11
|
Exe: 4
|
a)
|
Write a C Program to print the
multiplication table of a given number n up to a given value, where n is
entered by the user.
|
b)
|
Write a C Program to enter a decimal
number, and calculate and display the binary equivalent of that number.
|
|
c)
|
Write a C
Program to check whether the given number is Armstrong number or not.
|
a)
|
Write a C Program to print the
multiplication table of a given number n up to a given value, where n is
entered by the user.
|
Program:
[bkrishna@localhost
cp4]$ vi table.c
#include<stdio.h>
#include<stdlib.h>
main()
{
int i=1,n,r;
printf("Enter
any value:");
scanf("%d",&n);
if(n<0)
{
printf("\nThe
given number is a negative number");
exit(1);
}
printf("\nEnter
the range of the table:");
scanf("%d",&r);
printf("\nThe
multiplication table of %d up to given range is:",n);
while(i<=r)
{
printf("\n%d
* %d = %d",n,i,n*i);
i=i+1;
}
}
|
|
Output:
[bkrishna@localhost cp4]$ cc table.c
[bkrishna@localhost cp4]$ ./a.out
Enter any value:3
Enter the range of the table:7
The multiplication table of 3 up to
given range is:
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18
3 * 7 = 21
|
b)
|
Write a C Program to enter a decimal
number, and calculate and display the binary equivalent of that number.
|
Program:
[bkrishna@localhost cp4]$ vi
decitobin.c
#include<stdio.h>
main()
{
int a[30],n,c=0,m,i=0;
printf("Enter the number:");
scanf("%d",&n);
printf("\nBinary representation
of the %d is: ",n);
while(n>=1)
{
m=n%2;
a[i]=m;
i=i+1;
c=c+1;
n=n/2;
}
for(i=c-1;i>=0;i--)
{
printf("%d ",a[i]);
}
printf("\n");
}
|
|
Output:
[bkrishna@localhost cp4]$ ./a.out
Enter the number:1
Binary representation of the 1 is: 1
[bkrishna@localhost cp4]$ ./a.out
Enter the number:2
Binary representation of the 2 is: 1 0
[bkrishna@localhost cp4]$ ./a.out
Enter the number:3
Binary representation of the 3 is: 1 1
[bkrishna@localhost cp4]$ ./a.out
Enter the number:4
Binary representation of the 4 is: 1 0
0
[bkrishna@localhost cp4]$ ./a.out
Enter the number:10
Binary representation of the 10 is: 1
0 1 0
|
c)
|
Write a C
Program to check whether the given number is Armstrong number or not.
|
Program:
#include<stdio.h>
#include<stdlib.h>
main()
{
int n, m, rem, sum=0;
printf("Enter any positive integer: ");
scanf("%d",&n);
if(n<0)
{
printf("The entered number is
negative number\n");
exit(1);
}
m=n;
while(m!=0)
{
rem=m%10;
sum+=rem*rem*rem;
m=m/10;
}
if(sum==n)
printf("%d is an Armstrong Number \n",n);
else
printf("%d is not an Armstrong Number\n",n);
}
|
|
Output:
[bkrishna@localhost cp4]$ cc amg.c
[bkrishna@localhost cp4]$ ./a.out
Enter any positive integer: 153
153 is an Armstrong Number
[bkrishna@localhost cp4]$ ./a.out
Enter any positive integer: 143
143 is not an
Armstrong Number
|
Exe:
5
|
a)
|
Write a C program to interchange the
largest and smallest numbers in the array.
|
b)
|
Write a C
program to implement a liner search.
|
|
c)
|
Write a C
program to implement binary search
|
a)
|
Write a C program to interchange the
largest and smallest numbers in the array.
|
[bkrishna@localhost cp5]$ vi ils.c
#include<stdio.h>
void main()
{
int
a[10],i,n,min,max,mi,mx,temp;
printf("Enter the array
size:");
scanf("%d",&n);
printf("Enter the elements
of array: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("The array elements are : ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
min=0;
max=0;
for(i=0;i<n;i++) {
if(a[i]<min){ min=a[i];
mi=i;
}
if(a[i]>max){ max=a[i];
mx=i;
}
}
printf("\n smallest number:=%d \n largest number:=%d",a[mi],a[mx]);
temp=a[mi];
a[mi]=a[mx];
a[mx]=temp;
printf("\nAfter interchanging largest & smallest values the
array:\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
}
|
|
[bkrishna@localhost
cp5]$ cc ils.c
[bkrishna@localhost
cp5]$ ./a.out
Enter the
array size:5
Enter the
elements of array: 9 -2 44 -7 3
The array
elements are : 9 -2 44 -7 3
smallest number:=-7
largest number:=44
After
interchanging largest & smallest values the array:
9 -2 -7 44 3
|
b)
|
Write a C
program to implement a liner search.
|
[bkrishna@localhost cp5]$ vi lsearch.c
#include<stdio.h>
main()
{
int a[30],n,i,key,found=0;
printf("Enter number of elements:");
scanf("%d",&n);
printf("Enter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the key to be searched:");
scanf("%d",&key);
for(i=0;i<n;i++)
if(a[i]==key)
{
printf("\n%d Found at %d position\n",key,i);
found=1;
}
if(found==0)
printf("\n%d not found in given list\n");
}
|
|
[bkrishna@localhost
cp5]$ cc lsearch.c
[bkrishna@localhost
cp5]$ ./a.out
Enter number
of elements:5
Enter array
elements:6 7 4 6 3
Enter the key
to be searched:6
6 Found at 0
position
6 Found at 3
position
|
c)
|
Write a C
program to implement binary search
|
[bkrishna@localhost cp5]$ vi bsearch.c
#include<stdio.h>
main()
{
int a[10],i,n,m,c=0,l,u,mid;
printf("Enter the size of an array:");
scanf("%d",&n);
printf("Enter the elements in ascending order:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the number to be search:");
scanf("%d",&m);
l=0,u=n-1;
while(l<=u)
{
mid=(l+u)/2;
if(m==a[mid])
{
c=1;
printf("\n %d found at %d
position\n",m,mid);
break;
}
else if(m<a[mid])
{
u=mid-1;
}
else
l=mid+1;
}
if(c==0)
printf("%d is not found\n",m);
}
|
|
[bkrishna@localhost
cp5]$ cc bsearch.c
[bkrishna@localhost
cp5]$ ./a.out
Enter the size
of an array:5
Enter the
elements in ascending order:2 4 5 6 7
Enter the
number to be search:6
6 found at 3 position
|
Exe:
6
|
a)
|
Write a C
program to implement sorting of an array of elements
|
b)
|
Write a C program to input two m x n
matrices, check the compatibility and perform addition and multiplication of
them
|
a)
|
Write a C
program to implement sorting of an array of elements
|
Program:
[bkrishna@localhost cp6]$ cat
sortinga.c
#include<stdio.h>
#include<stdlib.h>
main()
{
int a[20],n,temp,i,j;
printf("Enter the number of
terms:\n");
scanf("%d",&n);
printf("Enter the elements of
array:\n");
for(i=0;i<n;i++)
scanf("%d", &a[i]);
printf("Array elements before
sorting \n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
for(i=0;i<n-1;i++)
for(j=0;j<n-1;j++)
if(a[j]>a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
printf("\nArray elements after
sorting \n:");
for(i=0;i<n;i++)
printf("%d ",a[i]);
}
|
|
Output:
[bkrishna@localhost cp6]$ cc
sortinga.c
[bkrishna@localhost cp6]$ ./a.out
Enter the number of terms:
5
Enter the elements of array:
2 8 1 -7 7
Array elements before sorting
2
8 1 -7 7
Array elements after sorting
:-7
1 2 7 8
|
b)
|
Write a C program to input two m x n
matrices, check the compatibility and perform addition and multiplication of
them
|
Program:
[bkrishna@localhost cp6]$ vi matrix.c
#include<stdio.h>
void main()
{
int
ch,i,j,m,n,p,q,k,r1,c1,r2,c2,a[10][10],b[10][10],c[10][10];
printf("for addition/
multiplication of two matrix first read two matrices A & B");
printf("Enter size of matrix A
(rows and columns)");
scanf("%d%d",&r1,&c1);
printf("Enter elements of matrix
A:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
}
printf("Enter size of matrix B
(rows and columns)");
scanf("%d%d",&r2,&c2);
printf("Enter elements of matrix
B:\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);
}
printf("\n =====Matrix
A=====\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%5d",a[i][j]);
printf("\n");
}
printf("\n =====Matrix
B=====\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
printf("%5d",b[i][j]);
printf("\n");
}
printf("\nMENU");
printf("\n[1]Addition");
printf("\n[2]Multiplication");
printf("\n[0]Exit");
printf("\nEnter your
choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
if((r1!=r2) || (c1!=c2))
{
printf("Addition of two matrices
not possible because for addition both matrices size must be equal");
}
else{
printf("\n =====Matrix Addition=====\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%5d",a[i][j]+b[i][j]);
printf("\n");
}
}
break;
case 2:
if(c1==r2)
{
printf("matrices can be
multiplied\n");
printf("resultant matrix is
%d*%d\n",r1,c2);
printf("\n =====Matrix
Multiplication=====\n");
for(i=0;i<r1;++i)
for(j=0;j<c2;++j)
{
c[i][j]=0;
for(k=0;k<c1;++k)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
printf("Resultant of two
matrices:\n");
write_matrix(c,r1,c2);
}
else
{
printf("Matrices cannot be
multiplied.");
}
break;
case 0:
printf("\n Choice
Terminated");
exit(0);
break;
default: printf("\n Invalid
Choice");
break;
}
}
/*Function to write the matrix*/
int write_matrix(int a[10][10],int
m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%5d",a[i][j]);
printf("\n");
}
return 0;
}
|
Output:
[bkrishna@localhost cp6]$ cc matrix.c
matrix.c: In function âmainâ:
matrix.c:85:1: warning: incompatible
implicit declaration of built-in function âexitâ
[bkrishna@localhost cp6]$ ./a.out
for addition/ multiplication of two
matrix first read two matrices A & B
Enter size of matrix A (rows and
columns)2 2
Enter elements of matrix A:
2 3
4 5
Enter size of matrix B (rows and columns)2 2
Enter elements of matrix B:
5 6
7 8
=====Matrix A=====
2 3
4 5
=====Matrix B=====
5 6
7 8
MENU
[1]Addition
[2]Multiplication
[0]Exit
Enter your choice:
1
=====Matrix Addition=====
7 9
11 13
[bkrishna@localhost cp6]$
[bkrishna@localhost cp6]$ ./a.out
for addition/ multiplication of two
matrix first read two matrices A & B
Enter size of matrix A (rows and
columns)2 2
Enter elements of matrix A:
2 3
4 5
Enter size of matrix B (rows and columns)2 2
Enter elements of matrix B:
2 3
4 5
=====Matrix A=====
2 3
4 5
=====Matrix B=====
2 3
4 5
MENU
[1]Addition
[2]Multiplication
[0]Exit
Enter your choice:
2
matrices can be multiplied
resultant matrix is 2*2
=====Matrix Multiplication=====
Resultant of two matrices:
16 21
28 37
|
Subscribe to:
Posts (Atom)
0 comments:
Post a Comment