1. LARGEST OF THREE
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c,res;
clrscr();
printf("Enter the value of A,B and C:");
scanf("%d %d%d",&a,&b,&c);
printf("The Largest Number is%d",res=(a>b?(a>c?a:c):(b>c?b:c)));
getch();
}
2. FIBNO
#include<stdio.h>
#include<conio.h>
main()
{
int i,f1,f2,f3,n;
clrscr();
printf("Enter the value of n: ");
scanf("%d",&n);
f1=-1; f2=1;
for(i=1;i<=n;i++)
{
f3=f1+f2;
printf(" %d",f3);
f1=f2;
f2=f3;
}
getch();
}
3. SORTING
#include <stdio.h>
#include <conio.h>
void main(void)
{
int a[50],i,n,j,t;
printf ("\nENTER THE VALUE OF N :");
scanf ("%d",&n);
printf ("\nENTER THE VALUES : ");
for (i=1;i<=n;i++)
scanf ("%d",&a[i]);
for (i=1;i<=n-1;i++)
{
for (j=i+1;j<=n;j++)
{
if (a[i] >= a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
for (i=1;i<=n;i++)
printf ("%d ",a[i]);
getch();
}
4. FACTORIAL
# include<stdio.h>
# include<conio.h>
void main()
{
int fact(int),val,a;
clrscr();
printf("enterthe number");
scanf("%d",&a);
val=fact(a);
printf("factorial%d",val);
getch();
}
int fact(int g)
{
int temp=1;
if (g==1)
{
return(1);
}
else
{
temp = temp * g*fact(g-1);
}
return(temp);
}
5. SWAPPING
#include<stdio.h>
#include<conio.h>
main()
{
int x,y;
clrscr();
printf("\nEnter the value of A and B:");
scanf("%d %d",&x,&y);
swap1(x,y);
swap2(x,y);
swap3(x,y);
getch();
}
swap1(int a,int b)
{
int t;
t=a;
a=b;
b=t;
printf("\nThe Value of A and B afterSwapping: %d %d",a,b);
}
swap2(int a1,int b1)
{
a1=a1+b1;
b1=a1-b1;
a1=a1-b1;
printf("\nThe Value of A and B afterswapping: %d %d",a1,b1);
}
swap3(int a2,int b2)
{
a2=a2^b2;
b2=a2^b2;
a2=a2^b2;
printf("\nThe Value of A and B afterswapping: %d %d",a2,b2);
}
6. PRIME OR NOT
#include<stdio.h>
#include<conio.h>
main()
{
int i,n,flag=0;
clrscr();
printf("\nEnter the value of n :");
scanf("%d",&n);
for(i=2;i<n-1;i++)
{
if (n%i==0)
{
flag=0;
break;
}
else
flag=1;
}
if (n==1)
printf("\nThe number is prime andcomposiste");
else if (flag==1)
printf ("\nPrime");
else
printf ("\nNot Prime");
getch();
}
7. STRING COUNT
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
void main(void)
{
int i,s=0,c=0,w=0,v=0,ch=0,d=0;
char name[500];
clrscr();
printf ("\nENTER THE STRING : ");
gets(name);
for (i=0;name[i]!='\0';i++)
{
ch++;
if ((name[i]=='a'||name[i]=='e'||name[i]=='i'||name[i]=='o'||name[i]=='u')||(name[i]=='A'||name[i]=='E'||name[i]=='I'||name[i]=='O'||name[i]=='U'))
v++;
else if ((name[i]>='a' &&name[i]<='z') || (name[i] >='A' && name[i]<='Z'))
c++;
else if(name[i]>='0'&&name[i]<='9')
d++;
else if (name[i]=='.')
s++;
else if (name[i]==' ' || name[i] == '\0')
w++;
}
printf ("\nVowels : %d",v);
printf ("\nCons : %d",c);
printf ("\nDigit : %d",d);
printf ("\ntotal : %d",ch);
printf ("\nSentence : %d",s);
printf ("\nword : %d",w);
getch();
}
8. MAX MIN
#include <stdio.h>
#include <conio.h>
void main(void)
{
int a[50],i,n,j,t,min=0,max=0;
printf ("\nENTER THE VALUE OF N :");
scanf ("%d",&n);
printf ("\nENTER THE VALUES : ");
for (i=1;i<=n;i++)
scanf ("%d",&a[i]);
for (i=1;i<=n-1;i++)
{
for (j=i+1;j<=n;j++)
{
if (a[i] > a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
for (i=1;i<=n;i++)
printf ("%d ",a[i]);
min = a[1];
max = a[n];
printf ("\nTHE MIN NUMBER :%d",min);
printf ("\nTHE MAX NUMBER :%d",max);
getch();
}
9. PALINDROME
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
void main()
{
char name[500],name1[500];
clrscr();
printf ("\nENTER THE STRING : ");
gets(name);
strcpy(name1,name);
strrev(name1);
printf ("\nGIVEN STRING :%s",name);
printf ("\nREVERSED STRING :%s",name1);
if (strcmp(name,name1)!=0)
printf ("\nNot PALINDROME");
else
printf ("\nPALINDROME");
getch();
}
10. DECIMAL TO BINARY
#include <stdio.h>
#include <conio.h>
void main()
{
int number,i,a[500],l;
clrscr();
printf ("ENTER A DECIMAL NUMBER :");
scanf ("%d",&number);
i=-1;
do
{
i++;
a[i]=number % 2;
number = number / 2;
printf ("%d",number);
}while (number!=0);
for (l=i;l>=0;l--)
printf ("\n%d",a[l]);
getch();
}
11. BINARY SEARCH
#include <stdio.h>
#include <conio.h>
void main()
{
int a[50],i,n,j,k,mid,low,high,x;
clrscr();
printf("\nEnter the value of n : ");
scanf ("%d",&n);
printf ("\nENTER THE NUMBER IN SORTEDORDER : ");
for (i=1;i<=n;i++)
scanf ("%d",&a[i]);
printf ("\nENTER THE NUMBER TO BE SEARCH: ");
scanf ("%d",&x);
low=1;
high=n;
while (low<=high)
{
mid = (low+high)/2;
if (x < a[mid])
high= mid-1;
else if (x>a[mid])
low = mid+1;
else
{
j=mid;
break;
}
j=0;
}
if (a[j]==x)
printf ("\nTHE NUMBER IS FOUND IN'%d' POSITION",j);
else
printf ("\nTHE NUMBER IS NOTFOUND");
getch();
}
12. STRING REVERSE WITHOUT STRING FUNCTIONS
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[20],str2[20];
int i,j=1,k,len;
clrscr();
printf("Enter the String: ");
scanf("%s",str1);
printf("\nThe given string:%s",str1);
len=strlen(str1);
for(i=len;i>=0;i--)
{
str2[j]=str1[i];
j=j+1;
}
printf("\nThe reversed String: ");
for(i=1;i<j;i++)
printf("%c",str2[i]);
getch();
}
13. STRING COPY WITHOUT STRCPY
#include<stdio.h>
#include<conio.h>
void main()
{
char *str1="Hellow";
char *str2;
clrscr();
str2=str1;
printf("%s",str2);
getch();
}
14. LINEAR SEARCH
#include <stdio.h>
#include <conio.h>
void main()
{
int a[50],i,n,j,k,mid,low,high,x,flag=0;
clrscr();
printf ("\nEnter the value of n :");
scanf ("%d",&n);
printf ("\nENTER THE NUMBER IN SORTEDORDER : ");
for (i=1;i<=n;i++)
scanf ("%d",&a[i]);
printf ("\nENTER THE NUMBER TO BE SEARCH: ");
scanf ("%d",&x);
for (i=1;i<=n;i++)
{
if (x==a[i])
{
printf ("\nTHE NUMBER IS FOUND '%d'POSITION",i);
flag =1;
break;
}
}
if (flag==0)
printf ("\nTHE NUMBER IS NOT FOUND");
getch();
}
15. ARMSTRONG NUMBER
#include<stdio.h>
#include<conio.h>
main()
{
int x,n,d,a;
x=0;
clrscr();
printf("Enter the no to chek");
scanf("%d",&n);
d=n;
do
{
a=n%10;
n=n/10;
x=(a*a*a)+x;
}while(n!=0);
if(x==d)
printf("The no isArmstrong no");
else
printf("The no isnot Armstrong no");
getch();
}
16. DOUBLE LINKED LIST
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#define null 0
struct doub
{
struct doub *llink;
int data;
struct doub *rlink;
}*head,*pt,*temp;
typedef struct doub node;
main()
{
char opt;
clrscr();
head=pt=(node *)malloc(sizeof(node));
printf("Enter the Element: ");
scanf("%d",&pt->data);
pt->llink=null;
pt->rlink=null;
do
{
printf("\ndo you want to cont (Y/N) :");
fflush(stdin);
scanf("%c",&opt);
if(opt=='Y'||opt=='y')
{
pt->rlink=(node*)malloc(sizeof(node));
printf("\nEnter the Element:");
scanf("%d",&pt->rlink->data);
pt->rlink->llink=pt;
pt=pt->rlink;
}
}while(opt=='Y'||opt=='y');
pt->rlink=null;
getch();
display();
getch();
head=(node *)malloc(sizeof(node));
temp->llink=null;
printf("\nEnter the item: ");
scanf("%d",&temp->data);
while (pt!=null)
{
temp->rlink=null;
temp->llink=pt;
pt=pt->rlink;
}
getch();
display();
getch();
}
display()
{
for(pt=head;pt!=null;pt=pt->rlink)
printf(" %d ",pt->data);
}
0 comments:
Post a Comment