Professional Documents
Culture Documents
100 Programs PDF
100 Programs PDF
66. Write a C program that will generate a table of values for the
equation;
f(X,Y) = 2ex3 + (23 + Y) x Where 1 < x < 5 with an increment 0.5 and 1
< y < 5 with an increment 0.25.
70. To find the longest word and its length in a give string.
71. Write program : To create data for 50 students ( roll, name, mark
1, mark 2, mark 3, termmark ) and then find the total for each student
and average mark of all students.
72. Write Programs : To round afloating point number to an indicate
decimal place e. g. 17.457 will be rounded to 17.46 to two decimal
places.
74. Write a c program to open a file named BPUT and write a line
of text in it by reading the text from the key board.
------------------------------------------------------
1. Program to calculate the factorial of a number
main()
{
int x,n;
printf("Enter a number :");
scanf("%d",&n);
x=fact(n);
printf("%d",x);
}
int fact(int n)
{
int f=1;
while(n>0)
{
f=f*n;
n--;
}
return f;
}
main()
{
int x,n;
printf("Enter a number :");
scanf("%d",&n);
x=sum_digit(n);
printf("%d",x);
}
int sum_digit(int n)
{
int s=0;
while(n>0)
{
s=s + n%10;
n=n/10;
}
return s;
}
3. Program to reverse a number
main()
{
int x,n;
printf("Enter a number :");
scanf("%d",&n);
x=reverse(n);
printf("%d",x);
}
int reverse(int n)
{
int s=0;
while(n>0)
{
s=s *10 + n%10;
n=n/10;
}
return s;
}
}
int daily_exp(int n)
{
int c=0;
while(n>0)
{
c++;
n=n-30;
}
return c;
}
main()
{
int x,n;
printf("Enter a number :");
scanf("%d",&n);
x=bit_count(n);
printf("%d ",x);
}
int bit_count(int n)
{
int c=0;
while(n>0)
{
c++;
n=n&n-1;
}
return c;
}
11. Program to calculate G.C.D of any two numbers
main()
{
int n1,n2,x;
printf("Enter two numbers:");
scanf("%d%d",&n1,&n2);
x=gcd(n1,n2);
printf("%d ",x);
}
int gcd(int n1,int n2)
{
while(n1!=n2)
{
if(n1>n2)
n1=n1-n2;
else
n2=n2-n1;
}
return n1;
}
12. Program to calculate L.C.M of two numbers.
main()
{
int n1,n2,x;
printf("Enter two numbers:");
scanf("%d%d",&n1,&n2);
x=lcm(n1,n2);
printf("%d ",x);
}
int lcm(int n1,int n2)
{
int x,y;
x=n1,y=n2;
while(n1!=n2)
{
if(n1>n2)
n1=n1-n2;
else
n2=n2-n1;
}
return x*y/n1;
}
13. Program to calculate fibonacci series
main()
{
int n;
printf("Enter the number range:");
scanf("%d",&n);
fibo(n);
}
int fibo(int n)
{
int i=0,j=1,k=2,r,f;
printf("%d %d ", i,j);
while(k<n)
{
f=i+j;
i=j;
j=f;
printf(" %d",j);
k++;
}
}
14. Program to calculate string palindrome
main()
{
char x[100],y[100];
printf("Enter a string :");
scanf("%s",x);
strcpy(y,x);
check_palindrome(x);
if(strcmp(x,y)==0)
printf("Palindrome");
else
printf("Not Palindrome");
}
int check_palindrome(char *x)
{
int len=strlen(x);
int i;
char temp;
for(i=0;i<len/2;i++)
{
temp=x[i];
x[i]=x[len-i-1];
x[len-i-1]=temp;
}
}
15. Program to check the number is Prime number or not
main()
{
int n,k;
printf("Enter a number:");
scanf("%d",&n);
k=check_prime(n);
if(k==2)
printf("Prime");
else
printf("Not prime");
}
int check_prime(int n)
{
int i=1,c=0;
while(i<=n)
{
if(n%i==0)
c++;
i++;
}
return c;
}
16. Program to find largest number in an array
main()
{
int a[]={15,67,25,90,40};
int k;
k=large_number(a);
printf("%d ",k);
}
int large_number(int a[5])
{
int i,big;
big=a[0];
for(i=1;i<5;i++)
{
if(big<a[i])
big=a[i];
}
return big;
}
17. Program to find Second largest number in an array
main()
{
int a[]={15,67,25,90,40};
int k;
k=large_number(a);
printf("%d ",k);
}
int large_number(int un[5])
{
int big1,big2;
int i;
big1 = un[0];
for ( i=1;i<5;++i )
if ( big1 < un[i] )
big1 = un[i];
if ( big1!=un[0] )
big2=un[0];
else
big2=un[1];
for(i=1; i<5;++i )
if (big1!=un[i] && big2 < un[i] )
big2=un[i];
return big2;
}
18. Program to remove duplicate elements in an array
main()
{
int i,k;
int x[10]={5,7,2,8,9,3,3,6,7,20};
k=remove_duplicate(x);
for(i=0;i<k;i++)
{
printf(" %d",x[i]);
}
}
int remove_duplicate(int p[10])
{
int size=10,i,j,k;
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
if(i==j)
{
continue;
}
else
if(*(p+i)==*(p+j))
{
k=j;
size--;
while(k < size)
{
*(p+k)=*(p+k+1);
k++;
}
j=0;
}
}
}
return size;
}
}
int decimal_binary(int n)
{
int m,no=0,a=1,rem;
m=n;
while(n!=0)
{
rem=n%2;
no=no+rem*a;
n=n/2;
a=a*10;
}
printf("%d",no);
}
20. Program to convert binary to decimal
main()
{
int n;
printf("Enter data in binary format :");
scanf("%d",&n);
binary_decimal(n);
}
int binary_decimal(int n)
{
int j=1,rem,n1=0;
while(n!=0)
{
rem=n%10;
n1=n1+rem*j;
j=j*2;
n=n/10;
}
printf("%d",n1);
}
}
int check_perfect(int n)
{
int s=0,i=1;
while(i<n)
{
if(n%i==0)
s=s+i;
i++;
}
return s;
}
22.Program to find generic root of a number.
main()
{
int n,k;
printf("Enter a number");
scanf("%d",&n);
k=generic(n);
printf("%d",k);
}
int generic(int n)
{
int sum,r;
if(n<10)
return n;
while(n>10)
{
sum=0;
while(n!=0)
{
r=n%10;
n=n/10;
sum+=r;
}
if(sum>10)
n=sum;
else
break;
}
return sum;
}
for(i=0;i<strlen(x);i++)
{
if(x[i]=='a' || x[i]=='A' || x[i]=='e' || x[i]=='E' ||
x[i]=='i' || x[i]=='I'
|| x[i]=='o' || x[i]=='O' ||
x[i]=='u' || x[i]=='U')
vowels++;
if(x[i]>=48 && x[i]<=57)
digit++;
if(x[i]==32) //space
space++;
}
word=space+1;
printf("%d %d %d %d\n",word,space,digit,vowels);
for(i=0;i<5;i++)
{
for(j=0;j<3;j++)
{
printf("%d",b[i][j]);
}
printf("\n");
}
}
31. Program to check a matrix is sparse matrix or not
main()
{
int mat[3][5]={
1,0,2,3,0,
0,1,0,1,0,
0,0,0,1,0
};
int i,j,nzero=0,zero=0;
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
{
if(mat[i][j]!=0)
nzero++;
else
zero++;
}
}
if(zero>nzero)
printf("The matrix is sparse matrix");
else
printf("Not a sparse matrix");
}
#include "stdio.h"
main()
{
int n,k;
int i=1,s1=0,s2=0;
for(k=1;k<=1000;k++)
{
n=k;
while(i<n)
{
if(n%i==0)
s1=s1+i;
i++;
}
i=1;
if(s1==n)
continue;
while(i<s1)
{
if(s1%i==0)
s2=s2+i;
i++;
}
if(n==s2)
printf("%d \n",n);
s1=0;
s2=0;
}
}
#include "math.h"
int main()
{
double a,b,c,s;
double area;
s = (a + b + c)/2;
area =sqrt (s*(s-a)*(s-b)*(s-c));
printf("%lf",area);
}
main()
{
int a[5]={4,9,40,2,25};
int i;
insert_sort(a);
for(i=0;i<5;i++)
printf("%d ",a[i]);
}
int insert_sort(int a[5])
{
int i,j,k,temp;
for(i=1;i<5;i++)
{
for(j=0;j<i;j++)
{
if(a[i]<a[j])
{
temp=a[i];
for(k=i;k>j;k--)
{
a[k]=a[k-1];
}
a[j]=temp;
}
}
}
}
38. Program for quick sort
main()
{
int x[5]={5,9,2,20,6};
int i;
quick_sort(x,0,4);
for(i=0;i<5;i++)
printf("%d ",x[i]);
}
quick_sort(int x[10],int first,int last)
{
int pivot,j,temp,i;
if(first<last)
{
pivot=first;
i=first;
j=last;
while(i<j)
{
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j)
{
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quick_sort(x,first,j-1);
quick_sort(x,j+1,last);
}
}
39. Program for Merge Sort
main()
{
int a[]={6,5,3,20,55};
int i,flag=0,n;
printf("Enter number to search :");
scanf("%d",&n);
for(i=0;i<5;i++)
{
if(a[i]==n)
{
flag=1;
break;
}
}
if(flag==1)
printf("Found\n");
else
printf("Not found\n");
}
41. Program for Sequential search using linked list
struct xxx
{
int data;
struct xxx *ad;
};
struct xxx *create_linkedlist();
main()
{
int k;
struct xxx *b;
b=create_linkedlist();
k=search(b);
if(k==1)
printf("Found ");
else
printf("Not found\n");
}
struct xxx *create_linkedlist()
{
char ch[10];
struct xxx *p,*q,*r;
p=malloc(sizeof(struct xxx));
printf("Enter data :");
scanf("%d",&p->data);
r=p;
while(1)
{
printf("Do u continue yes/no :");
scanf("%s",ch);
if(strcmp(ch,"no")==0)
break;
q=malloc(sizeof(struct xxx));
p->ad=q;
p=q;
printf("Enter data :");
scanf("%d",&p->data);
}
p->ad=0;
return r;
}
int search(struct xxx *p)
{
int n,flag=0;
printf("Enter number to search :");
scanf("%d",&n);
while(p!=0)
{
if(p->data==n)
{
flag=1;
break;
}
p=p->ad;
}
return flag;
}
void main()
{
int a[10],i,n,m,c=0,l,u,mid;
printf("Enter the size of an array->");
scanf("%d",&n);
printf("\nEnter the elements of the array->");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nThe elements of an array are->");
for(i=0;i<n;i++)
{
printf(" %d",a[i]);
}
printf("\nEnter 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;
break;
}
else
if(m<a[mid])
{
u=mid-1;
}
else
l=mid+1;
}
if(c==0)
printf("\nThe number is not in the list");
else
printf("\nThe number is found");
}
43. Program for Binary search using linked list
struct xxx
{
int roll;
struct xxx *ad;
};
struct xxx *create();
main()
{
int status;
int u,n;
struct xxx *b;
b=create();
visit(b);
selection_sort(b);
printf("\n");
visit(b);
u=count(b);
printf("Enter number to search :");
scanf("%d",&n);
status=binary_search(b,1,u,n);
if(status==1)
printf("Found\n");
else
printf("Not found\n");
}
int binary_search(struct xxx *p,int l,int u,int n)
{
struct xxx *r=p;
int mid,i,f=0;
while(l<=u && r!=0)
{
mid=(l+u)/2;
for(i=1;i<mid;i++)
{
r=r->ad;
}
if(n==r->roll)
{
f=1;
break;
}
if(n<r->roll)
{ u=mid-1;
}
if(n>r->roll)
{
l=mid+1;
}
r=p;
}
return f;
}
}
int visit(struct xxx *p)
{
while(p!=0)
{
printf("%d ",p->roll);
p=p->ad;
}
}
struct xxx *create()
{
struct xxx *p,*q,*r;
char ch[10];
p=malloc(sizeof(struct xxx));
r=p;
while(1)
{
printf("Enter roll :");
scanf("%d",&p->roll);
}while(x!=0);
}
int pop()
{
if(p==0)
{
printf("Stack is empty\n");
return;
}
else
{
printf("%d \n",p->roll);
p=p->ad;
}
}
int push()
{
struct xxx *q;
if(p==0)
{
p=malloc(sizeof(struct xxx));
printf("Enter roll :");
scanf("%d",&p->roll);
p->ad=0;
}
else
{
q=malloc(sizeof(struct xxx));
q->ad=p;
p=q;
printf("Enter roll :");
scanf("%d",&p->roll);
}
}
for(i=strlen(x)-1;i>=0;i--)
{
if(*p>=48 && *p<=57)
{
if(opd==0)
{
opd=malloc(sizeof(struct xxx));
opd->data=*p;
opd->ad=0;
}
else
{
q=malloc(sizeof(struct xxx));
q->ad=opd;
opd=q;
opd->data=*p;
}
}
else
{
if(opr==0)
{
opr=malloc(sizeof(struct xxx));
opr->data=*p;
opr->ad=0;
}
else
{
k1=check_precedence(*p);
k2=check_precedence(opr->data);
if(k1<=k2)
{
while(k1<=k2 && opr!=0)
{
q=malloc(sizeof(struct xxx));
q->ad=opd;
opd=q;
opd->data=opr->data;
opr=opr->ad;
if(opr==0)
{ opr=malloc(sizeof(struct xxx));
opr->ad=0;
break;
}
k2=check_precedence(opr->data);
}
opr->data=*p;
}
else
{
q=malloc(sizeof(struct xxx));
q->ad=opr;
opr=q;
opr->data=*p;
}
}//else
} //else
p--;
} //for
while(opr!=0)
{
q=malloc(sizeof(struct xxx));
q->ad=opd;
opd=q;
opd->data=opr->data;
opr=opr->ad;
}
memset(y,0,sizeof(y));
i=0;
while(opd!=0)
{
y[i]=opd->data;
i++;
opd=opd->ad;
}
printf("%s ",y);
}
int check_precedence(int m)
{
switch(m)
{
case '+':
case '-':
return 1;
case '/':
case '*':
return 2;
}
}
46. Program to Evaluate postfix notation
struct xxx
{
unsigned char data;
struct xxx *ad;
};
main()
{
unsigned char a,b,c;
struct xxx *st=0,*q;
char x[100];
printf("Enter Post Expression:");
scanf("%s",x);
char *p=x;
while(*p!=0)
{
if(*p>=48 && *p<=57)
{
if(st==0)
{
st=malloc(sizeof(struct xxx));
st->data=*p;
st->ad=0;
}
else
{
q=malloc(sizeof(struct xxx));
q->ad=st;
st=q;
st->data=*p;
}
}
else
{
b=st->data-48;
st=st->ad;
a=st->data-48;
if(*p=='*')
{
c=a*b;
}
else
if(*p=='/')
{
c=a/b;
}
else
if(*p=='+')
{
c=a+b;
}
else
if(*p=='-')
{
c=a-b;
}
st->data=c+48;
}
p++;
}
printf("%d ", st->data-48);
}
47. Program to implement Queue using linked list
#include<stdio.h>
#include<malloc.h>
struct node
{
int info;
struct node *next;
} ;
struct node *front, *rear;
void enqueue(int elt);
int dequeue();
void display();
void main()
{
int ch, elt;
rear = NULL;
front = NULL;
while (1)
{
printf("1 Insert\n");
printf("2 Delete\n");
printf("3 Display\n");
printf("4 Exit\n");
printf("Enter your choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter The Element Value\n");
scanf("%d", &elt);
enqueue(elt);
break;
case 2:
elt = dequeue();
printf("The deleted element = %d\n", elt);
break;
case 3:
display();
break;
default:
exit(0);
}
}
}
void enqueue(int elt)
{
struct node *p;
p = (struct node*)malloc(sizeof(struct node));
p->info = elt;
p->next = NULL;
if (rear == NULL || front == NULL)
front = p;
else
rear->next = p;
rear = p;
}
int dequeue()
{
struct node *p;
int elt;
if (front == NULL || rear == NULL)
{
printf("\nUnder Flow");
exit(0);
}
else
{
p = front;
elt = p->info;
front = front->next;
free(p);
}
return (elt);
}
void display()
{
struct node *t;
t = front;
while (front == NULL || rear == NULL)
{
printf("\nQueue is empty");
exit(0);
}
while (t != NULL)
{
printf("->%d", t->info);
t = t->next;
}
}
struct xxx
{
int pr;
struct xxx *ad;
};
struct xxx *p=0;
main()
{
int x;
do
{
printf("1 for add\n");
printf("2 for dele\n");
printf("3 for traverse\n");
printf("0 for stop\n");
printf("Enter choice :");
scanf("%d",&x);
if(x==1)
add();
else
if(x==2)
dele();
else
if(x==3)
trav();
}while(x!=0);
}
int trav()
{
struct xxx *r=p;
while(r!=0)
{
printf("%d ",r->pr);
r=r->ad;
}
}
int add()
{
struct xxx *q,*r,*m;
if(p==0)
{
p=malloc(sizeof(struct xxx));
printf("Enter priority :");
scanf("%d",&p->pr);
p->ad=0;
}
else
{
q=malloc(sizeof(struct xxx));
printf("Enter priority :");
scanf("%d",&q->pr);
if(q->pr <= p->pr)
{
q->ad=p;
p=q;
}
else
{
r=p;
while(q->pr > r->pr)
{
m=r;
if(r->ad==0)
{
r->ad=q;
q->ad=0;
return;
}
r=r->ad;
}
m->ad=q;
q->ad=r;
}
}
}
int dele()
{
struct xxx *r,*m;
r=p;
if(r==0)
{
printf("Stack is empty\n");
exit(0);
}
while(r->ad!=0)
{
m=r;
r=r->ad;
}
free(r);
m->ad=0;
}
50. Traverse a linked list in reverse order
#include <stdio.h>
#define NODES 4
typedef struct list_head {
struct node* head;
struct node* tail;
} list_head;
node n[NODES];
list_head init_empty_list()
{
list_head h;
h.head = 0;
h.tail = 0;
return h;
}
list_head init_list()
{
int i;
list_head h;
for (i=0; i<NODES-1; ++i) {
n[i].next = &n[i+1];
n[i].value = i+1;
}
n[NODES-1].next = 0;
n[NODES-1].value = NODES;
h.head = &n[0];
h.tail = &n[NODES-1];
return h;
}
void print_list(list_head h)
{
node* p = h.head;
while (p) {
printf("%d ", p->value);
p = p->next;
}
printf("\n");
}
list_head reverse_list(list_head h)
{
list_head nh;
if ((h.head == 0) || (h.head->next ==0)) {
return h;
}
nh.head = h.head->next;
nh.tail = h.tail;
nh = reverse_list(nh);
h.head->next = 0;
nh.tail->next = h.head;
nh.tail = h.head;
return nh;
}
int main()
{
list_head head;
head = init_list();
print_list(head);
head = reverse_list(head);
print_list(head);
return 0;
}
#include "fcntl.h"
main(int x, char *y[], char *z[])
{
int i;
char ch;
if(x<2)
{
printf("Too few parameters\n");
exit(0);
}
for(i=1;i<x;i++)
{
int k=open(y[i],O_RDONLY);
if(k==-1)
{
printf("File not found\n");
break;
}
while(read(k,&ch,1))
printf("%c",ch);
close(k);
}
}
52. Calculate the age of a person after giving the date of the birth.
#include "time.h"
main()
{
char x[100],y[100];
int dd,mm,yy;
int bd,bm,by;
int nd,nm,ny;
unsigned int t;
struct tm *mytime;
t=time(0);
mytime=localtime(&t);
dd=mytime->tm_mday;
mm=mytime->tm_mon+1;
yy=mytime->tm_year+1900;
if(dd>=bd)
nd=dd-bd;
else
if(mm==1 || mm==3 || mm==5 || mm==7 || mm==8 || mm==10 || mm==12)
{
dd=dd+31;
nd=dd-bd;
}
else
if(mm==4 || mm==6 || mm==9 || mm==11)
{
dd=dd+30;
nd=dd-bd;
}
else
if(mm==2)
{
dd=dd+28;
nd=dd-bd;
}
if(mm>=bm)
{
nm=mm-bm;
}
else
{
mm=mm+12;
yy=yy-1;
nm=mm-bm;
}
ny=yy-by;
printf("%d %d %d \n",ny,nm,nd);
}
53. Cacluate the average marks of student looking at the following
table
p1 p2 p3 average Result
>=60% first
>=50 second
>=40 third
avg=(p1+p2+p3)/3;
where p1,p2 and p3 >=30
main()
{
int p1,p2,p3,avg;
avg=(p1+p2+p3)/3;
54. Cacluate the amount to be paid after giving the total time.
Time Amount
8 hours 100/-
Next 4 hours 20/- ph
Next 4 hours 30/- ph
Next 4 hours 40/- ph
main()
{
int time,amt;
printf("Enter Total Time :");
scanf("%d",&time);
if(time==8)
amt=100;
else
if(time>8 && time<=12)
amt=100+(time-8)*20;
else
if(time>12 && time<=16)
amt=180+(time-12)*30;
else
if(time>16 && time<=20)
amt=300+(time-16)*40;
else
{
printf("Invalid entry\n");
exit(0);
}
printf("%d", amt);
}
55. Program to convert upper case to lower case
void main()
{
char str[20];
int i;
printf("Enter any string->");
scanf("%s",str);
printf("The string is->%s",str);
for(i=0;i<=strlen(str);i++)
{
if(str[i]>=65&&str[i]<=90)
str[i]=str[i]+32;
}
printf("\nThe string in uppercase is->%s",str);
}
56. Program to calculate the power of a number
void main()
{
int pow,num,i=1;
long int sum=1;
printf("\nEnter a number: ");
scanf("%d",&num);
printf("\nEnter power: ");
scanf("%d",&pow);
while(i<=pow)
{
sum=sum*num;
i++;
}
printf("\n%d to the power %d is: %ld",num,pow,sum);
void main()
{
int a[50],size,num,i,pos,temp;
printf("\nEnter size of the array: ");
scanf("%d",&size);
printf("\nEnter %d elements in to the array: ",size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);
printf("\nEnter position and number to insert: ");
scanf("%d %d",&pos,&num);
i=0;
while(i!=pos-1)
i++;
temp=size++;
while(i<temp)
{
a[temp]=a[temp-1];
temp--;
}
a[i]=num;
for(i=0;i<size;i++)
printf(" %d",a[i]);
}
}
main()
{
int serise,square,fact=1,loop=1,loop1;
float sum;
float result=0;
printf("Enter The serise ");
scanf("%d",&serise);
while(loop<=serise)
{
square=pow(loop,2);
for(loop1=1;loop1<=loop;loop1++)
{
fact = fact * loop1;
}
sum=(float)square/fact;
if(loop%2!=0)
result = result + sum;
else
result = result - sum;
fact=1;
loop++;
}
printf("The summation Of the serise is %f\n",result);
}
#include "time.h"
#include "stdio.h"
main()
{
char daybuf[20];
int dd,mm,yy;
unsigned int t;
struct tm *mytime;
t=time(0);
mytime=localtime(&t);
dd=mytime->tm_mday;
mm=mytime->tm_mon+1;
yy=mytime->tm_year+1900;
mytime->tm_hour = 0;
mytime->tm_min = 0;
mytime->tm_sec = 1;
mytime->tm_isdst = -1;
if(mktime(mytime) == -1)
fprintf(stderr, "Unkown -\n");
else
strftime(daybuf, sizeof(daybuf), "%A", mytime);
#include<stdio.h>
int main()
{
char input[16];
char p[4][4];
int i,a,b,len;
int j,c;
printf("pl enter any ip address\n");
scanf("%s",input);
a=0;b=0;
for(i=0;input[i];i++)
{
if(input[i]!='.')
p[a][b++]=input[i];
else
{
p[a][b]=0;
a++;
b=0;
}
}
p[a][b]=0;
for(a=0;a<4;a++)
{
len=strlen(p[a]);
for(j=len-1;j>=0;j--)
p[a][j+3-len]=p[a][j];
for(j=0;j<(3-len);p[a][j]='0',j++);
p[a][3]=0;
}
input[0]=0;
for(i=0;i<4;i++)
{
strcat(input,p[i]);
strcat(input,".");
}
input[15]=0;
puts(input);
return 0;
}
61.Caclulate the net salary looking at the following table
Basic TA DA PF NET
>=20000 500 200 200 ?
>=10000 200 300 100 ?
>=500 100 100 100 ?
main()
{
int basic,ta,da,pf,net;
printf("Enter basic Salary :");
scanf("%d",&basic);
if(basic>=20000)
{
ta=500;
da=200;
pf=200;
}
else
if(basic>=10000)
{
ta=200;
da=300;
pf=100;
}
else
if(basic>=5000)
{
ta=100;
da=100;
pf=100;
}
else
{
printf("Invalid entry");
exit(0);
}
net=(basic+ta+da)-pf;
printf("%d", net);
}
main()
{
struct student details[SIZE],temp;
int loop,outer,inner;
for(loop = 0;loop<SIZE;loop++)
{
printf("Enter name :");
scanf("%s",details[loop].name);
printf("Enter roll :");
scanf("%d",&details[loop].roll);
printf("Entere Age: ");
scanf("%d",&details[loop].age);
printf("Enter address: ");
scanf("%s",details[loop].p.addr);
printf("Enter Phone number:");
scanf("%d",&details[loop].p.phoneno);
}
printf("Before Sorting\n");
for(loop = 0;loop < SIZE;loop++)
{
printf("%s\t%d\t%d\t%s\t%d\n",details[loop].name,details[loop].ro
ll,details[loop].age,details[loop].p.addr,details[loop].p.phoneno);
}
printf("\n");
for(outer = 0;outer < SIZE;outer++)
{
for(inner = outer + 1;inner < SIZE;inner++)
{
if(details[outer].roll > details[inner].roll)
{
temp = details[outer];
details[outer] = details[inner];
details[inner] = temp;
}
}
}
printf("After Sorting\n");
for(loop = 0;loop < SIZE;loop++)
{
printf("%s\t%d\t%d\t%s\t%d\n",details[loop].name,details[loop].ro
ll,details[loop].age,details[loop].p.addr,details[loop].p.phoneno);
}
printf("\n");
}
#include "fcntl.h"
#include "stdio.h"
struct student
{
char name[30];
int roll;
int age;
}__attribute__((__packed__));
main()
{
int handlerStructure, numberOfStudent, loop;
handlerStructure = open("studentDetails.txt",O_RDWR);
//write(handlerStructure,&details,sizeof(struct student));
write(handlerStructure,&details[loop].name,strlen(details[loop].n
ame));
write(handlerStructure,&details[loop].roll,sizeof(details[loop].r
oll));
write(handlerStructure,&details[loop].age,sizeof(details[loop].ag
e));
}
loop = 0;
while(read(handlerStructure,&details[loop],sizeof(struct
student)))
{
write(1,&details[loop].name,strlen(details[loop].name));
write(1,&details[loop].roll,sizeof(details[loop].roll));
write(1,&details[loop].age,sizeof(details[loop].age));
loop++;
}
}
65. Write a program to count no. of paragraphs, lines, words and
characters present in a file.
78. Program for DFS
struct xxx
{
int node;
int total;
int status;
struct xxx *ad;
};
struct xxx p[5];
main()
{
struct xxx *q=0,*r,*m;
int a[5][5]={
0,1,1,1,0,
0,0,0,0,1,
0,0,0,1,0,
0,1,0,0,1,
0,0,0,0,0
};
int i,j,k;
int result[5];
memset(p,0,sizeof(p));
//create adjacence list
for(i=0;i<5;i++)
{
p[i].node=i+1;
for(j=0;j<5;j++)
{
if(a[i][j]==1)
p[i].total++;
}
for(k=1;k<=p[i].total;k++)
{
printf("Enter adjacence node for %d\n",p[i].node);
if(q==0)
{
q=malloc(sizeof(struct xxx));
printf("Enter adjacence node :");
scanf("%d",&q->node);
q->ad=0;
p[i].ad=q;
}
else
{
r=malloc(sizeof(struct xxx));
printf("Enter adjacence node :");
scanf("%d",&r->node);
q->ad=r;
q=r;
r->ad=0;
}
}
q=0;
p[i].total=0;
}
//traverse adjacence list
for(i=0;i<5;i++)
{
printf("%d : ",p[i].node);
if(p[i].ad!=0)
{
r=p[i].ad;
while(r!=0)
{
printf("%d -> ",r->node);
r=r->ad;
}
printf("\n");
}
}
q=malloc(sizeof(struct xxx));
printf("Enter source :");
scanf("%d",&q->node);
set_status(q->node);
p[q->node-1].status=1;
q->ad=0;
while(1)
{
r=p[q->node-1].ad;
if(r==0)
break;
p[q->node-1].status=1;
while(r!=0)
{
if(r->status==0)
{
m=malloc(sizeof(struct xxx));
m->node=r->node;
set_status(r->node);
m->ad=q;
q=m;
}
r=r->ad;
}
while(q!=0)
{
printf("%d ",q->node);
q=q->ad;
}
}
set_status(int n)
{
struct xxx *r;
int i;
for(i=0;i<5;i++)
{
r=p[i].ad;
while(r!=0)
{
if(r->node==n)
r->status=1;
r=r->ad;
}
}
while(f!=0)
{
printf("%d ",f->node);
f=f->ad;
}
}
set_status(int n)
{
struct xxx *r;
int i;
for(i=0;i<5;i++)
{
r=p[i].ad;
while(r!=0)
{
if(r->node==n)
r->status=1;
r=r->ad;
}
}
}
j=0;
printf("Enter number to search :");
scanf("%d",&k);
pos=(k%m+j)%m;
if(k==x[pos])
flag=1;
else
{
while(pos<m)
{
pos++;
if(x[pos]==k)
{
flag=1;
break;
}
}
}
if(flag==1)
printf("Found\n");
else
printf("Not found\n");
}
}
j=0;
printf("Enter search string :");
scanf("%s",p);
k=p[0];
pos=(k%m+j)%m;
if(strcmp(x[pos],p)==0)
f=1;
else
while(j<m)
{
j++;
pos=(k%m+j)%m;
if(strcmp(x[pos],p)==0)
{
f=1;
break;
}
}
if(f==1)
printf("Found");
else
printf("Not found");
}
struct xxx
{
int data;
struct xxx *ad;
};
main()
{
struct xxx p[10],*q,*r;
int i,k,m=10,pos,flag=0;
memset(p,0,sizeof(p));
for(i=0;i<10;i++)
{
printf("Enter element :");
scanf("%d",&k);
pos=k%m;
if(p[pos].data==0)
{
p[pos].data=k;
}
else
{
if(p[pos].ad==0)
{
q=malloc(sizeof(struct xxx));
p[pos].ad=q;
q->data=k;
q->ad=0;
}
else
{
r=p[pos].ad;
while(r->ad!=0)
{
r=r->ad;
}
q=malloc(sizeof(struct xxx));
q->data=k;
q->ad=0;
r->ad=q;
}
}
}
printf("Enter number to search:");
scanf("%d",&k);
pos=k%m;
if(k==p[pos].data)
flag=1;
else
{
r=p[pos].ad;
while(r!=0)
{
if(k==r->data)
{
flag=1;
break;
}
r=r->ad;
}
}
if(flag==1)
printf("Found\n");
else
printf("Not found\n");
}
83. Shortmest path using warshal's algorith
main()
{
int a[6][6]={
0,5,3,99,1,99,
99,0,99,5,99,99,
99,99,0,99,2,2,
99,99,3,0,99,3,
99,2,99,1,0,99,
99,99,99,99,99,0
};
int i,j,k;
int p[6][6];
for(i=0;i<6;i++)
{
for(j=0;j<6;j++)
{
if(a[i][j]==0)
p[i][j]=0;
else
p[i][j]=a[i][j];
}
}
//find shortest path
for(k=0;k<6;k++)
{
for(i=0;i<6;i++)
{
for(j=0;j<6;j++)
{
if(p[i][j]<=p[i][k] + p[k][j])
p[i][j]=p[i][j];
else
p[i][j]=p[i][k]+p[k][j];
}
}
}
//traverse shortest path
for(i=0;i<6;i++)
{
for(j=0;j<6;j++)
{
printf("%d ",p[i][j]);
}
printf("\n");
}
}
84. Shortest path using dijkstra's algorith
main()
{
int x[8];
int y[8];
int i,j,label,s,c=1;
int flag[8];
int a[8][8]={
0,100,3,100,100,100,3,100,
100,0,100,100,100,100,2,5,
100,100,0,3,100,100,100,100,
100,100,100,0,100,1,100,100,
100,2,100,100,0,2,100,2,
100,100,100,100,100,0,6,1,
100,2,4,4,1,100,0,100,
100,100,100,5,100,100,100,0
};
memset(flag,0,sizeof(flag));
printf("Enter source:");
scanf("%d",&label);
flag[label]=1;
for(i=0;i<8;i++)
{
x[i]=a[label][i];
}
while(c<8)
{
label=smallest(x,flag);
s=x[label];
for(i=0;i<8;i++)
{
y[i]=a[label][i]+s;
if(x[i]>y[i])
x[i]=y[i];
}
flag[label]=1;
c++;
}
for(i=0;i<8;i++)
{
printf("%d ",x[i]);
}
}
main()
{
int a[6][6]={
0,1,1,0,1,0,
0,0,0,1,0,0,
0,0,0,0,1,1,
0,0,1,0,0,1,
0,1,0,1,0,0,
0,0,0,0,0,0
};
int i,j,k;
int p[6][6];
for(i=0;i<6;i++)
{
for(j=0;j<6;j++)
{
if(a[i][j]==0)
p[i][j]=0;
else
p[i][j]=1;
}
}
for(k=0;k<6;k++)
{
for(i=0;i<6;i++)
{
for(j=0;j<6;j++)
{
p[i][j]=p[i][j]|(p[i][k] & p[k][j]);
}
}
}
//visit path matrix
for(i=0;i<6;i++)
{
for(j=0;j<6;j++)
{
printf("%d ",p[i][j]);
}
printf("\n");
}
}
86. Program to read the current date and time
#include<time.h>
main()
{
struct tm *k;
unsigned long int t;
int h,m,s;
int dd,mm,yy;
t = time(0);
k = localtime(&t);
h = k->tm_hour;
m = k->tm_min;
s = k->tm_sec;
dd = k->tm_mday;
mm = k->tm_mon+1;
yy = k->tm_year+1900;
printf("Time : %d : %d : %d\n",h,m,s);
printf("Date : %d : %d : %d\n",dd,mm,yy);
}
#include "dirent.h"
main()
{
DIR *p;
struct dirent *q;
p = opendir("/home/satyam/Desktop/demo");
while((q=readdir(p)) != 0)
{
if(q->d_name[0] != '.')
printf("%s\n",q->d_name);
}
}