Friday, October 14, 2011

Write a C program to print following sentences. a. I am an Indian b. “I am an Indian” c. *I am an Indian*

#include //This is include information about standard library
main() //User define function
{
printf("I am an Indian\n"); //here printing the given sentence
printf("*I am an Indian*\n"); //here printing the given sentence
printf("\"I am an Indian\"\n"); //here printing the given sentence
}

Define the structure called Batsman having properties like Batsman_ name,Batsman_country and Bats_avg .Using Batsman declare an array PLAYER with 11 elements and write a program to read the information of all the 11 Players and print Country wise list containing names of batsman with their batting averages.

#include
main()
{
struct Batsman
{
char Batsman_name[30];
char Batsman_country[10];
float Batsman_avg;
};

struct Batsman PLAYER[3];
char temp[1];
int i,j,k,l,m;
for (i=0;i<11;i++)
{
printf("Enter Batsman name: ");
gets(PLAYER[i].Batsman_name);
printf("Enter Batsman Country: ");
gets(PLAYER[i].Batsman_country);
printf("Enter Batsman Average: ");
scanf("%f",&PLAYER[i].Batsman_avg);
gets(temp);
}
for(j=0;j<11;j++)
{
m=0;
for(k=0;k if(strcmp(PLAYER[j].Batsman_country,PLAYER[k].Batsman_country)==0)
{
m++;
break;
}
if (m==0)
{
printf("\fCountry '%s' batsman are :-\n\n",PLAYER[j].Batsman_country);
for(l=0;l<11;l++)
if(strcmp(PLAYER[j].Batsman_country,PLAYER[l].Batsman_country)==0)
{
printf("%s\t",PLAYER[l].Batsman_name);
printf("%s\t",PLAYER[l].Batsman_country);
printf("%.4f",PLAYER[l].Batsman_avg);
printf("\n");
}
}
}

}
#include
main()
{
int i,n;
struct {
char emp_id[10];
char emp_name[10];
char emp_gen[10];
int emp_sal;
char email_add[30];
}
Employee[50];
for(i=0;i<10;i++)
{
printf("\n\nEnter Employee id:\t");
scanf("%s",Employee[i].emp_id);
printf("Enter Employee name:\t");
scanf("%s",Employee[i].emp_name);
printf("Enter Employee gender:\t");
scanf("%s",Employee[i].emp_gen);
printf("Enter Employee sal:\t");
scanf("%d",&Employee[i].emp_sal);
printf("Enter Employee email:\t");
scanf("%s",Employee[i].email_add);
}
for(i=0;i<10;i++)
{
printf("\n\nEmployee id : \t%s\n",Employee[i].emp_id);
printf("Employee name : \t%s\n",Employee[i].emp_name);
printf("Employee gender : \t%s\n",Employee[i].emp_gen);
printf("Employee salary : \t%d\n",Employee[i].emp_sal);
printf("Employee email : \t%s\n\n\n",Employee[i].email_add);
}
}

Define the structure called Employee having properties like emp_id, emp_name, gender, and emp_sal email_add. Write a program which takes the details of 10 employees.

#include
main()
{
int i,n;
struct {
char emp_id[10];
char emp_name[10];
char emp_gen[10];
int emp_sal;
char email_add[30];
}
Employee[50];
for(i=0;i<10;i++)
{
printf("\n\nEnter Employee id:\t");
scanf("%s",Employee[i].emp_id);
printf("Enter Employee name:\t");
scanf("%s",Employee[i].emp_name);
printf("Enter Employee gender:\t");
scanf("%s",Employee[i].emp_gen);
printf("Enter Employee sal:\t");
scanf("%d",&Employee[i].emp_sal);
printf("Enter Employee email:\t");
scanf("%s",Employee[i].email_add);
}
for(i=0;i<10;i++)
{
printf("\n\nEmployee id : \t%s\n",Employee[i].emp_id);
printf("Employee name : \t%s\n",Employee[i].emp_name);
printf("Employee gender : \t%s\n",Employee[i].emp_gen);
printf("Employee salary : \t%d\n",Employee[i].emp_sal);
printf("Employee email : \t%s\n\n\n",Employee[i].email_add);
}
}
#include
main()
{
int i,n;
struct {
char emp_id[10];
char emp_name[10];
char emp_gen[10];
int emp_sal;
char email_add[30];
}
Employee[50];
for(i=0;i<10;i++)
{
printf("\n\nEnter Employee id:\t");
scanf("%s",Employee[i].emp_id);
printf("Enter Employee name:\t");
scanf("%s",Employee[i].emp_name);
printf("Enter Employee gender:\t");
scanf("%s",Employee[i].emp_gen);
printf("Enter Employee sal:\t");
scanf("%d",&Employee[i].emp_sal);
printf("Enter Employee email:\t");
scanf("%s",Employee[i].email_add);
}
for(i=0;i<10;i++)
{
printf("\n\nEmployee id : \t%s\n",Employee[i].emp_id);
printf("Employee name : \t%s\n",Employee[i].emp_name);
printf("Employee gender : \t%s\n",Employee[i].emp_gen);
printf("Employee salary : \t%d\n",Employee[i].emp_sal);
printf("Employee email : \t%s\n\n\n",Employee[i].email_add);
}
}

Define the structure called CLASS having properties like stud_id, stud_name and stud_branch. Write a program which takes the details of at least 15 students.

main()
{
int i,n;
struct {
char stud_id[30];
char stud_name[30];
char stud_branch[30];
}
class[50];
printf("\nHow many students details would you want in the class(enter 15<) :\t");
scanf("%d",&n);
for(i=0;i {
printf("\n\nEnter student id:\t");
scanf("%s",class[i].stud_id);
printf("Enter student name:\t");
scanf("%s",class[i].stud_name);
printf("Enter student branch:\t");
scanf("%s",class[i].stud_branch);
}
printf("\n\n");
for(i=0;i {
printf("student id :\t\t %s\n",class[i].stud_id);
printf("student name :\t\t %s\n",class[i].stud_name);
printf("student branch :\t %s\n\n\n",class[i].stud_branch);
}
}

Write a function that calculates arithmetic addition, subtraction, multiplication and division of two integers?

#include
int addition(int x, int y)
{
int sum=x+y;
return sum;
}
int substraction(int x, int y)
{
int sub =x-y;
return sub;
}
int multiplication(int x, int y)
{
int mul =x*y;
return mul;
}
float division(float x,float y)
{
float div =(x/y);
return div;
}
main()
{
int a, b;
printf("\nEnter 1st integer:\t");
scanf("%d",&a);
printf("Enter 1st integer:\t");
scanf("%d",&b);
printf("\naddition :\t\t%d\n",addition(a,b));
printf("substraction :\t\t%d\n",substraction(a,b));
printf("multiplication :\t%d\n",multiplication(a,b));
printf("division :\t\t%.2f\n\n",division(a,b));
}

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';
}

Write a C program to find number of pairs of adjacent numbers whose sum is 8 in a given list of items? (Example : i/p: 2 4 5 3 1 7 1 7 4 5 output: 4 )

#include
int main()
{
int l[10],a,b,c=0,s;
printf("Enter the size of array You want :-");
scanf("%d",&a);
printf("Enter the elements in the array :-\n");
for(b=0;b {
scanf("%d",&l[b]);
}
printf("\nThe elements in the array are :-");
for(b=0;b {
printf(" %d",l[b]);
}
printf("\n");
printf("Enter your sum number :-");
scanf("%d",&s);
for(b=0;b {
if (l[b]+l[b+1]==s)
c=c+1;
}
printf("Adjacent numbers whose sum is \'%d\' having pairs are : \'%d\'\n",s,c);
}

Write a C program to find a. Addition of two matrices b. Multiplication of two matrices. c. Trace of a matrix d. Transpose of matrix

#include
main()
{
printf("\t\t1.Addition of two matrices\n");
printf("\t\t2.Multiplication of two matrices.\n");
printf("\t\t3.Trace of a matrix\n");
printf("\t\t4.Transpose of matrix\n");
int a[10][10],b[10][10],c[10][10],i,j,k,sum=0,m,n,o,p,s;
printf("Select a option from the above list: \t");
scanf("%d",&s);
switch(s)
{
case 1:
printf("\n\t\t=============================>a.Addition of two matrices<==============================================\n"); printf("\n\t ***For the addition Enter the same same size of matrix*** \n"); printf("\nEnter rows and columns of the first matrix:\n"); scanf("%d %d",&m,&n); printf("\nEnter rows and columns of the second matrix:\n"); scanf("%d %d",&o,&p); printf("\nEnter the elements here for first matrix:\n"); for(i=0;ib.Multiplication of two matrices<==============================================\n"); printf("\nEnter rows and columns of the first matrix:\n"); scanf("%d %d",&m,&n); printf("\nEnter rows and columns of the second matrix:\n"); scanf("%d %d",&o,&p); if(n!=o) { printf("\nMatrix mutiplication is not possible\n"); printf("***First matrix columns are equal to second matrix rows***"); } printf("\nEnter the elements here for first matrix:\n"); for(i=0;ic.Trace of a matrix<==============================================\n"); printf("\nEnter rows and columns of the matrix:\n"); scanf("%d %d",&m,&n); printf("\nEnter the elements here for first matrix:\n"); for(i=0;id.Transpose of matrix<==============================================\n");
printf("\nEnter rows and columns of the matrix:\n");
scanf("%d %d",&m,&n);
printf("\nEnter the elements here for first matrix:\n");
for(i=0;i for(j=0;j scanf("%d",&a[i][j]);
printf("\nThe real matrix is :\n");
for(i=0;i {
printf("\n");
for(j=0;j {
printf("%d\t",a[i][j]);
}
}
printf("\n\n");
for(i=0;i for(j=0;j b[i][j]=0;
for(i=0;i {
for(j=0;j {
b[i][j]=a[j][i];
}
}
printf("\nTraspose of matrix is :\n\t");
for(i=0;i {
printf("\n");
for(j=0;j {
printf("%d\t",b[i][j]);
}
}
printf("\n\n");
break;

default :
printf("\n Choose the right option which is with in the range 1-4 only \n\n");
break;
}
}

Write a C program to generate and print the first N Fibonacci num

#include
int fib(int);
main()
{
int n;
printf("Enter N value:");
scanf("%d",&n);
fib(n);
}
int fib(int n)
{
int a=0,b=1,c;
printf("%d %d ",a,b);
while(n>0)
{
c=b+a;
printf("%d ",c);
a=b;
b=c;
n-=1;
}
printf("\n");

}

Write a C program to find following parameters for given list of elements? a. Max b. Min c. Mode d. Variance e. Standard deviation

#include

#include

main()
{
printf("\t\t1.Max\n");
printf("\t\t2.Min\n");
printf("\t\t3.Mode\n");
printf("\t\t4.Variance\n");
printf("\t\t5.Standard deviation\n\n");
int c,a[20],min,max,i,j,n,s=0,k,l,tv,mode=0,count=0,mx=0;
float x,y=0,std,z=0;
printf("Select a option from the above list:-\t");
scanf("%d",&c);
switch(c)
{
case 1:
printf("\n\t=============================>a.Max(Maximum value)<==============================================\n"); printf("Enter the No of Elements You want :\t"); scanf("%d",&n); printf("Enter the Elements You want :\n"); for(i=0;imax)
max=a[i];
}
printf("Maximum value = %d \n",max);
break;
case 2:
printf("\n\t=============================>a.Min(Minimum value)<==============================================\n"); printf("Enter the num of Elements You want :\t"); scanf("%d",&n); printf("Enter the Elements You want :\n"); for(i=0;imax)
max=a[i];
}
printf("Minimum value = %d \n",min);
break;
case 3:
("\n\t=============================>c.Mode(Mode value)<==============================================\n"); printf("Enter the num of Elements You want :\t"); scanf("%d",&n); printf("Enter the Elements You want :\n"); for(i=0;ia[j+1])
{
tv=a[j];
a[j]=a[j+1];
a[j+1]=tv;
}
}
}
for(l=0;lmx)
{
mx=count;
mode=a[l];
}
}
printf("Mode = %d \n",mode);
break;
case 4:
("\n\t=============================>d.Variance<==============================================\n"); printf("Enter the num of Elements You want :\t"); scanf("%d",&n); printf("Enter the Elements You want :\n"); for(i=0;ie. Standard deviation<==============================================\n");
printf("Enter the num of Elements You want : \t");
scanf("%d",&n);
printf("Enter the Elements You want : \n");
for(i=0;i {
scanf("%d",&a[i]);
s=s+a[i];
}
k=s/n;
for(i=0;i {
y=y+pow((a[i]-k),2);
}
z=y/n;
std=sqrt(z);
printf("Standard deviation : %f\n\n",std);
break;
default :
printf("\n Choose the right option which is with in the range 1-5 only \n\n");
}
}

Write a C program to find given element in a list or not.(linear search)?

#include
main()
{
int a,b,c,d,e=0;
printf("\nEnter the length of your list = ");
scanf("%d",&a);
int list[a];
for (b=1;b<=a;b++)
{
printf("\nElement%d:",b);
scanf("%d",&list[b]);
}
printf("\nEnter a Element you want search:\t");
scanf("%d",&c);
for (d=1;d<=a;d++)
{
if (list[d]==c)
e=e+1;
}
if (e==0)
printf("*****The given input element not found*****");
else
printf("\nThe given element present %d time(s) in the list.\n",e);
}

Start with a positive integer n. If n is even, divide it by 2, else replace n by 3n+1. Repeat until we obtain the value 1 in the sequence. The 3x+1 conjecture says that we eventually reach 1 irrespective of what initial value of n we start with. Different initial values of n lead to different numbers of iterations of the loop before hitting the value 1. For example, if we start with n=3, we get a sequence 3,10,5,16,8,4,2,1 using 7 iterations. If we start with 7, we need 16 iterations, since the sequence is now 7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1. You are given a bound B. Your task is to locate that particular value of n in the range 1<=n<=B for which the number of iterations is maximum. Once this integer nis located, you print n, the corresponding number of iterations, and finally the sequence associated with n. If the bound is 10, then the output of your program would look like: Maximum number of iterations = 19 for n = 9. The sequence is: 9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 Report the output of your program for the bound 100,000.

#include
main()
{
int a,b,c,d,e,f=0,g=0,h=0;
printf("Enter your Bound :\t");
scanf("%d",&a);
for(b=1;b<=a;b++)
{
c=b;
f=0;
d=b;
while(c!=1)
{
if(c%2==0)
{
c=c/2;
f=f+1;
}
else
{
c=(3*c)+1;
f=f+1;
}
}
if(g {
g=f;
e=d;
}
}
printf("Maximum number of iterations = %d for n=%d\n",g,e);
printf("The sequence is : %d\n",e);
while(e!=1)
{
if(e%2==0)
{
e=e/2;
h=h+1;
printf("%d ",e);
}
else
{
e=(3*e)+1;
h=h+1;
printf("%d ",e);
}
}
printf("\n");
}