Friday, October 14, 2011

Write a function to obtain the prime factors of given number? For example, prime factors of 24 are 2,2,2 and 3, whereas prime factors of 35 are 5 and 7

#include
pri_fact(int n)
{
int i;
for(i=2;i<=n;i++)
{
if(n%i==0)
{
printf("%d ",i);
pri_fact(n/i);
break;
}
}
}
main()
{
int n;
printf("\nEnter an integer :\t");
scanf("%d",&n);
printf("\n");
pri_fact(n);
printf("\n\n");
}

Write a function which receives a float and an int from main (), finds the product of these two numbers and returns the product which is printed through main()?

#include
float product(int a,float b)
{
float c;
c=a*b;
return c;
}
main()
{
int x;
float y;
printf("\nEnter an integer :\t");
scanf("%d" ,&x);
printf("Enter a float :\t\t");
scanf("%f",&y);
printf("product :\t\t%.2f\n\n",product(x,y));
}

Write a function that calculates Area and Perimeter/ Circumference of the Circle, whose radius is entered through the keyboard?

#include
#define pi 3.14
float perimeter(float k)
{
float p=2*pi*k;
return p;
}
float area(float k)
{
float a =pi*k*k;
return a;
}
main()
{
float r;
printf("\nEnter radius of cicle:\t\t");
scanf("%f",&r);
printf("Perimeter/circumference :\t%.2f\n",perimeter(r));
printf("Area of the circle is :\t\t%.2f\n\n",area(r));
}

Write a C program to find character frequency in a given string? Example: given Input is: I AM AN INDIAN


#include
main()
{
char s[100], ch;
int count= 0, n[26]={0},temp,i;
printf("Enter a string\t:");
gets(s);
printf("Character\t\tFreequency");
printf("\n---------\t\t----------\n");
for (i=0;i<26;i++) { temp=s[i]; if (temp>=65 && temp<=90)
{
temp+=32;
s[i]=temp;
}
else
s[i]=temp;
}

while(s[count]!='\0')
{
n[s[count]-'a']++;
count++;
}
for (count=0;count<26;count++ )
{
if( n[count] != 0 )
printf(" %c\t\t\t %d\n",count+'a',n[count]);
}
}

Write a program to do the following: a. Output of the question is: Who was the winner of 2007 cricket world cup? b. Accept answer c. If answer is correct print good and stop otherwise give output message as answer is wrong, so try again. d. Display the correct answer when the answer is wrong even at the third attempt.

#include
#include
main()
{
int i=1;
do
{
char a[100];
printf("Who is the winner of 2007 cricket world cup\t:");
scanf("%s",a);
if (strcmp(a,"australia")==0||strcmp(a,"AUSTRALIA")==0||strcmp(a,"Australia")==0)
{
printf("Right Answer\n");
break;
}
else if(i<=2 && strcmp(a,"australia")!=0 && strcmp(a,"AUSTRALIA")!=0&&strcmp(a,"Australia")!=0)
printf("Sorry,Wrong Anser try again\n");
else if(i==3 && strcmp(a,"australia")!=0 && strcmp(a,"AUSTRALIA")!=0&&strcmp(a,"Australia")!=0)
{printf("\nThis time also you entered wrong answer\n\n");
printf("\n\t\t\tCorrect Answer is : Australia \n\n");
}
}
while(i++ && i<4);
}

Write a C program to find how many times given substring is exist ?

#include
main()
{
int i=0,j=0,k=0,count=0,l=0,m=0;
char a[100],b[100];
printf("\nEnter the string \t:");
gets(a);
printf("\nEnter sub string \t:");
gets(b);
l=strlen(b);
while (a[i]!=EOF)
{
if (a[i]==b[j])
{
i++;
j++;
m=1;

if (j==l)
{
j=0;
k=1;
count=count+1;
}
}
else
{
if (m==1)
{
j=0;
m=0;
}
else
i++;
}
}

if (k==1)
{
printf("\n\nThe Entered sub string is present in the given string\n\n");
printf("\nIt is present %d times in the given string\n\n",count);
}
else
{
if (k==0)
printf("\n\nThe Entered sub string is not present in the given string\n\n");
}
}

Write the following C Programs without string functions? a. Length of a string b. Reverse of a string c. String Concatenation d. String comparison e. String copy

#include
#include
main()
{
printf("\t\t1.Length of a string\n");
printf("\t\t2.Reverse of a string\n");
printf("\t\t3.String Concatenation\n");
printf("\t\t4.String comparison\n");
printf("\t\t5.String copy\n\n");
int o,i;
char string[500],a[100],b[100],c;
printf("Select a option from the above list:-\t");
scanf("%d",&o);
switch(o)
{
case 1:
{
printf("\n\t=============================>a.Length of a string<==============================================\n"); printf("Enter a string\t:"); scanf("%s",&string); i=0; while (string[i]!='\0') i++; printf("string length\t:%d\n",i); break; } case 2: { printf("\n\t=============================>b.Reverse of a string<==============================================\n"); printf("Enter a string\t\t\t:\t"); scanf("%s",&a); reverse(a); printf("Reverse of entered string is :\t%s\t\n",a); break; } case 3: { printf("\n\t=============================>c.String Concatenation<==============================================\n"); printf("Enter first string to concatenate :\t"); scanf("%s",&a); printf("Enter second string to concatenate :\t"); scanf("%s",&b); concat(a,b); printf("After concatenation String is :\t%s\n", a); break; } case 4: { printf("\n\t=============================>d.String comparison<==============================================\n"); printf("Enter the first string :\t"); scanf("%s",&a); printf("Enter the second string :\t"); scanf("%s",&b); c= compare(a,b); if (c == 0) printf("Both Entered strings are same.\n"); else printf("Entered strings are not equal.\n"); break; } case 5: { printf("\n\t=============================>e.String copy<==============================================\n");
printf("Enter a string :\t");
scanf("%s",&a);
copy(b, a);
printf("copied string :\t%s\n", b);
break;
}
default:
{
printf("\nChoose the right option which is with in the range 1-5 only \n\n");
}
}
}
reverse(char*);
reverse(char *a)
{
int l, i;
char *begin, *end, temp;
l = strlen(a);
begin = a;
end = a;
for (i=0;i<(l-1);i++)
end++;
for (i=0;i {
temp = *end;
*end = *begin;
*begin = temp;
begin++;
end--;
}
}
concat(char*, char*);
concat(char *a, char *b)
{
while(*a)
a++;
while(*b)
{
*a = *b;
b++;
a++;
}
*a = '\0';
}
int compare(char *a, char *b)
{
while(*a)
{
if(*a == *b)
{
a++;
b++;
}
else
return -1;
}
}
copy(char*, char*);
copy(char *b, char *a)
{
while(*a)
{
*b = *a;
a++;
b++;
}
*b = '\0';
}