Advanced Programming in C

Please note: ADD header files stdio.h and conio.h in all programs



Program-1 Write a C program to check the no. is Palindrome or not using UDF.

int palindrom(int);
void main()
{
int no,p;
clrscr();
printf("\n\nEnter number: ");
scanf("%d",&no);
p=palindrom(no);
printf("\n%d",p);
getch();
}
int palindrom(int n)
{
int a,rev=0;
while(n>0)
{
a=n%10;
rev=(rev*10)+a;
n=n/10;
}
return rev;
}


2. Write a program to find factorial of given no using UDF.

int factorial(int);
void main()
{
int no,f;
clrscr();
printf("\n\nenter no: ");
scanf("%d",&no);
f=factorial(no);
printf("\n\nanswer: %d",f);
getch();
}
int factorial(int n)
{
int i,ans=1;
for(i=1;i<=n;i++) { ans=ans*i; } return ans; }

 3. Write a program to find factorial of given no using recursion.
 int factorial(int);
void main()
{
 int n,f;
printf("\n\nEnter no: ");
scanf("%d",&n);
 f=factorial(n);
printf("\nfactorial: %d",f);
getch();
}
int factorial(int no)
{
 int fact=1;
 if(no==1)
{
 return 1;
 }
 else
{
fact=no*(factorial(no-1)); return fact;
}
}
 4. Write a program to display first 25 terms of Fibonacci series using recursion.
int fibonacci(int);
void main()
{
int i=1,c,n;
clrscr();
 printf("\nEnter number: ");
scanf("%d",&n);
for(c=1;c<=n;c++)
{
 printf("\n%d",fibonacci(i));
 i++;
 }
 getch();
}
 int fibonacci(int n)
{
 if(n==0)
{
 return 0;
}
else if(n==1)
 {
 return 1;
 } else { return(fibonacci(n-1)+fibonacci(n-2)); } }

 5. Write a program using a recursive function to find the GCD(Greatest Common Divisor) of two Positive integer numbers.

 int gcd(int,int); void main() { int x,y,z; clrscr(); scanf("%d %d",&x,&y); z=gcd(x,y); printf("\n\n%d",z); getch(); } int gcd(int a,int b) { int r; if(a%b==0) { return b; } else { printf("\nb=%d r=%d",b,a%b); r=a%b; return gcd(b,r); } }

 6.Write a program to swap value of two integer number using UDF

void swap(int,int);
void main()
 {
int x,y;
clrscr();
printf("\n\nEnter first number: ");
scanf("%d",&x);
printf("\n\nEnter second number: ");
scanf("%d",&y); swap(x,y);
getch();
}
void swap(int a,int b)
{
int temp;
temp=a;
 a=b;
b=temp;
printf("\n\nFirst value: %d",a);
printf("\n\nSecond value: %d",b);
}
 7. Write a function prime that returns 1 if its argument is a prime and return zero Otherwise.

int check_prime(int);
void main()
{
int no,result;
printf("\n\nEnter number: ");
scanf("%d",&no);
 result=check_prime(no);
if(result==0)
{
printf("%d is not prime",no);
}
else
{
printf("%d is prime",no);
}
getch();
}
int check_prime(int n)
{
int c,r;
for(c=2;c<=n-1;c++)
 {
 if(n%c==0) r=0; else r=1;
 }
 return r;
}

 8. Write a program that will print the longest word written in a line using UDF.

 void longWord (char temp[], int x )
{
 int i,k; int counter = 0;
 int max_word = -1,posi=0;
 temp[x+1]=' ';
 for(i=0; i<=x+1; i++)
 {
 if(temp[i] !=' ')
 {
 counter++;
 }
 else if(temp[i]==' ')
 {
 if(counter > max_word)
{

max_word = counter;
posi=i;


}
counter = 0;
}

}
for(k=posi-max_word;k < posi; k++)
{
printf("%c",temp[k]);
 }
}


void main()
{
 char str[50];
 int len;
 clrscr();
 printf("\n\nEnter String: ");
 gets(str);
 len=strlen(str);
 longWord(str,len);
 getch();
 }

 10. Write a program that uses a UDF to sort an array of integer.
 void sort(int a[],int n); void main()
{
 int a[15],i,n; clrscr(); printf("\nEnter the no u want to enter: "); scanf("%d",&n); for(i=0;i < n; i++) { printf("\nEnter no:- "); scanf("%d",&a[i]); } sort(a,n); getch(); } void sort(int a[],int n) { int i,j,temp; for(i=0;i < n; i++) { for(j=i+1;j < n; j++) { if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}

}
}

for(i=0;i < n; i++)
{
printf("\na[%d]=%d",i,a[i]);
}

}


11. Write a program to search a number within an array using UDF.



more details come soon......

You have no more items.
But wait! We have recommended items waiting for you to read.
Sweet! Show me my recommendations