Documente Academic
Documente Profesional
Documente Cultură
4.ARMSTRONG NO:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,a,r, sum=0;
clrscr();
printf(" enter the no:");
scanf(" %d",&n);
a=n;
while(n>0)
{
r=n%10;
sum=sum+r*r*r;
n=n/10;
}
if(a==sum)
printf("%d is an armsstrong no",sum);
else
printf("its is not an armstrong no",);
getch();
}
5.Decimal to binary :
#include<stdio.h>
#include<conio.h>
main()
{
int bnum,digit,decimal=0,bin,base=0;
clrscr();
printf("\nEnter the binary number:");
scanf("%d",&bnum);
bin=bnum;
while(bnum!=0)
{
digit=bnum%10;
decimal=decimal+(digit<<base);
base=base+1;
bnum=bnum/10;
}
printf("\nThe binary equivalent of %d in decimal=%d",bin,decimal);
getch();
}
6.Standard deviation:
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int i,n;
float val[30],tempvar,mean,var,sd;
float sum=0;
float sumvar=0;
clrscr();
printf(" Enter the value of N:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%f",&val[i]);
sum=sum+val[i];
}
mean=sum/(float)n;
for( i=1;i<=n;i++)
{
tempvar=val[i]-mean;
sumvar+=tempvar*tempvar;
}
var=sumvar/(float)n;
sd=sqrt(var);
printf(" The of Items N is:%d\n",n);
printf("Mean: %3.2f\n",mean);
printf("Variance:%3.2f\n",var);
printf("Standard Deviation :%3.2f\n",sd);
getch();
}
11.Sine series:
#include<stdio.h>
#include<math.h>
#include<conio.h>
main()
{
int no,i;
float x,a,sum,b;
clrscr();
printf("Enter the number : ");
scanf("%f %d",&x,&no);
b=x;
x=x*3.141/180;
a=x;
sum=x;
for(i=1;i<no+1;i++)
{
a=(a*pow((double)(-1),(double)(2*i-1))*x*x)/(2*i*(2*i+1));
sum=sum+a;
}
printf("Sin(%f) value is %f",b,sum);
getch();
}
12.Cosine series:
#include<stdio.h>
#include<math.h>
#include<conio.h>
main()
{
float x,a,sum=1,temp=1;
int i,no=20,mul;
clrscr();
printf("\nEnter the value of x:");
scanf("%f",&x);
a=x;
x=x*3.1412/180;
temp=temp*pow((double)(-1),(double)(2*i-1))*x*x/(2*i*(2*i-1));
sum=sum+temp;
printf("\nThe cosine value of %f is %f",a,sum);
getch();
}
cost=unit*1.00;
printf(" The total payment is Rs:%3.2f\n",cost);
}
getch();
}
14.Menu driven calculator:
#include<stdio.h>
#include<conio.h>
void main()
{ int n;
float add,sub,mul,a,b,div;
clrscr();
printf("Enter the values for a & b :");
scanf("%f%f",&a,&b);
printf("\n Enter 1 for addition");
printf("\n Enter 2 for subtraction \n ");
printf("\n Enter 3 for multiplication");
printf("\n Enter 4 for division\n");
scanf("%d",&n);
switch(n)
{
case 1: printf("The values are %f %f",a,b);
add=a+b;
printf("\nThe resultant value is :%f",add);
break;
case 2: printf("The values are%f %f",a,b);
sub=a-b;
printf("\nThe resultant value is :%f",sub);
break;
case 3: printf("The values are %f %f",a,b);
mul=a*b;
printf("\nThe resultant value is :%f",mul);
break;
case 4: printf("The values are%f %f",a,b);
div=a/b;
printf("\nThe resultant value is :%5.2f",div);
break;
default :printf("ERROR :enter the values within the range");
}getch();
}
17.Palindrome of a no:
#include<stdio.h>
#include<conio.h>
main()
{
unsigned long int a,num,sum=0,rnum=0,rem;
clrscr();
printf("\nEnter the number:");
scanf("%ld",&num);
a=num;
while(num!=0)
{
rem=num%10;
sum=sum+rem;
rnum=rnum*10+rem;
num=num/10;
}
printf("\nThe sum of the digits of %ld is %ld\n",a,sum);
printf("\nThe reverse number of the %ld is %ld",a,rnum);
if(a==rnum)
printf("\nThe given number is a palindrome");
else
printf("\nThe given number is not a palindrome");
getch();
18.Palindrome of a string:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
main()
{
int len=0,i,j;
char name[25];
clrscr();
printf("Enter the string:");
scanf("%s",name);
while(name[len]!='\0')
len++;
printf("\n%d",len);
for(i=0,j=len-1;i<len/2;i++,j--)
{
if(name[i]!=name[j])
{
printf("\nThe given string is not a palindrom");
exit(0);
}
else printf(" the given string is palindrome");
}
}
if(t<a[m])
h=m-1;
else if (t>a[m])
l=m+1;
else
{
printf("\n Entered %d is in position %d",t,m+1);
flag=2;
break;
}
}
if(flag==1)
printf("\n Entered element is not in the array");
getch();
}
21.Quadratic equations:
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
int a,b,c,d;
float root1,root2;
clrscr();
printf("Enter the values of a,b,c\n");
scanf("%d %d %d",&a,&b,&c);
d=b*b-4*a*c;
if(d>=0)
{ printf(" Real and postive root");
root1=(-b+sqrt(d))/(2*a);
root2=(+b+sqrt(d))/(2*a);
printf("The roots of the values a=%d,b=%d,c=%d are\nR1: %f
R2:%f",a,b,c,root1, root2);
}
else if(d==0)
{
printf(" The roots are equal");
root1=-b/2*a; since d==0;
root1=root2;
printf(" \nRoot1 =%d \n Root2 =%d");
}
else
printf("The roots are imaginary");
getch();
}
22.Pascal triangle:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,m,n;
clrscr();
printf("Enter the number of lines to be printed : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(k=n;k>i;k--)
printf(" ");
for(j=0;j<=i;j++)
{
if((j==0)||(i==0))
m=1;
else
m=m*(i-j+1)/j;
printf("%4d",m);
}
printf("\n\n");
}
getch();
}
23.Ordinary factorial
#include<stdio.h>
#include<conio.h>
main()
{
long int fact=1,i,num;
clrscr();
printf("Enter the number:");
scanf("%ld",&num);
for(i=1;i<=num;i++)
{
fact=fact*i;
}
printf("The factorial of %ld is %ld:",num,fact);
getch();
}
24.Ordinary fibonacci:
#include<stdio.h>
#include<conio.h>
main()
{
int num,fib=0,a=0,b=1,i;
clrscr();
printf("Enter the number:");
scanf("%d",&num);
printf("\n FIBBONACI SERIES is :\n");
if(num==0)
printf("0");
else
{
for(i=0;i<num;i++)
{
fib=fib+a;
a=b;
b=fib;
printf("\n %d\t",fib);
}
getch();
}
}
if(loc==0)
printf("Unsuccessful search. %d not found. \n", key);
else
{
printf("Successful search.\n");
printf("%d found at position %d. \n", key, loc);
}getch();
}
int bin(int b[],int low,int high)
{
static int mid;
int i;
if(low<=high)
{
mid=(low + high)/2;
if(key<b[mid])
{
high = mid-1;
bin(b,low,high);
}
else if(key>b[mid])
{
low = mid+1;
bin(b,low,high);
}
else if(key==b[mid])
return(mid+1);
}
else
return(0);
}
33.Acending order:
#include<stdio.h>
#include<conio.h>
main()
{
int num[100],no,i,j,a;
clrscr();
printf("Enter Upper Limit:");
scanf("%d",&no);
printf("Enter the numbers");
for(i=0;i<no;i++)
scanf("%d",&num[i]);
for(i=0;i<no-1;i++)
{
for(j=i+1;j<no;j++)
{
if(num[i]<num[j])
{
a=num[i];
num[i]=num[j];
num[j]=a;
}
}
}
printf("\nThe ascending order of the given numbers");
for(i=0;i<no;i++)
printf("\n\t%d",num[i]);
printf("\n The descending number of the given numbers");
for(j=no-1;j>=0;j--)
printf("\n\t%d",num[j]);
getch();
}
36.Matrix addition:
#include<stdio.h>
#include<conio.h>
main()
{
int a[25][25],b[25][25],c[25][25],i,j,m,n;
clrscr();
printf("Enter the rows and column of two matrixes:\n");
scanf("%d %d",&m,&n);
printf("\nEnter the elements of A matrix:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
printf("nEnter the elements of B matrix:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&b[i][j]);
}
printf("\nThe elements of A matrix");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t%d",a[i][j]);
}
printf("\nThe elements of B matrix");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t%d",b[i][j]);
}
printf("\nThe addition of two matrixes");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf("\t%d",c[i][j]);
}
}
getch();
}
37.Matrix multiplication:
#include<stdio.h>
#include<conio.h>
main()
{
int a[25][25],b[25][25],c[25][25],i,j,k,r,s;
int m,n;
clrscr();
printf("\nEnter the Rows and Columns of A matrix:");
scanf("%d %d",&m,&n);
printf("\nEnter the Rows and Columns of B matrix:");
scanf("%d %d",&r,&s);
if(m!=r)
printf("\nThe matrix cannot multiplied");
else
{
printf("\nEnter the elements of A matrix");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("\t%d",&a[i][j]);
}
printf("\nEnter the elements of B matrix");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("\t%d",&b[i][j]);
}
printf("\nThe elements of A matrix");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t%d",a[i][j]);
}
printf("\n The elements of B matrix");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t%d",b[i][j]);
}
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
c[i][j]=0;
for(k=0;k<m;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
38.Transpose of matrix:
#include<stdio.h>
main()
{
int i,j,a[25][25],row,col;
printf("\nEnter the number of rows and column of matrix");
scanf("%d%d",&row,&col);
printf("\nEnter the elements of the matrix");
for(i=0;i<row;i++)
for(j=0;j<col;j++)
scanf("%d",&a[i][j]);
printf("The given matrix");
for(i=0;i<row;i++)
{
printf("\n");
for(j=0;j<col;j++)
printf("\t%d",a[i][j]);
}
printf("\nThe transpose of the given matrix");
for(i=0;i<row;i++)
{
printf("\n");
for(j=0;j<col;j++)
printf("\t%d",a[j][i]);
}
}
char temp[12];
int i, j;
for(i=1;i<n;i++)
for(j=2;j<n;j++)
if(strcmp(x[i],x[j])>0)
{
strcpy(temp,x[i]);
strcpy(x[i],x[j]);
strcpy(x[j ,temp);
}
return;
}
42.POINTER TO AN ARRAY:
# include<stdio.h>
# include<stdlib.h>
void main()
{
int m,n,i,j;
void readinput(int *a[10],int m,int n);
void computesum(int *a[10],int *b[10],int *c[10],int m,int n);
void writeoutput(int *c[10],int m,int n);
int *a[10],*b[10],*c[10];
4
#include<stdio.h>
#include<string.h>
void reorder(int n,char *x[]);
main()
{
int i,n=0;
char *x[10];
printf(" enter the string");
printf(type END for finish");
do
{
x[n]=(char*)malloc(12*sizeof(char));
printf(string %d",n+1);
scanf("%s",x[n]);
}while(strcmp(x[n++],"END");
reorder(n--,x);
printf(" reordered list of strings");
for(i=0;i<n;i++)
printf("string %d",i+1,x[i]]);
void reorder(int n,char *x[])
{
char *temp;
int i, j;
for(i=1;i<n;i++)
for(j=2;j<n;j++)
if(strcmp(x[i],x[j])>0)
{
temp=x[i];
x[i]=,x[j]);
x[j ]=temp;
}
return;
}
45.Call by reference:
#include<stdio.h>
#include<conio.h>
main()
{
int x,y;
clrscr();
printf("\nEnter the two numbers:");
scanf(" %d %d",&x,&y);
printf("\nBefore swapping:x=%d\ty=%d",x,y);
display(&x,&y);
printf("\nAfter swapping :x=%d\ty=%d",x,y);
getch();
}
display(int *a,int *b)
{
int t;
t=*a;
*a=*b;
*b=t;
}
printf("\n");
printf("NAME \tREGISTER-NO \tAVERAGE \tGRADE\n");
for(pt=stud;pt<stud+no;pt++)
{
printf("%-20s%-10s",pt->name,pt->regno);
printf("%10d \t %c\n",pt->avg,pt->grade);
}
getch();
}
)
47.Write a program using nested structure
#include<stdio.h>
void main()
{
struct date
{
int day, year;
char month[20];
};
struct employee
{
int code;
char name;
float salary;
struct data doj;
};
struct employee emp1;
printf("\n\n");
printf("Enter Employee Code :");
scanf("%d",&emp1.code);
printf("Enter Employee Name :");
scanf("%s",emp1.name);
printf("Enter Employee Salary :");
scanf("%f",&emp1.salary);
printf("Enter Employee Date of Joining :");
scanf("%d %s %d",&emp1.doj.day, emp1.doj.month, &emp1.doj.year);
printf("The Employee Code is %d",emp1.code);
printf("The Employee Name is %s",emp1.name);
printf("The Employee Salary is %f",emp1.salary);
printf("The Employee Date of Joining is %d %s %d",emp1.doj.day,
emp1.doj.month, emp1.doj.year);
}
while(a=='y')
{
printf("enter name , age,basic sal\n");
scanf("%s%d%d",e.name,&e.age,&e.bs);
fprintf(fp,"%s%d%d",e.name,e.age,e.bs);
printf("add another record(Y/N):");
a=getch();
}
break;
case '2': rewind(fp);
fscanf(fp,"%s%d%d",e.name,&e.age,&e.bs);
printf("\n%s\t%d\t%d",e.name,e.age,e.bs);
break;
case '3': a='Y';
while(a=='Y')
{
printf("enter name to modify");
scanf("%s",empname);
rewind(fp);
while(fread(&e,rsize,1,fp)==1)
{
if(strcmp(e.name,empname)==0)
{
printf("enter new name age & basic sal");
scanf("\n%s\t%d\t%d",e.name,&e.age,&e.bs);
fseek(fp,-rsize,SEEK_CUR);
fwrite(&e,rsize,1,fp);
break;
}
}
printf("modify another record(Y/N):");
a=getch();
}
break;
case '4': a='Y';
while(a=='Y')
{
printf("enter name to delete");
scanf("%s",empname);
ft=fopen("temp.txt","w+");
rewind(fp);
while(fread(&e,rsize,1,fp)==1)
if(strcmp(e.name,empname)!=0)
fwrite(&e,rsize,1,ft);
fclose(fp);
fclose(ft);
remove("emp.txt");
rename("temp.txt","emp.txt");
fp=fopen("emp.txt","r+");
printf("delete another record(Y/N):");
a=getch();
}
break;
case '0': fclose(fp);
exit();
}
getch();
}
}