/* 1. CHECK MAXIMUM VALUE THAT CAN BE REPRESENTED IN ALL THE
STORAGE TYPES. [ int(short,long,signed,unsigned), char, double, float]
*/
#include
#include
#include
void main()
{
int size;
clrscr();
size=sizeof(short int);
printf("max value in short int is ------>%lf\n",pow(2,size*8-1)-1);
size=sizeof(long int);
printf("max value in long int is ------->%lf\n",pow(2,size*8-1)-1);
size=sizeof(int);
printf("max value in signed int is ----->%lf\n",pow(2,size*8-1)-1);
size=sizeof(unsigned int);
printf("max value in unsigned int is --->%lf\n",pow(2,size*8));
size=sizeof(char);
printf("max value in char is ----------->%lf\n",pow(2,size*8-1)-1);
size=sizeof(double);
printf("max value in double is --------->%lf\n",pow(2,size*8-1)-1);
size=sizeof(float);
printf("max value in float is ---------->%lf\n",pow(2,size*8-1)-1);
getch();
}
/*
=====OUT PUT=====
max value in short int is ------>32767.000000
max value in long int is ------->2147483647.000000
max value in signed int is ----->32767.000000
max value in unsigned int is --->65536.000000
max value in char is ----------->127.000000
max value in double is --------->9223372036854775810.000000
max value in float is ---------->2147483647.000000
*/
/* 2. WRITE A C PROGRAM TO REVERSE A NUMBER */
#include
#include
#include
#define MAX 25
void main()
{
char arr[MAX],temp;
char *f,*r;
clrscr();
printf("Enter a Number To Reverse : ");
gets(arr);
f=arr;
r=&arr[strlen(arr)-1];
while(f
{
temp= *f;
*f=*r;
*r=temp;
f++;
r--;
}
printf("Reversed string is %s",arr);
getch();
}
/*
=====OUT PUT=====
1. Enter a Number To Reverse : 0987654321
Reversed string is 1234567890
------------------------------------------
2. Enter a Number To Reverse : 123321
Reversed string is 123321
*/
/* 3A. WRITE A C PROGRAM TO GENERATE 'N' PRIME NUMBERS. */
#include
#include
void main()
{
int n,m,flag,i,count=0;
clrscr();
printf("\n Enter The Value for 'N' : ");
scanf("%d",&n);
printf("\n The %d Prime No. are...\n",n);
for(i=2;count
{
flag=prime(i);
if(flag==0)
{
printf(" %d \n",i);
count++;
}
}
getch();
}
/* FUNCTION TO FIND PRIME NO. */
prime(int i)
{
int j,f;
f=0;
for(j=2;j<=i/2;j++)
if(i%j==0)
f=1;
return(f);
}
/*
=====OUT PUT=====
1. Enter The Value for 'N' : 3
The 3 Prime No. are...
2
3
5
-------------------------------
2. Enter The Value for 'N' : 9
The 9 Prime No. are...
2
3
5
7
11
13
17
19
23
*/
/* 3B. INPUT TWO INTEGERS 'N' AND 'M'.FIND THE NO. OF PRIME NUMBERS
B/W 'N' AND 'M'(CREATE A FUNCTION WHICH FINDS IF THE GIVEN NO. IS
PRIME.THIS FUNCTION CAN BE CALLED REPEATEDLY).
*/
#include
#include
main()
{
int n,m,flag,i,count=0;
clrscr();
printf("\n Enter The Value for 'N' : ");
scanf("%d",&n);
printf("\n Enter the Value for 'M'[M>N] : ");
scanf("%d",&m);
if((n1))
{
printf("\n The Prime No. are...\n");
for(i=n;i<=m;i++)
{
flag=prime(i);
if(flag==0)
{
printf("%d \n",i);
count++;
}
}
printf("\n There are \'%d\' PRIME no. B/W %d and %d.",count,n,m);
}
else
printf("\n Invalid Condition... ");
getch();
}
/* FUNCTION TO FIND PRIME NO. */
prime(int i)
{
int j,f;
f=0;
for(j=2;j<=i/2;j++)
if(i%j==0)
f=1;
return(f);
}
/*
=====OUT PUT=====
1. Enter The Value for 'N' : 9
Enter the Value for 'M'[M>N] : 2
Invalid Condition...
2. Enter The Value for 'N' : 2
Enter the Value for 'M'[M>N] : 25
The Prime No. are...
2
3
5
7
11
13
17
19
23
There are '9' PRIME no. B/W 2 and 25.
*/
/* 4A. WRITE A C PROGRAM TO GENERATE N FIBONACCI NUMBERS */
#include
#include
void main()
{
int a=0,b=1,c,n,i;
clrscr();
printf("Enter the value of N : ");
scanf("%d",&n);
printf("\n%d\n%d",a,b);
for(c=a+b,i=3;i<=n;c=a+b,a=b,b=c,i++)
printf("\n%d",c);
getch();
}
/*
=====OUTPUT=====
Enter the value of N : 10
The 10 Fibonacci Numbers Are...
0
1
1
1
2
3
5
8
13
21
*/
/* 4B. WRITE A C PROGRAM TO GENERATE FIBONACCI SERIES UPTO A GIVEN
NUMBER */
#include
#include
void main()
{
int a=0,b=1,c,n;
clrscr();
printf("Enter the limit to Generate Fibonacci No.s : ");
scanf("%d",&n);
printf("\n%d\n%d",a,b);
for(c=a+b;c<=n;c=a+b,a=b,b=c)
printf("\n%d",c);
getch();
}
/*
=====OUT PUT=====
Enter the limit to Generate Fibonacci No.s : 25
0
1
1
1
2
3
5
8
13
21
*/
/* 4C. WRITE A C PROGRAM TO GENERATE 3 DIGITS FIBONACCI SERIES */
#include
#include
void main()
{
int a=0,b=1,c;
clrscr();
printf("THREE DIGITS FIBONACCI SERIES :\n");
for(c=a+b;c<100 data-blogger-escaped-c="a+b,a=b,b=c);</span">
for(;c<1000 data-blogger-escaped-c="a+b,a=b,b=c)</span">
printf("\n%d",c);
getch();
}
/*
=====OUT PUT=====
THREE DIGITS FIBONACCI SERIES :
144
233
377
610
987
*/
/* PR5.WRITE A C PROGRAM TO CONVERT THE FOLLOWING :
a. DECIMAL TO BINARY NUMBER
b. BINARY TO DECIMAL NUMBER
c. REAL DECIMAL NUMBER TO HEXADECIMAL NUMBER
d. REAL DECIMAL TO OCTAL NUMBER
*/
#include
#include
#include /* isdigit */
#include /* atoi */
#include
#define MAX 25
void dectobin()
{
int bin,dec,n,r,k;
printf("Enter a Decimal number \t");
scanf("%d",&dec);
n=dec; bin=0;k=1;
while(n>0)
{
r=n%2;
n=n/2;
bin=bin+r*k;
k=k*10;
}
printf("\n Decimal = %d \n Binary = %d",dec,bin);
}
void bintodec()
{
int bin,dec,n,r,k;
printf("Enter a Binary number \t");
scanf("%d",&bin);
n=bin; dec=0;k=1;
while(n>0)
{
r=n%10;
if(r>1)
{
printf("\t ERROR!! Not a Binary number");
return;
}
n=n/10;
dec=dec+r*k;
k=k*2;
}
printf("\n Binary = %d \n Decimal = %d",bin,dec);
}
void rdectohex()
{
char str[MAX],str1[MAX],str2[MAX],a;
int i,j,n,r,len,temp,sign=1;
printf("Enter a Real Decimal Number\t");
scanf("%s",str);
for(j=0,i=0 ; str[i]!='.' && str[i]!='\0' ; i++)
{
if(str[i]=='-')
sign=-1;
else if(str[i]=='+')
sign=1;
else if(str[i]>='0' && str[i]<='9')
str1[j++]=str[i];
else
{
printf("ERROR!! Not a Real Decimal Number");
return;
}
}
str1[j]='\0';
i++;
for(j=0 ; str[i]!='\0' ; i++)
{
if(str[i]>='0' && str[i]<='9')
str2[j++]=str[i];
else
{
printf("ERROR!! Not a Real Decimal Number");
return;
}
}
str2[j]='\0';
/* before decimal */
i=0;
n=atoi(str1);
while(n>0)
{
r=n%16;
if(r>9)
{
switch(r)
{
case 10:a='A';break;
case 11:a='B';break;
case 12:a='C';break;
case 13:a='D';break;
case 14:a='E';break;
case 15:a='F';break;
}
}
else
a=(char)r+'0';
n=n/16;
str1[i++]=a;
}
str1[i]='\0';
/* after decimal */
i=0;
j=0;
n=atoi(str2);
len=strlen(str2);
while(j<5 data-blogger-escaped-span="span">
{
temp=pow(10,len);
r=(n*16) / temp; /* r=quotient */
n=(n*16) % temp;
if(r>9)
{
switch(r)
{
case 10:a='A';break;
case 11:a='B';break;
case 12:a='C';break;
case 13:a='D';break;
case 14:a='E';break;
case 15:a='F';break;
}
}
else
a=(char)r+'0';
str2[i++]=a;
j++;
}
str2[i]='\0';
/* To Display */
strrev(str1);
if(sign==-1)
printf("\nHexadecimal : -%s.%s",str1,str2);
else
printf("\nHexadecimal : %s.%s",str1,str2);
}
void rdectooct()
{
char str[MAX],str1[MAX],str2[MAX];
int i,j,n,r,k,temp,len,sign=1,oct1,oct2;
printf("Enter a Real Decimal Number\t");
scanf("%s",str);
for(j=0,i=0 ; str[i]!='.' && str[i]!='\0' ; i++)
{
if(str[i]=='-')
sign=-1;
else if(str[i]=='+')
sign=1;
else if(str[i]>='0' && str[i]<='9')
str1[j++]=str[i];
else
{
printf("ERROR!! Not a Real Decimal Number");
return;
}
}
str1[j]='\0';
i++;
for(j=0 ; str[i]!='\0' ; i++)
{
if(str[i]>='0' && str[i]<='9')
str2[j++]=str[i];
else
{
printf("ERROR!! Not a Real Decimal Number");
return;
}
}
str2[j]='\0';
/* before decimal */
i=0;
n=atoi(str1);
oct1=0;k=1;
while(n>0)
{
r=n%8;
if(r>7)
{
printf("\t ERROR!! Not a Octal number");
return;
}
n=n/8;
oct1=oct1+r*k;
k=k*10;
}
/* After decimal */
oct2=0;
j=0;
n=atoi(str2);
len=strlen(str2);
temp=pow(10,len);
while(j<4 data-blogger-escaped-span="span">
{
r=(n*8)/temp; /* r is Quotient and generated
carry is indicated by it*/
n=(n*8)%temp;
oct2=(oct2*10)+r;
j++;
}
/* To Display */
if(sign==-1)
printf("\nOctal : -%d.%d",oct1,oct2);
else
printf("\nOctal : %d.%d",oct1,oct2);
}
void main()
{
int flag=1,choice=0;
while(flag!=0)
{
clrscr();
printf("1. Decimal to Binary\n");
printf("2. Binary to Decimal\n");
printf("3. Real Decimal to Hexadecimal\n");
printf("4. Real Decimal to Octal\n");
printf("5. Exit\n");
printf("Enter choice -->\t");
scanf("%d",&choice);
switch(choice)
{
case 1: dectobin();flag=1;getch();break;
case 2: bintodec();flag=1;getch();break;
case 3: rdectohex();flag=1;getch();break;
case 4: rdectooct();flag=1;getch();break;
default: flag=0;break;
}
choice=0;
}
}
/*
=====OUT PUT=====
1. Decimal to Binary
2. Binary to Decimal
3. Real Decimal to Hexadecimal
4. Real Decimal to Octal
5. Exit
Enter choice --> 1
Enter a Decimal number 10
Decimal = 10
Binary = 1010
1. Decimal to Binary
2. Binary to Decimal
3. Real Decimal to Hexadecimal
4. Real Decimal to Octal
5. Exit
Enter choice --> 2
Enter a Binary number 1010
Binary = 1010
Decimal = 10
1. Decimal to Binary
2. Binary to Decimal
3. Real Decimal to Hexadecimal
4. Real Decimal to Octal
5. Exit
Enter choice --> 3
Enter a Real Decimal Number 12.87
Hexadecimal : C.DEB85
1. Decimal to Binary
2. Binary to Decimal
3. Real Decimal to Hexadecimal
4. Real Decimal to Octal
5. Exit
Enter choice --> 4
Enter a Real Decimal Number 25.895
Octal : 31.7121
*/
/* 6A. WRITE A C PROGRAM TO SUM THE SERIES:
x^3 x^5 x^7
SIN(x)= x - ----- + ----- + ----- + ...
3! 5! 7!
*/
#include
#include
#include
#define PI 3.143
long int fact(int n) /* To find factorial */
{
if(n==0)
return 1;
else
return(n*fact(n-1));
}
void main()
{
int i,n,term=1;
float x,y,sum;
clrscr();
printf("\n SIN Series...");
printf("\n Enter No. of Terms N [1-12] : ");
scanf("%ld",&n);
if(n>=1 && n<=12)
{
printf("\n Enter the Value of 'X' : ");
scanf("%f",&y);
x=y*(PI/180.00); /* Converting degrees to radians */
sum=x;
for(i=3;term<=n;i+=2)
{
if(i==3)
sum-=pow(x,i)/(float)fact(i);
else
sum+=pow(x,i)/(float)fact(i);
term++;
}
printf("\n Sin(%f) = %f",y,sum);
}
else
printf("Wrong entry!!!");
getch();
}
/* =====OUT PUT=====
SIN Series...
1. Enter No. of Terms N [1-12] : 9
Enter the Value of 'X' : 45
Sin(45.000000) = 0.707429
2. SIN Series...
Enter No. of Terms N [1-12] : 5
Enter the Value of 'X' : 0
Sin(0.000000) = 0.000000
3. SIN Series...
Enter No. of Terms N [1-12] : 15
Wrong entry!!!
*/
/* 6B. WRITE A C PROGRAM TO SUM THE SERIES:
x^2 x^4 x^6
COS(x)= 1 - ----- + ----- - ----- + ...
2! 4! 6!
*/
#include
#include
#include
#define PI 3.143
long int fact(int n) /* To find factorial */
{
if(n==0)
return 1;
else
return(n*fact(n-1));
}
void main()
{
int i,n,term=1;
float x,y,sum1,sum2;
clrscr();
printf("\n COS Series...");
printf("\n Enter No. of Terms N [1-12] : ");
scanf("%ld",&n);
if(n>=1 && n<=12)
{
printf("\n Enter the Value of 'X' : ");
scanf("%f",&y);
x=y*(PI/180.00); /* Converting degrees to radians */
sum1=1;
sum2=0;
for(i=2;term<=n;i+=2)
{
if(i%4==0)/* To add positive terms */
sum1+=pow(x,i)/(float)fact(i);
else /* To add Negative terms */
sum2+=pow(x,i)/(float)fact(i);
term++;
}
printf("\n Cos(%.2f) = %f",y,sum1-sum2);
}
else
printf("Wrong entry!!!");
getch();
}
/* =====OUT PUT=====
1. COS Series...
Enter No. of Terms N [1-12] : 9
Enter the Value of 'X' : 90
Cos(90.00) = -0.000700
2. COS Series...
Enter No. of Terms N [1-12] : 11
Enter the Value of 'X' : 0
Cos(0.00) = 1.000000
3. COS Series...
Enter No. of Terms N [1-12] : 0
Wrong entry!!!
*/
/* 6C. WRITE A C PROGRAM TO SUM THE SERIES:
x x^2 x^3
e^x = 1 + ----- + ----- + ----- + ...
1! 2! 3! */
#include
#include
#include
#define PI 3.143
long int fact(int n) /* To find factorial */
{
if(n==0)
return 1;
else
return(n*fact(n-1));
}
void main()
{
int i,n,term=1;
float x,y,sum;
clrscr();
printf("\n Enter No. of Terms N [1-12] : ");
scanf("%ld",&n);
if(n>=1 && n<=12)
{
printf("\n Enter the Value of 'X' in degrees : ");
scanf("%f",&y);
x=y*(PI/180.00); /* Converting degrees to radians */
sum=1;
for(i=1;term<=n;i++)
{
sum+=pow(x,i)/(float)fact(i);
term++;
}
printf("\n e^%.2f = %f",y,sum);
}
else
printf("Wrong entry!!!");
getch();
}
/* =====OUT PUT=====
1.Enter No. of Terms N [1-12] : 8
Enter the Value of 'X' in degrees : 60
e^60.00 = 2.850986
2.Enter No. of Terms N [1-12] : 12
Enter the Value of 'X' in degrees : 90
e^90.00 = 4.813863
3.Enter No. of Terms N [1-12] : 9
Enter the Value of 'X' in degrees : 0
e^0.00 = 1.000000
*/
/* 7. THREE POSITIVE INTEGERS a,b,c FORM A TRIPLET OF a^2 + b^2 = c^2.
WRITE A C PROGRAM TO FIND ALL PYTHOGOREAN TRIPLETS a,b,c WHERE
a,b <>
*/
#include
#include
#include
void main()
{
int a,b,c2,c;
clrscr();
printf("Pythogorean Triplets\n\n");
for(a=1;a<120 data-blogger-escaped-a="a" data-blogger-escaped-span="span">
{
for(b=a;b<120 data-blogger-escaped-b="b" data-blogger-escaped-span="span">
{
c2 = a*a + b*b;
c=(int)sqrt(c2);
if(c*c == c2)
{
printf("%d %d %d,\t",a,b,c);
}
}
}
getch();
}
/*
=====OUT PUT=====
Pythogorean Triplets
3 4 5, 5 12 13, 6 8 10, 7 24 25, 8 15 17,
9 12 15, 9 40 41, 10 24 26, 11 60 61, 12 16 20,
12 35 37, 13 84 85, 14 48 50, 15 20 25, 15 36 39,
15 112 113, 16 30 34, 16 63 65, 18 24 30, 18 80 82,
20 21 29, 20 48 52, 20 99 101, 21 28 35, 21 72 75,
24 32 40, 24 45 51, 24 70 74, 25 60 65, 27 36 45,
28 45 53, 28 96 100, 30 40 50, 30 72 78, 32 60 68,
33 44 55, 33 56 65, 35 84 91, 36 48 60, 36 77 85,
36 105 111, 39 52 65, 39 80 89, 40 42 58, 40 75 85,
40 96 104, 42 56 70, 44 117 125, 45 60 75, 45 108 117,
48 55 73, 48 64 80, 48 90 102, 51 68 85, 54 72 90,
56 90 106, 56 105 119, 57 76 95, 60 63 87, 60 80 100,
60 91 109, 63 84 105, 65 72 97, 66 88 110, 66 112 130,
69 92 115, 72 96 120, 75 100 125, 78 104 130, 80 84 116,
81 108 135, 84 112 140, 87 116 145, 88 105 137, 96 110 146,
100 105 145,
*/
/* 8. INPUT AN INTEGER N, FIND OUT THE POSITIONS OF OCCURRENCE OF
DIGITS 0,1,2,3,4,...9 IN THIS NUMBER. (CREATE A FUNCTION WHICH FINDS
THE POSITIONS OF OCCURRENCE OF A PARTICULAR DIGIT. THIS FUNCTION
CAN BE CALLED REPEATEDLY).
*/
#include
#include
#define MAX 25
void main()
{
char arr[MAX],posof;
int i,present;
clrscr();
printf("Enter an integer");
scanf("%s",arr);
for(i=0;i
{
if( !(arr[i]>='0' && arr[i]<='9') )
printf("The number is not an integer");
}
for(posof='0';posof<='9';posof++)
{
printf("The position of %c -->",posof);
for(i=0,present=0;i
{
if(arr[i]==posof)
{
printf("%d ",i+1);
present++;
}
}
if(present==0)
printf("Not present");
printf("\n");
}
getch();
}
/*
=====OUT PUT=====
1.Enter an integer : 234876521
The position of 0 -->Not present
The position of 1 -->9
The position of 2 -->1 8
The position of 3 -->2
The position of 4 -->3
The position of 5 -->7
The position of 6 -->6
The position of 7 -->5
The position of 8 -->4
The position of 9 -->Not present
2.Enter an integer : xyz
The number is not an integer
The position of 0 -->Not present
The position of 1 -->Not present
The position of 2 -->Not present
The position of 3 -->Not present
The position of 4 -->Not present
The position of 5 -->Not present
The position of 6 -->Not present
The position of 7 -->Not present
The position of 8 -->Not present
The position of 9 -->Not present
*/
/* 9a.WRITE A CPROGRAM TO GENERATE THE FOLLOWING OUT PUT:
a) 1
2 3
4 5 6
7 8 9 10
*/
#include
#include
#define MAXLINE 4
void main()
{
int linenum=1,count=1,i=1;
clrscr();
while(linenum <= MAXLINE)
{
for(count=1 ; count<=linenum ; i++)
{
printf("%d\t",i);
count++;
}
printf("\n");
linenum++;
}
getch();
}
/*
=====OUT PUT=====
1
2 3
4 5 6
7 8 9 10
*/
/* 9b.WRITE A CPROGRAM TO GENERATE THE FOLLOWING OUT PUT:
b) 1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
*/
#include
#include
#define MAXLINE 5
void main()
{
int curlin,i;
clrscr();
curlin=1;
while(curlin <= MAXLINE)
{
for(i=0 ; i < (MAXLINE - curlin) ; i++)
printf(" "); /* 3 spaces */
for(i=1;i<=curlin;i++)
{
if(i<=9)
printf("%d ",i); /* 1 digit followed by 2 spaces */
else
printf("%d ",i); /* 2 digits followed by 1 space */
}
i-=2;
for( ; i>=1 ; i--)
{
if(i<=9)
printf("%d ",i); /* 1 digit followed by 2 spaces */
else
printf("%d ",i); /* 2 digits followed by 1 space */
}
printf("\n");
curlin++;
}
getch();
}
/*
=====OUT PUT=====
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
*/
/* 10. IN A GIVEN STRING, FIND THE FREQUENCY OF OCCURRENCE OF
A PARTICULAR LIST
*/
#include
#include
#include
#define MAX 30
void main()
{
char str[MAX],str1[MAX];
int i,j,len1,len2,flag,freq;
clrscr();
printf(" \n Enter a String : ");
gets(str);
printf("\n Enter a List : ");
gets(str1);
len1=strlen(str);
len2=strlen(str1);
freq=0;
for(i=0;i
{
flag=0;
for(j=0;j
{
if(str[i]==str1[j])
{
flag++;
i++,j++;
}
else
{
i++;
break;
}
}
if(flag==len2)
freq++;
}
printf("\n The Frequency of occurence of \n\t\"%s\" in \"%s\" is %d",str1,str,freq);
getch();
}
/*
=====OUT PUT=====
1. Enter a String : KAUNDINYA
Enter a List : KAUN
The Frequency of occurence of
"KAUN" in "KAUNDINYA" is 1
2. Enter a String : bharath
Enter a List : kaun
The Frequency of occurence of
"kaun" in "bharath" is 0
*/