C-programms
/* 33. USE RECURSION TO FIND THE FIRST N TERMS OF THE FIBONACCI
SERIES
*/
#include
#include
int fib(int n)
{
if(n==0)
return 0;
else if(n==1)
return 1;
else
return(fib(n-1)+fib(n-2));
}
void main()
{
int n,i;
clrscr();
printf("Enter the value of N : ");
scanf("%d",&n);
printf("\n The fibonacci series is...\n");
for(i=0;i
{
printf("%d\n",fib(i));
}
getch();
}
/*
=====OUT PUT=====
Enter the value of N : 10
The fibonacci series is...
0
1
1
2
3
5
8
13
21
34
*/
/* 34. WRITE A RECURSIVE FUNCTION TO FIND THE FACTORIAL OF A NUMBER */
#include
#include
long int fact(int n)
{
if(n==0)
return 1;
else
return(n*fact(n-1));
}
void main()
{
int n;
clrscr();
printf("Enter the value of N : ");
scanf("%d",&n);
printf("Factorial of %d is %ld",n,fact(n));
getch();
}
/*
=====OUT PUT=====
Enter the value of N : 6
Factorial of 6 is 720
*/
/* 35. WRITE A RECURSIVE FUNCTION TO FIND THE SUM OF N NATURAL
NUMBERS
*/
#include
#include
int sum(int n)
{
if(n==1)
return 1;
else
return(n+sum(n-1));
}
void main()
{
int n;
clrscr();
printf("Enter the value of N : ");
scanf("%d",&n);
printf("Sum of %d integers is %d",n,sum(n));
getch();
}
/*
=====OUT PUT=====
Enter the value of N : 10
Sum of 10 integers is 55
*/
/* 37. USE RECURSION TO FIND X^n WHERE X IS A REAL NUMBER AND n
IS AN INTEGER. n CAN BE NEGATIVE, ZERO OR POSITIVE
*/
#include
#include
float fun(float x,int n)
{
if(n==0)
return 1;
else if(n>0)
return(x*fun(x,n-1));
else
return(1/x*fun(x,n+1));
}
void main()
{
int n;
float x;
clrscr();
printf("Enter value of X and n for [X^n] : ");
printf("\n X = ");
scanf("%f",&x);
printf(" n = ");
scanf("%d",&n);
printf("\nThe result is %f",fun(x,n));
getch();
}
/*
=====OUT PUT=====
1. Enter value of X and n for [X^n] :
X = 4
n = 2
The result is 16.000000
2. Enter value of X and n for [X^n] :
X = 2
n = 4
The result is 16.000000
*/
/* 38. FIND THE SOLUTION OF THE GIVEN QUADRATIC EQUATION. ( CREATE
A MACRO WHICH CALCULATES THE DETERMINANT.
____________
-A ± Ö B^2 - 4*A*C
Formula used:- X = --------------------------------
2*A
*/
#include
#include
#include
#define DET(a,b,c) b*b-4*a*c
void main()
{
float a,b,c;
clrscr();
printf("Enter coordinates of quad eqn: Ax2 + Bx + C : ");
printf("\n A = ");
scanf("%f",&a);
printf(" B = ");
scanf("%f",&b);
printf(" C = ");
scanf("%f",&c);
printf("\nRoots are : ");
if(DET(a,b,c)>=0)
{
printf("\n(1). %f",(-a+sqrt(DET(a,b,c)))/(2*a));
printf("\n(2). %f",(-a-sqrt(DET(a,b,c)))/(2*a));
}
else
{
printf("\n(1). %f + %f i",-a/(2*a),sqrt(-(DET(a,b,c)))/(2*a) );
printf("\n(2). %f + %f i",-a/(2*a),-sqrt(-(DET(a,b,c)))/(2*a) );
}
getch();
}
/*
=====OUTPUT=====
1.Enter coordinates of quad eqn: Ax2 + Bx + C :
A = 2
B = 4
C = 1
Roots are :
(1). 0.207107
(2). -1.207107
2.Enter coordinates of quad eqn: Ax2 + Bx + C :
A = 4
B = 1
C = 9
Roots are :
(1). -0.500000 + 1.494783 i
(2). -0.500000 + -1.494783 i
*/
/* 39. FIND THE LARGEST AND SECOND LARGEST OF THREE INTEGERS.
( CREATE MACRO WHICH FINDS LARGEST OF TWO INTEGERS.USE THIS
MACRO MULTIPLE TIMES. USE A NESTED IF ELSE STRUCTURE.)
*/
#include
#include
#define LARGE(A,B) A>B ? A : B
void main()
{
int a,b,c,temp;
clrscr();
printf("Enter three integers : ");
scanf("%d %d %d",&a,&b,&c);
temp=LARGE(b,c);
temp=LARGE(a,temp);
printf("\nLargest is : %d",temp);
if(temp==a)
printf("\nSecond Largest is : %d",LARGE(b,c));
else if(temp==b)
printf("\nSecond Largest is : %d",LARGE(a,c));
else
printf("\nSecond Largest is : %d",LARGE(a,b));
getch();
}
/*
=====OUTPUT=====
Enter three integers : 546 234 987
Largest is : 987
Second Largest is : 546
*/
/* 40. CREATE AN ARRAY OF TEN ELEMENTS WHERE EACH ELEMENT IS A
STRUCTURE WITH THE FOLLOWING FIELDS : IDNO, NAME,MARKS AND
AGE. READ DATA INTO THIS ARRAY. FIND THE AVERAGE MARKS OF THESE
TEN STUDENTS AND ALSO THE NAME OF THE YOUNGEST STUDENT.
*/
#include
#include
#define MAX 10
struct student{
char name[20],idno[10];
int age;
int marks;
};
void main()
{
struct student arr[MAX];
int i,count,minage;
char minname[MAX][20];
float avg=0;
clrscr();
printf("Enter the records for each student :");
for(i=0;i
{
printf("\nEnter %d student's record",i+1);
printf("\n NAME : ");
scanf("%s",arr[i].name);
printf(" IDNO : ");
scanf("%s",arr[i].idno);
printf(" AGE : ");
scanf("%d",&arr[i].age);
printf(" MARKS : ");
scanf("%d",&arr[i].marks);
}
for(i=0;i
avg+=arr[i].marks;
avg=avg/MAX;
count=0;
i=0;
minage=arr[i].age;
strcpy(minname[count++],arr[i].name);
i++;
for(;i
{
if(arr[i].age <>
{
count=0;
minage=arr[i].age;
strcpy(minname[count++],arr[i].name);
}
else if(arr[i].age == minage)
strcpy(minname[count++],arr[i].name);
}
printf("\n\tAverage marks of %d students is %f",MAX,avg);
printf("\n\tThe youngest student/s is/are");
for(i=0;i
printf("\n\t%s",minname[i]);
getch();
}
/*
=====OUTPUT=====
Enter the records for each student :
Enter 1 student's record
NAME : AAAAAAA
IDNO : 0111
AGE : 19
MARKS : 56
Enter 2 student's record
NAME : BBBBBBB
IDNO : 0112
AGE : 20
MARKS : 89
Enter 3 student's record
NAME : CCCCCCC
IDNO : 0114
AGE : 23
MARKS : 78
Enter 4 student's record
NAME : DDDDDDD
IDNO : 0118
AGE : 24
MARKS : 67
Enter 5 student's record
NAME : EEEEEE
IDNO : 0115
AGE : 22
MARKS : 87
Enter 6 student's record
NAME : FFFFFF
IDNO : 0117
AGE : 21
MARKS : 90
Enter 7 student's record
NAME : GGGGGGG
IDNO : 0120
AGE : 20
MARKS : 35
Enter 8 student's record
NAME : HHHHH
IDNO : 0122
AGE : 23
MARKS : 59
Enter 9 student's record
NAME : IIIIII
IDNO : 0128
AGE : 22
MARKS : 45
Enter 10 student's record
NAME : JJJJJJ
IDNO : 0116
AGE : 19
MARKS : 99
Average marks of 10 students is 70.500000
The youngest student/s is/are
AAAAAAA
JJJJJJ
*/
/* 41. DECLARE THREE POINTER VARIABLES TO STORE A CHARACTER,
A CHARACTER STRING AND AN INTEGER RESPECTIVELY. INPUT VALUES
INTO THESE VARIABLES. OUTPUT THESE POINTER VARIABLES AS WELL
AS THEIR CONTENTS.
*/
#include
#include
void main()
{
int a,*pi;
char c,str[20];
char *pc,*ps;
clrscr();
printf("Enter a character : ");
scanf("%c",&c);
printf("Enter an integer : ");
scanf("%d",&a);
printf("Enter a string : ");
scanf("%s",str);
pi=&a;
pc=&c;
ps=str;
printf("\nThe value of character pointer is : %u",pc);
printf("\nThe content of character pointer is : %c\n",*pc);
printf("\nThe value of integer pointer is : %u",pi);
printf("\nThe content of integer pointer is : %d\n",*pi);
printf("\nThe value of string pointer is : %u",ps);
printf("\nThe content of string pointer is : %s",ps);
getch();
}
/*
=====OUTPUT=====
Enter a character : S
Enter an integer : 9
Enter a string : kaun
The value of character pointer is : 65523
The content of character pointer is : S
The value of integer pointer is : 65524
The content of integer pointer is : 9
The value of string pointer is : 65500
The content of string pointer is : kaun
*/
/* 44. INPUT THREE INTEGERS X, Y AND N.PERFORM
a. BIT WISE AND OF X AND Y
b. BIT WISE INCLUSIVE OR OF X AND Y
c. BIT WISE EXCLUSIVE OR OF X AND Y
d. BIT WISE COMPLEMENT OF X
e. LEFT SHIFT OF X BY N BITS
f. RIGHT SHIFT OF Y BY N BITS
DISPLAY X,Y AND THE RESULTS OF THE ABOVE OPERATIONS IN BIT
FORMAT.
*/
#include
#include
#define MAX 25
char* dectobin(unsigned int dec,char arr[])
{
unsigned int n;
int i=0,r;
char a,temp;
char *f,*re;
n=dec;
if(n==0)
arr[i++]='0';
else
{
while(n>0)
{
r=n%2;
n=n/2;
a=(char)r+'0';
arr[i++]=a;
}
}
arr[i]='\0';
/* To reverse array */
f=arr;
re=&arr[strlen(arr)-1];
while(f
{
temp= *f;
*f=*re;
*re=temp;
f++;
re--;
}
return arr;
}
void main()
{
unsigned int x,y,n,ans;
char arr[MAX];
clrscr();
printf("Enter three integers : ");
printf("\n X = ");
scanf("%d",&x);
printf(" Y = ");
scanf("%d",&y);
printf(" N = ");
scanf("%d",&n);
printf("\n X = decimal : %u \t bin : %s",x,dectobin(x,arr));
printf("\n Y = decimal : %u \t bin : %s\n",y,dectobin(y,arr));
ans=x&y;/* bitwise AND */
printf("\n X&Y = decimal : %u \t bin : %s",ans,dectobin(ans,arr));
ans=x|y;/* bitwise INCLUSIVE OR */
printf("\n X|Y = decimal : %u \t bin : %s",ans,dectobin(ans,arr));
ans=x^y;/* bitwise EXCLUSIVE OR */
printf("\n X^Y = decimal : %u \t bin : %s",ans,dectobin(ans,arr));
ans=~x;/* bitwise COMPLEMENT */
printf("\n !X = decimal : %u \t bin : %s",ans,dectobin(ans,arr));
ans=x<
printf("\n X<
ans=y>>n;/* Right shift by n bits*/
/*printf("\n%d",ans); */
printf("\n Y>>N = decimal : %u \t bin : %s",ans,dectobin(ans,arr));
getch();
}
/*
=====OUT PUT=====
Enter three integers :
X = 5
Y = 9
N = 2
X = decimal : 5 bin : 101
Y = decimal : 9 bin : 1001
X&Y = decimal : 1 bin : 1
X|Y = decimal : 13 bin : 1101
X^Y = decimal : 12 bin : 1100
!X = decimal : 65530 bin : 1111111111111010
X< bin : 10100
Y>>N = decimal : 2 bin : 10
0 comments:
Post a Comment