Você está na página 1de 36

COMPUTER CONCEPT AND C PROGRAMING

1A)Design,develop and execute a program in c to find and output all the roots
of a given quadratic equation, for non-Zero coefficients.
#include<stdio.h>

/*preprocessor dirictives*/

#include<conio.h>
#include<stdlib.h>
#include<math.h>
void main()

/*Begining of main function*/

{
float a,b,c,root1,root2;

/* local declaration*/

float realp,imagp,d;
clrscr();
printf("Enter the values of A B and C \n");
scanf("%f%f%f",&a,&b,&c);
if (a==0||b==0||c==0)

//All the three inputs should be positive

{
printf("\n roots cannot be determined \n");
exit(1);
}
else
{
d=(b*b)-(4.0*a*c);
/* this block of code used to find the complex roots*/
if(d==0)
{
printf("\n roots are real and equal");
root1=-b/(2.0*a);
root2=-b/(2.0*a);
CSE

Page 1

COMPUTER CONCEPT AND C PROGRAMING


printf("\n root1 = %f",root1);
printf("\n root2 = %f",root2);

}
/* this block of code used to find the equal roots*/
else if(d>0)
{
printf("\n roots are real and distinct \n");
root1=(-b+sqrt((b*b)-(4.0*a*c)))/(2.0*a);
root2=(-b-sqrt((b*b)-(4.0*a*c)))/(2.0*a);
printf("\n root1 = %f",root1);
printf("\n root2 = %f",root2);

}
/* this block of code used to find the distinct roots*/
else
{
printf("\n complex roots");
realp=-b/(2.0*a);
imagp=sqrt(fabs((b*b)-(4.0*a*c)))/(2.0*a); //abs() is used to get only positive
values
printf("\n root1 =%f + i %f",realp,imagp);
printf("\n root2 =%f - i %f",realp,imagp);

}
}
getch();
} /* End of main function */
CSE

Page 2

COMPUTER CONCEPT AND C PROGRAMING

OUTPUT:
1.Enter the values of A B and C
1 2 3
roots are real and equal
root1= -1.000000
root2=-1.000000
2.Enter the values of A B and C
-1 2 3
roots are real and distinct
root1= -1.000000
root2=3.000000
3.Enter the values of A B and C
2 4 6
Complex roots
root1= -1.000000+i 1.414214
root2= -1.000000-i 1.414214
4.Enter the values of A B and C
0 1 2
roots cannot be determined.

CSE

Page 3

COMPUTER CONCEPT AND C PROGRAMING


2A) Design,develop and execute a program in C to implement Euclid's
Algorith to find the GCD and LCM of two integers and to output the results
along with the given integers.
#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,rem,gcd,lcm,a,b;

// Local Variable Declarations

clrscr();
printf("Enter the values for M and N \n");
scanf("%d%d",&m,&n);
a=m;
b=n;
while(n!=0) //While loop to repeat till the value of n is zero
{
rem=m%n;
m=n;
n=rem;
}
gcd=m;
lcm=(a*b)/gcd;
printf("The GCD of %d and %d is %d\n",a,b,gcd);
printf("The LCM of %d and %d is %d\n",a,b,lcm);
getch();
}

CSE

Page 4

COMPUTER CONCEPT AND C PROGRAMING

OUTPUT:
1.Enter the values for M and N
8 4
The GCD of 8 and 4 is 4.
The LCM of 8 and 4 is 8.
2.Enter the values for M and N
24 32
The GCD of 24 and 32 is 8.
The LCM of 24 and 32 is 96.

CSE

Page 5

COMPUTER CONCEPT AND C PROGRAMING


3A) Design,develop and execute a program in C to reverse a given four digit
integer number

and check whether it is a palindrome or not.Output the

given number with suitable message.


#include<stdio.h>
#include<conio.h>
void main()
{
int m,rev,dig,n;

// Local Variable Declaration

clrscr();
printf("Enter the Number to be Reversed:");
scanf("%d",&n);
m=n;
rev=0;
/* This block will reverse the given number */
while(n!=0)

// While loop will repeat till the valus of n becomes Zero

{
dig=n%10;
rev=rev*10+dig;
n=n/10;
}
printf("The given number is %d\n",m);
printf("The reversed number is %d\n",rev);
if(m==rev) // Check the equality of the given number and reversed number
{
printf("The given mumber is palindrome\n");
}
else
CSE

Page 6

COMPUTER CONCEPT AND C PROGRAMING


{
printf("The given number is not a palindrome");
}
getch();
}

OUTPUT:
1.Enter the number to be Reversed
1221
The given number is 1221
The reversed number is 1221
The given number is palindrome.
2.Enter the number to be Reversed
1234
The given number is 1234
The reversed number is 4321
The given number is not a palindrome.

CSE

Page 7

COMPUTER CONCEPT AND C PROGRAMING


4A)Design,develop and execute a program in C to evaluate the given
polynomial f(x)=a4x4+ a3x3+ a2x2+ a1x1+ a0 for given value of x and the
coefficients using Horners method.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],x,n,i=0,res=0;
clrscr();
printf("\n Enter the Degree of the polynomial =");
scanf("%d",&n);
printf("Enter the n+1 cofficients of the polynomial\n");
for(i=0;i<=n;i++)
{
printf("Enter cofficient of x^%d=",i);
scanf("%d",&a[i]);
}
printf("\n Enter value of x=");
scanf("%d",&x);
res=a[n];
for(i=n-1;i>=0;i--)
res=res*x+a[i]; //calculating polynomial using horner's method
printf("\n The value of the polynomial=%d",res);
getch();
}

CSE

Page 8

COMPUTER CONCEPT AND C PROGRAMING


OUTPUT:
1.Enter the degree of the polynomial=5
Enter the n+1 coefficents of the polynomial
Enter cofficient of x^0=1
Enter cofficient of x^1=2
Enter cofficient of x^2=3
Enter cofficient of x^3=4
Enter cofficient of x^4=5
Enter cofficient of x^5=6
Enter value of x=1
The value of the polynomial=21

2.Enter the degree of the polynomial=4


Enter the n+1 coefficents of the polynomial
Enter cofficient of x^0=2
Enter cofficient of x^1=4
Enter cofficient of x^2=6
Enter cofficient of x^3=8
Enter cofficient of x^4=10
Enter value of x=2
The value of the polynomial=258

CSE

Page 9

COMPUTER CONCEPT AND C PROGRAMING


5A) Design,develop and execute a program in C to copy its input to its
output,replacing each string of one or more blanks by a single blank.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main ()
{
char c[50];
int space=0,i;
clrscr();
printf("Enter string with spaces\n");
gets(c);
printf("String with only one space\n");
for(i=0;i<=strlen(c);i++)
{
if(c[i]==' ')
{
if(space==0)
{
space=1;
printf("%c",c[i]);
}

}
else
{
space=0;
CSE

Page 10

COMPUTER CONCEPT AND C PROGRAMING


printf("%c",c[i]);
}

}
getch();
}

OUTPUT:
1.Enter string with spaces
CBIT

CSE

KOLAR

String with only one space


CBIT CSE KOLAR
2.Enter string with spaces
A

String with only one space


AB C D

CSE

Page 11

COMPUTER CONCEPT AND C PROGRAMING


6A)Design,develop and execute a program in C to input N integer numbers in
ascending order into a single dimensional array and perform a binary search
both the given array and the sorted array with suitable headings.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n,low,high,mid,key;
clrscr();
printf("Enter the number of elements:");
scanf("%d",&n);
printf("\n Enter the array elements in ascending order:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n Enter the element to search:");
scanf("%d",&key);
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(a[mid]==key)
{
printf("The element is found at position :%d",mid+1);
getch();
exit(0);
}
CSE

Page 12

COMPUTER CONCEPT AND C PROGRAMING


else if(key>a[mid])
low=mid+1;
else
high=mid-1;
}
printf("\n The element not found in array");
getch();
}

OUTPUT:
1.Enter the number of elements
5
Enter the array elements in ascending order:
10 20 30 40 50
Enter the elements to search:40
The elements is found at position:4

2.Enter the number of elements


6
Enter the array elements in ascending order:
25 30 35 40 45 50
Enter the elements to search:60
The elements not found in array.

CSE

Page 13

COMPUTER CONCEPT AND C PROGRAMING


7A)Design,develop and execute a program in C to input N integer numbers
into a single dimensional array, sort them in ascending order using bubble
sort technique and print both the given array and the sorted array with
suitable headings.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,i,j,temp;
clrscr();
printf("enter n value\n");
scanf("%d",&n);
printf("enter array elements to be sorted\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
CSE

Page 14

COMPUTER CONCEPT AND C PROGRAMING


printf("\nsorted array elements are:\n\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}

OUTPUT:
1.Enter n value
5
Enter array elements to be searched
50 20 45 10 12
Sorted array elements are:
10 12 20 45 50
2.Enter n value
6
Enter array elements to be searched
65 70 90 15 100 10
Sorted array elements are:
10 15 65 70 90 100

CSE

Page 15

COMPUTER CONCEPT AND C PROGRAMING


8A)Design,develop and execute a program in C to compute and print the word
length on the host Machine.
#include<stdio.h>
#include<conio.h>
int wordlength()
{
int n,count=0;
n=~0;
\\printf("\nthe value of n is: %d",n);
while(n!=0)
{
n=n<<1;
count++;
\\printf("\nthe value of n and count is %d and %d",n,count);
}
return count;
}

void main()
{
int c;
clrscr();
c=wordlength();
printf("\n\nWordlength of machine is %d bits",c);
getch();
}

CSE

Page 16

COMPUTER CONCEPT AND C PROGRAMING


OUTPUT:
Word length of machine is 16 bits.

CSE

Page 17

COMPUTER CONCEPT AND C PROGRAMING


9B)Design, develop and execute a program in C to calculate the approximate
value of exp(0.5) using the Taylor Series expansion for the exponential
function. Use the terms in the expansion until the last term is less than the
machine epsilon defined FLT_EPSILON in the header file<float.h>. Also print
the value returned by the Mathematical function exp( ).
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<float.h>
int fact(int k)
{
int i,prod=1;
for(i=1;i<=k;i++)
{
prod*=i;
}
return prod;
}
double userexp(float k)
{
int i;
double sum,term,n;
sum=1;
for(i=1;;i++)
{
term=pow(k,i)/fact(i);
CSE

Page 18

COMPUTER CONCEPT AND C PROGRAMING


if(term<FLT_EPSILON)
break;
sum=sum+term;
}
return sum;
}
void main()
{
float x;
double sum;
x=0.5;
clrscr();
sum=userexp(x);
printf("\n Value after using User defined function\n");
printf("\n e^%f=%lf\n",x,sum);
printf("\n Using library function\n");
printf("\n e^%f=%lf\n",x,exp(x));
getch();
}

OUTPUT:
Value after using User defined function
e^0.5000000=1.648721
Using library function
e^0.5000000=1.648721

CSE

Page 19

COMPUTER CONCEPT AND C PROGRAMING


10B) Design, develop and execute a program in C to read two matrices A (M x
N) and B (P x Q) and compute the product of A and B if the matrices are
compatible for multiplication. The program must print the input matrices and
the resultant matrix with suitable headings and format if the matrices are
compatible for multiplication, otherwise the program must print a suitable
message. (For the purpose of demonstration, the array sizes M, N, P, and Q
can all be less than or equal to 3)

#include<stdio.h>
#include<conio.h>
void read(int a[20][20],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
}
void product(int a[20][20],int b[20][20],int c[20][20],int m,int q,int n)
{
int i,j,k;
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
CSE

Page 20

COMPUTER CONCEPT AND C PROGRAMING


for(k=0;k<n;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
void display(int a[20][20],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
}
void main()
{
int j,k,a[20][20],b[20][20],c[20][20],m,n,p,q;
clrscr();
printf("\nEnter no of rows and columns in A matrix A(M*N):");
scanf("%d%d",&m,&n);
printf("\nEnter no of rows and columns in B Matrix B(p*q):");
scanf("%d%d",&p,&q);
if(n==p)
{
printf("\nEnter the numbers in matrix A");
read(a,m,n);
CSE

Page 21

COMPUTER CONCEPT AND C PROGRAMING


printf("\nEnter the numbers in matrix B");
read(b,p,q);
product(a,b,c,m,q,n);
printf("Matrix A:\n\n");
display(a,m,n);
printf("Matrix B:\n\n");
display(b,p,q);
printf("\nMatrix A*B \n\n");
display(c,m,q);
}
else
{
printf("\n MAtrix cannot be multiplied");
}
getch();
}

CSE

Page 22

COMPUTER CONCEPT AND C PROGRAMING


OUTPUT:
1.Enter the no of rows and columns in A matrix A(m*n):3 3
Enter the no of rows and columns in B matrix B(p*q):3 3
Enter the numbers in matrix A 2 4 3
4 5 0
2 3 4
Enter the numbers in matrix B

6 5 7
0 0 0
4 8 9

Matrix A:
2 4 3
4 5 0
2 3 4
Matrix B:
6 5 7
0 0 0
4 8 9
Matrix (A*B):
24 34 41
24 20 28
28 42 50
2.Enter the no of rows and columns in A matrix A(m*n):3 2
Enter the no of rows and columns in B matrix B(p*q):4 5
Matrix cannot be multiplied.

CSE

Page 23

COMPUTER CONCEPT AND C PROGRAMING


11B)Design, develop and execute a parallel program in C to add, elementwise,
two one-dimensional arrays A and B of N integer elements and store the result
in another one-dimensional array C of N integer elements.
#include<stdio.h>
#include<omp.h>
int main()
{
int a[100],b[100],c[100],i, n;
printf("\nenter the number of elements");
scanf("%d",&n);
printf("\nEnter the element of 1st array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
a[i]=1;
printf("enter the elements of 2nd array");
for(i=0;i<n;i++)
scanf("%d",&b[i]);
b[i]=2;
printf("\nthe contents of array A\n");
for(i=0;i<n;i++)
{
printf("\na[%d]=%d",i,a[i]);
}
printf("\nthe contents of array B\n");
for(i=0;i<n;i++)
{
printf("\nb[%d]=%d",i,b[i]);
}
#pragma omp parallel for shared(c)
for(i=0;i<n;i++)
{
#pragma omp critical (c_lock)
c[i]=a[i]+b[i];
printf("\nc[%d]=%d,thread id=%d\n",i,c[i],omp_get_thread_num());
}
printf("\nthe new array is====");
for(i=0;i<n;i++)
CSE

Page 24

COMPUTER CONCEPT AND C PROGRAMING


{
printf("%d
}
printf("\n");
}

" , c[i]);

OUTPUT:
Enter the number of elements 5
Enter the element of 1st array 2 7 4 5 3
Enter the element of 2st array 3 2 5 1 6
The contents of array A
a[0]=2
a[1]=7
a[2]=4
a[3]=5
a[4]=3
The contents of array B
b[0]=3
b[1]=2
b[2]=5
b[3]=1
b[4]=6
c[0]=5,thread_id=0
c[1]=9,thread_id=1
c[2]=9,thread_id=2
c[3]=6,thread_id=0
c[4]=9,thread_id=1
The new array is====5 9 9 6 9

CSE

Page 25

COMPUTER CONCEPT AND C PROGRAMING


12B)Design and develop a function rightrot (x, n) in C that returns the value
of the integer x rotated to the right by n bit positions as an unsigned
integer.Invoke the function from the main with different values for x and n
and print the results with suitable headings.
#include<stdio.h>
#include<conio.h>
unsigned int rightrot(unsigned int n,int bit)
{
int i;
for(i=1;i<=bit;i++)
{
n=n>>1;

}
return n;
}

void main()
{
unsigned int n,value;
int x;
clrscr();
printf("Enter an unsigned interger <=65535\n");
scanf("%d",&n);
printf("Enter how many bits to be rotated");
scanf("%d",&x);
printf("\nRight rotation of %d by %d bits is:",n,x);
CSE

Page 26

COMPUTER CONCEPT AND C PROGRAMING


value=rightrot(n,x);
printf("%d",value);
getch();
}

OUTPUT:
1.Enter an unsigned integer <=65535
58
Enter how many bits to be rotated
4
Right rotation of 58 by 4 bits is 3.
2.Enter an unsigned integer <=65535
8
Enter how many bits to be rotated
2
Right rotation of 8 by 2 bits is 2.

CSE

Page 27

COMPUTER CONCEPT AND C PROGRAMING


13B)Design and develop a function isprime (x) that accepts an intege
argument and returns 1 if the argument is prime and 0 otherwise. The
function must use plain division checking approach to determine if a given
number is prime. Invoke this function from the main with different
valuesobtained from the user and print appropriate messages.

#include<stdio.h>
#include<conio.h>
int isprime(int num)
{
int i;
for(i=2;i<=num/2;i++)
{
if(num%i==0)
return 0;//number is not a prime
}
return 1;
}
void main()
{
int number;
clrscr();
printf("Enter the number\n");
scanf("%d",&number);
if(isprime(number))
printf("%d is a prime number\n",number);
else
CSE

Page 28

COMPUTER CONCEPT AND C PROGRAMING


printf("%d is not a prime number\n",number);
getch();
}

OUTPUT:
1.Enter the number
101
101 is a prime number.
1.Enter the number
21
21 s not a prime number.

CSE

Page 29

COMPUTER CONCEPT AND C PROGRAMING


14B)Design, develop and execute a parallel program in C to determine and
print the prime numbers which are less than 100 making use of algorithm of
the Sieve of Eratosthenes.
#include<stdio.h>
#include<math.h>
#include<omp.h>
void main()
{
int a[1000],i,n,k;
printf("Enter the number of elements:");
scanf("%d",&n);
printf("Initialize all the array elements:");
for(i=1;i<=n;i++)
{
a[i]=1;
}
#pragma omp parallel
for(i=2;i<=sqrt(n);i++)
{
if(a[i]!=0)
{
#pragma omp for
for(k=i*i;k<=n;k+=i)
{
a[k]=0;
}
}
}
printf("The prime numbers are:");
for(i=1;i<=n;i++)
{
if(a[i])
printf("%d\t",i);
}
}

CSE

Page 30

COMPUTER CONCEPT AND C PROGRAMING


OUTPUT:
Enter the number of elements 100
Initialize all the array elements:
The prime numbers are:1 2 3 5 7 11 13 17 19 23 29 31 37 41
43 47 53 59 61 67 71 73 79 83 89 97

CSE

Page 31

COMPUTER CONCEPT AND C PROGRAMING


15B)Design and develop a function reverses (s) in C to reverse the string s in
place. Invoke this function from the main for different strings and print the
original and reversed strings.
#include<stdio.h>
#include<conio.h>
void reverses(char source[],char destination[])
{
int i,n;
n=strlen(source);
for(i=0;i<n;i++)
{
destination[n-1-i]=source[i];
}
destination[n]='\0';
}
void main()
{
char source[20],destination[20];
clrscr();
printf("\nEnter the string:");
gets(source);
reverses(source,destination);
printf("\nString before reverse is: %s\n",source);
printf("\nString after reverse is: %s",destination);
getch();
}
CSE

Page 32

COMPUTER CONCEPT AND C PROGRAMING

OUTPUT:
1.Enter the string CBIT
String before reverse is CBIT.
String after reverse is TIBC.
2.Enter the string COMPUTER
String before reverse is COMPUTER.
String after reverse is RETUPMOC.

CSE

Page 33

COMPUTER CONCEPT AND C PROGRAMING


16B)Design and develop a function match any (s1,s2) which returns the first

location in the string s1 where any character from the string s2 occurs, or 1
if s1 contains no character from s2. Do not use the standard library function
which does a similar job! Invoke the function match any (s1. s2) from the
main for different strings and print both the strings and the return value from
the function match any (s1,s2).
#include<stdio.h>
#include<conio.h>
/*inbuilt fuction that does similar job is strcspn()*/
int matchany(char str1[],char str2[])
{
int i,j;
char symbol;
for(i=0;str2[i]!='\0';i++)
{
symbol=str2[i];
for(j=0;str1[j]!='\0';j++)
{
if(symbol==str1[j])
return j+1;
}
}
return -1;
}
void main()
{
char str1[20],str2[20];
CSE

Page 34

COMPUTER CONCEPT AND C PROGRAMING


int pos;
clrscr();
printf("Enter the string\n");
gets(str1);
printf("Enter the string to search\n");
gets(str2);
pos=matchany(str1,str2);
if(pos==-1)
printf("No character in %s is present in %s",str1,str2);
else
printf("Match in character is found at pos %d",pos);
getch();
}

OUTPUT:
1.Enter the string
India
Enter the string to search
area
Match in character is found at pos 5.
2.Enter the string
Karnataka
Enter the string to search
me
No character in me is present in karnataka.

CSE

Page 35

COMPUTER CONCEPT AND C PROGRAMING

CSE

Page 36

Você também pode gostar