Saturday 12 March, 2011

Write a function to calculate the roots of the quadratic equation. The function must use two pointer parameters, one to receive the coefficients a, b, and c, and the other to send the roots to the calling function

#include<stdio.h>
#include<conio.h>
void solve(int*,float*);
int main()
{
int a[3];
float *root;
clrscr();
printf("Enter the value of A : ");
scanf("%d",&a[0]);
printf("Enter the value of B : ");
scanf("%d",&a[1]);
printf("Enter the value of C : ");
scanf("%d",&a[2]);
solve(a,root);
if(root==NULL)
printf("The root is not possible\n");
else if(root[0]==root[1])
printf("The root is %.2f\n",root[0]);
else
printf("The roots are %.2f & %.2f\n",root[0],root[1]);
getch();
return 0;
}
void solve(int *a,float *r)
{
float d;
d=a[1]*a[1]-4*a[0]*a[2];
if(d<0)
{
r=NULL;
}
else if(d==0)
{
r[0]=r[1]=-a[1]/(2*a[0]);
}
else
{
r[0]=-a[1]/(2*a[0]);
r[1]=-a[1]/(2*a[0]);
}
}
Output:
Enter the value of A : 1
Enter the value of B : -4
Enter the value of C : 4
The root is 2.00

Write a program using pointers to read an array of integers and print its elements in reverse order

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *ptr,i,n;
clrscr();
printf("Enter the no of elements:");
scanf("%d",&n);
ptr=(int *)malloc(sizeof(int)*n);
if(ptr==NULL)
{
printf("Not enough memory");
exit(1);
}
for(i=0; i<n; i++)
{
printf("Enter %d element : ",i+1);
scanf("%d",&ptr[i]);
}
printf("Array in original order\n");
for(i=0; i<n; i++)
{
printf("%d\n",ptr[i]);
}
printf("Array in reverse order\n");
for(i=n-1; i>=0; i--)
{
printf("%d\n",ptr[i]);
}
getch();
return 0;
}
Output:
Enter the no of elements:5
Enter 1 element : 12
Enter 2 element : 56
Enter 3 element : 89
Enter 4 element : 45
Enter 5 element : 23
Array in original order
12
56
89
45
23
Array in reverse order
23
45
89
56
12

In a program declare following structure member: name, code, age, weight and height. Read all members of the structure for 100 persons and find list of persons with all related data whose weight > 50 and height > 40 and print the same with suitable format and title

#include<stdio.h>
#include<conio.h>
#define size 3
struct
{
char nm[20],cd;
int height,age,weight;
}s[size];
int main()
{
int i;
clrscr();
for(i=0;i<size;i++)
{
printf("For person %d\n",i+1);
printf("Enter the name : ");
fflush(stdin);
gets(s[i].nm);
printf("Enter the code : ");
flushall(.);
s[i].cd=getc(stdin);
printf("Enter the age : ");
fflush(stdin);
scanf("%d",&s[i].age);
printf("Enter the weight : ");
fflush(stdin);
scanf("%d",&s[i].weight);
printf("Enter the height : ");
fflush(stdin);
scanf("%d",&s[i].height);
}
printf("\n\nData having weight>50 & height>40\n");
printf("\nName Code Age Height Weight\n");
printf("--------------------------------\n");
for(i=0;i<size;i++)
{
if(s[i].weight>50 && s[i].height>40)
printf("%-10s%-5c%3d%7d%7d\n",
s[i].nm,s[i].cd,s[i].age,s[i].height,s[i].weight);
}
getch();
return 0;
}
Output:
For person 1
Enter the name : radhe
Enter the code : D
Enter the age : 21
Enter the weight : 65
Enter the height : 144
For person 2
Enter the name : Mehul
Enter the code : R
Enter the age : 22
Enter the weight : 42
Enter the height : 45
For person 3
Enter the name : Umesh
Enter the code : S
Enter the age : 21
Enter the weight : 55
Enter the height : 35
Data having weight>50 & height>40
Name Code Age Height Weight
--------------------------------
radhe D 21 144 65

Define a structure called cricket that will describe the following information: Player name Team name Batting average Using cricket, declare an array player with 50 elements and wire a program to read the information about all the 50 players and print a list.

#include<stdio.h>
#include<conio.h>
#include<string.h>
struct cricket
{
char nm[20],team[20];
int avg;
};
#define total 5
int main()
{
struct cricket player[total],temp;
int i,j;
clrscr();
for(i=0;i<total;i++)
{
printf("For player %d\n",i+1);
printf("Enter the name of player : ");
fflush(stdin);
gets(player[i].nm);
printf("Enter the team : ");
fflush(stdin);
gets(player[i].team);
printf("Enter the batting average : ");
fflush(stdin);
scanf("%d",&player[i].avg);
}
printf("\nTeam Name Average\n");
printf(" \n");
for(i=0;i<total;i++)
{
printf("%-10s %-10s %7d\n",player[i].team,player[i].nm,player[i].avg);
}
getch();
return 0;
}
Output:
For player 1
Enter the name of player : radhe
Enter the team : India
Enter the batting average : 100
For player 2
Enter the name of player : Tiwari
Enter the team : Pakistan
Enter the batting average : 5
For player 3
Enter the name of player : Tendulkar
Enter the team : India
Enter the batting average : 45
For player 4
Enter the name of player : Dhoni
Enter the team : India
Enter the batting average : 48
For player 5
Enter the name of player : Yuvi
Enter the team : India
Enter the batting average : 39
Team Name Average
-----------------------------
India radhe 100
Pakistan Tiwari 5
India Tendulkar 45
India Dhoni 48
India Yuvi 39

Write a program that search an item from array of string

#include<stdio.h>
#include<conio.h>
#include<string.h>
int string(char[],char[]);
int main()
{
char str[100],find[20];
int i;
clrscr();
printf("Enter the string :");
gets(str);
printf("Enter the srting that you want to find : ");
gets(find);
i=string(str,find);
if(i==1)
printf("%s is found in %s",find,str);
else
printf("%s is not found in %s",find,str);
getch();
return 0;
}
int string(char str[20], char find[20])
{
int i,j,flag;
for(i=0;str[i];i++)
{
if(str[i]==find[0])
{
flag=1;
for(j=1;find[j];j++)
{
if(str[i+j]!=find[j])
{
flag=0;
break;
}
}
if(flag==1)
break;
}
}
return flag;
}
Output:
Enter the string :radhe
Enter the string that you want to find : r
r is found in string
Enter the string :kano
Enter the string that you want to find : z
z is not found in kano

Write a function to upper the string

#include<stdio.h>
#include<conio.h>
void reverse(char[]);
int main()
{
char str[20];
clrscr();
printf("Enter string : ");
gets(str);
reverse(str);
getch();
return 0;
}
void reverse(char str[20])
{
int i;
printf("%s in reverse order is ",str);
for(i=strlen(str)-1;i>=0;i--)
printf("%c",str[i]);
}
Output:
Enter string : radhe
radhe in reverse order is ehdar

Write a function that will scan a character string passed as an argument and convert all lower-case character into their upper-case equivalent

#include<stdio.h>
#include<conio.h>
void upper(char[]);
int main()
{
char str[20];
clrscr();
printf("Enter string : ");
gets(str);
upper(str);
getch();
return 0;
}
void upper(char str[20])
{
int i;
printf("%s in upper case is ",str);
for(i=0;str[i];i++)
{
if(str[i]>96 && str[i]<123)
str[i]-=32;
}
printf("%s",str);
}
Output:
Enter string :string
string in upper case is STRING

Write a function which returns 1 if the given number is palindrome otherwise returns 0

#include<stdio.h>
#include<conio.h>
int pelindrome(int);
int main()
{
int n,p;
clrscr();
printf("Enter a number : ");
scanf("%d",&n);
p=pelindrome(n);
if(p==1)
printf("%d is pelindrome",n);
else
printf("%d is not pelindrome",n);
getch();
return 0;
}
int pelindrome(int n)
{
char a[6],b[6];
itoa(n,a,10);
strcpy(b,a);
strrev(b);
if(strcmp(a,b)==0)
return 1;
else
return 0;
}
Output:
Enter a number : 123
123 is not pelindrome
Enter a number : 121
121 is palindrome

Write a function prime that returns 1 if its argument is a prime no. and returns 0 otherwise

#include<stdio.h>
#include<conio.h>
int prime(int);
int main()
{
int n,p;
clrscr();
printf("Enter a number : ");
scanf("%d",&n);
p=prime(n);
if(p==1)
printf("%d is prime\n",n);
else
printf("%d is not prime\n",n);
getch();
return 0;
}
int prime(int n)
{
int i;
for(i=2;i<n;i++)
{
if(n%i==0)
return 0;
}
return 1;
}
Output:
Enter a number : 18
18 is not prime
Enter a number : 5
5 is prime

Use recursive calls to evaluate f(x) = x – x3/3! + x5/5! – x7/7! + ……

#include<stdio.h>
#include<conio.h>
int main()
{
float series(float,int),x;
int n;
clrscr();
printf("\nEnter X:");
scanf("%f",&x);
printf("\nEnter n:");
scanf("%d",&n);
printf("\nAns %f",series(x,n));
getch();
return 0;
}
float series(float x,int n)
{
long factorial(int);
float power(float,int);
float sum=0;
int i,s=1;
for(i=1;i<=n;i+=2)
{
sum+=(power(x,i)/factorial(i))*s;
s*=-1;
}
return sum;
}
float power(float x, int y)
{
float p=1;
int i;
for(i=1;i<=y;i++)p*=x;
return p;
}
long factorial(int p)
{
long f=1;
int i;
for(i=1;i<=p;i++)f*=i;
return f;
}
Output
Enter X:1.2
Enter n:5
Ans 0.932736

Write a program that finds a given word in a string

#include<stdio.h>
#include<conio.h>
main()
{
char str[100],find[100];
int i,j,flag;
clrscr();
printf("Enter the string :");
gets(str);
printf("Enter the srting that you want to find : ");
gets(find);
for(i=0;str[i];i++)
{
if(str[i]==find[0])
{
flag=1;
for(j=1;find[j];j++)
{
if(str[i+j]!=find[j])
{
flag=0;
break;
}
}
if(flag==1)
break;
}
}
if(flag==1)
printf("%s is found in %s at %d position",find,str,i+1);
else
printf("%s is not found in %s",find,str);
getch();
}
Output:
Enter the string :The Cat & The Dog
Enter the srting that you want to find : Dog
Dog is found in The Cat & The Dog at 15 position

Write a program that will read a string and rewrite it in the alphabetical order. i.e. the word STRING should be written as GINRST

#include<stdio.h>
#include<conio.h>
main()
{
char str[100],temp;
int i,j;
clrscr();
printf("Enter the string :");
gets(str);
printf("%s in ascending order is -> ",str);
for(i=0;str[i];i++)
{
for(j=i+1;str[j];j++)
{
if(str[j]<str[i])
{
temp=str[j];
str[j]=str[i];
str[i]=temp;
}
}
}
printf("%s\n",str);
getch();
}
Output:
Enter the string :string
string in ascending order is -> ginrst

Write a program that will read a text and count all occurrences of a particular word

#include<stdio.h>
#include<conio.h>
main()
{
char str[100],find[100];
int i,j,cnt=0,flag;
clrscr();
printf("Enter the string :");
scanf("%s",str);
printf("Enter the srting that you want to find : ");
scanf("%s",find);
for(i=0;str[i];i++)
{
flag=0;
if(str[i]==find[0])
{
flag=1;
for(j=1;find[j];j++)
{
if(str[i+j]!=find[j])
{
flag=0;
break;
}
}
if(flag==1)
cnt++;
}
else
{
}
}
printf("%s occurs in %s %d times\n",find,str,cnt);
getch();
}
Enter the string :The cat & The dog
Enter the srting that you want to find : The
The occurs in The cat & The dog 2 times

Give the 1-D array A and B, which are sorted in ascending order. Write a program to merge them into a single sorted array C that contains every item from arrays A and B, in ascending order

#include<stdio.h>
#include<conio.h>
int main()
{
int a[10],b[10],c[20],n1,n2,i,j,temp,k=0;
clrscr();
printf(" Enter the no. of element for 1st array : ");
scanf("%d",&n1);
for(i=0;i<n1;i++,k++)
{
printf(" Enter element [%d] : ",i+1);
scanf("%d",&a[i]);
c[k]=a[i];
}
for(i=0;i<n1;i++)
{
for(j=i+1;j<n1;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\n After sorting 1st array : ");
for(i=0;i<n1;i++)
{
printf("\n Element [%d] = %d",i+1,a[i]);
}
printf("\n\n Enter the no. of element for 2nd array : ");
scanf("%d",&n2);
for(i=0;i<n2;i++,k++)
{
printf(" Enter element [%d] : ",i+1);
scanf("%d",&b[i]);
c[k]=b[i];
}
for(i=0;i<n2;i++)
{
for(j=i+1;j<n2;j++)
{
if(b[i]>b[j])
{
temp=b[i];
b[i]=b[j];
b[j]=temp;
}
}
}
printf("\n After sorting 2nd array : ");
for(i=0;i<n2;i++)
{
printf("\n Element [%d] = %d",i+1,b[i]);
}
for(i=0;i<n1+n2;i++)
{
for(j=i+1;j<n1+n2;j++)
{
if(c[i]>c[j])
{
temp=c[i];
c[i]=c[j];
c[j]=temp;
}
}
}
printf("\n\n\n After combined and sorted both array :- ");
for(i=0;i<n1+n2;i++)
{
printf("\n Element [%d] = %d",i+1,c[i]);
}
getch();
}
Output:
Enter the no. of element for 1st array : 5
Enter element [1] : 20
Enter element [2] : 18
Enter element [3] : 6
Enter element [4] : 12
Enter element [5] : 4
After sorting 1st array :
Element [1] = 4
Element [2] = 6
Element [3] = 12
Element [4] = 18
Element [5] = 20
Enter the no. of element for 2nd array : 3
Enter element [1] : 6
Enter element [2] : 2
Enter element [3] : 3
After sorting 2nd array :
Element [1] = 2
Element [2] = 3
Element [3] = 6
After combined and sorted both array :-
Element [1] = 2
Element [2] = 3
Element [3] = 4
Element [4] = 6
Element [5] = 6
Element [6] = 12
Element [7] = 18
Element [8] = 20