Download C Assignment

Document related concepts
no text concepts found
Transcript
[Type text]
C Programming
1) Write a program to print “Hello World” message.
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr(); // clears the output window
printf("\n \"Hello World\"");
// prints the message
getch();
return 0;
}
Output:
“Hello World”
1|Page
[Type text]
C Programming
2) Write a program to print Name, Address and Birth Date.
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr(); // clears the output window
printf("\n Name : Dipen");
// prints the name
printf("\n Address : Nadiad");
// prints the address
printf("\n Birth Date : 02-08-1988"); // prints the birth date
getch();
return 0;
}
Output:
Name: Dipen
Address: Nadiad
Birth Date:02-08-1988
2|Page
[Type text]
C Programming
3) Write a program to Add, Multiply and Divide two integers and float
numbers.
#include<stdio.h>
#include<conio.h>
int main()
{
int x,y; // define 2 integer variable x & y
float a,b; // define 2 real variable x & y
clrscr(); // clears the output window
printf("\n Arithmetic operation on integer:-");
printf("\n\n Enter 1st integer number : ");
scanf("%d",&x); // input first integer number
printf(" Enter 2nd integer number : ");
scanf("%d",&y); // input second integer number
printf("\n\n Addition of two integer numbers is : %d",x+y);
printf("\n Multiplication of two integer numbers is : %d",x*y);
printf("\n Division of two integers numbers is : %d",x/y);
printf("\n\n Arithmetic operation on float:-");
printf("\n\n Enter 1st float number : ");
scanf("%f",&a); // input first real number
printf(" Enter 2nd float number : ");
scanf("%f",&b); // input second real number
printf("\n\n Addition of two float number is :%.2f",a+b);
printf("\n Multiplication of two float number is :%.2f",a*b);
printf("\n Division of two float number is:%.2f",a/b);
getch();
return 0;
}
3|Page
[Type text]
C Programming
Output:
Arithmetic operation on integer:Enter 1st integer number : 20
Enter 2nd integer number : 5
Addition of two integer numbers is : 25
Multiplication of two integer numbers is : 100
Division of two Integers numbers is : 4
Arithmetic operation on float:Enter 1st float number : 10.50
Enter 2nd float number : 5.2
Addition of two float number is : 15.70
Multiplication of two float number is : 54.60
Division of two float number is: 2.02
4|Page
[Type text]
C Programming
4) Write a program to convert Rupees (float) to paisa (int).
#include<stdio.h>
#include<conio.h>
int main()
{
float rs; //real variable for rupees
int ps; // integer variable for paisa
clrscr();
printf("\n\n Enter rupees to convert into paisa : ");
scanf("%f",&rs);
ps=rs*100;
printf("\n Paisa of given rupees is %d",ps);
getch();
return 0;
}
Output:
Enter rupees to convert into paisa : 98.52
Paisa of given rupees is 9852
5|Page
[Type text]
C Programming
5) Write program to accept number of days and print year, month and
remaining days.
#include<stdio.h>
#include<conio.h>
int main()
{
int day,y,m; /*day for day input & output,
y for calculate year,
m for calculate month
printf("Enter the number of days : ");
scanf("%d",&day);
y=day/365; //calculate years
day%=365; // calculate remaining days
m=day/30; // calculate months
day%=30; // calculate remaining days
printf("%d years,%d months, %d days",y,m,day);
getch();
return 0;
}
Output:
Enter number of days : 1025
2 years,9 months, 25 days
6|Page
[Type text]
C Programming
6) Write program to check whether the entered number is PRIME or not.
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,flag=0;
clrscr();
// declare variable i, n & flag and initialize flag with 0
printf("\n Enter the number : ");
scanf("%d",&n);
for(i=2;i<n;i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
if(flag==0)
{
printf("\n %d is a Prime number",n);
}
else
{
printf("\n %d ia not a Prime number",n);
}
getch();
return 0;
}
Output:
Enter the number : 4
4 is not a Prime number
Enter Number : 7
7 is a Prime number
7|Page
[Type text]
C Programming
7) Write a program to check whether the entered number is EVEN or ODD.
#include<stdio.h>
#include<conio.h>
int main()
{
int n; // declare integer type variable
clrscr();
printf("\n Enter the number : ");
scanf("%d",&n);
if(n%2==0) // check the condition whether the modulo is zero or not
{
printf("\n Entered number %d is EVEN",n);
}
else
// otherwise this part will be executes
{
printf("\n Entered number %d is ODD",n);
}
getch();
return 0;
}
Output:
Enter the number : 1
Entered number 1 is ODD
Enter the number : 2
Entered number 2 is EVEN
8|Page
[Type text]
C Programming
8) Using While loop print 1 2 3 4 5 …… 10.
#include<stdio.h>
#include<conio.h>
int main()
{
int n=1;
clrscr();
while(n<=10) // loop will be executed till n is less or equl to 10
{
printf(" %d ",n);
n++;
}
getch();
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10
9|Page
[Type text]
C Programming
9) Print series 2, 4, 6, 8,…....,n.
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n; // declare two integer type variables
clrscr(); // clears the output window
printf("\n Enter the number : ");
scanf("%d",&n); // input the value of n
printf(“\n”); // will break the line on output window
for(i=2;i<=n;i++)
{
if(i%2==0)
{
printf(" %d ",i);
}
}
getch();
return 0;
}
Output:
Enter the number : 14
2 4 6 8 10 12 14
10 | P a g e
[Type text]
C Programming
10) Print series 2, 4, 16,……,n*n using shorthand operator and while loop.
#include<stdio.h>
#include<conio.h>
int main()
{
int n,x; // declare the variable
long double i=2; //declare the variable of long double type
clrscr();
printf("Enter the number : ");
scanf("%d",&n);
printf("\n");
x=1;
while(x<=n) // loop will be execute till the value of x I less or equal n
{
printf("%.2Lf\n",i); // print the value of I upto 2 decimals only
x++;
i*=i;
}
getch();
return 0;
}
Output:
Enter the number : 4
2.00
4.00
16.00
256.00
11 | P a g e
[Type text]
C Programming
11) Write a program to generate Fibonacci series.
#include<stdio.h>
#include<conio.h>
int main()
{
int n1=0,n2=1,n3=1,n,i;
clrscr();
printf("Enter the Number : ");
scanf("%d",&n);
printf("\n");
for(i=1;i<=n;i++)
{
printf(" %d ",n3);
n3=n1+n2;
n1=n2;
n2=n3;
}
getch();
return 0;
}
Output:
Enter the Number : 5
1 1 2 3 5
12 | P a g e
[Type text]
C Programming
12) Write a program to print the multiplication table.
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,n;
clrscr();
printf("Enter the number : ");
scanf("%d",&n);
for(i=1;i<=10;i++)
{
printf("\n %d * %d = %d",n,i,i*n);
}
getch();
return 0;
}
Output:
Enter the number : 9
9*1=9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90
13 | P a g e
[Type text]
C Programming
13) Write a program to find factorial of the entered number.
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,f=1;
clrscr();
printf("\n Enter the Number : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
f=f*i;
}
printf("\n The factorial of %d is : %d",n,f);
getch();
return 0;
}
Output:
Enter the Number : 6
The factorial of 5 is : 720
14 | P a g e
[Type text]
C Programming
14) Write a program to print all the numbers and sum of all the integers
that are greater then 100 and less than 200 and are divisible by 7.
#include<stdio.h>
#include<conio.h>
int main()
{
int i,sum=0;
clrscr();
for(i=100;i<200;i++)
{
if(i%7==0)
{
printf(" %d ",i);
sum=sum+i;
}
}
printf("\n\n Sum of all above integers that are divisible by 7 is %d",sum);
getch();
return 0;
}
Output:
105 112 119 126 133 140 147 154 161 168 175 182 189 196
Sum of all above integers that are divisible by 7 is 2107
15 | P a g e
[Type text]
C Programming
15) Write a program to find the roots of an equation ax2 + bx + c = 0.
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
float a,b,c,alf,bt,dlt;
clrscr();
printf("\n Enter a: ");
scanf("%f",&a);
printf("\n Enter b: ");
scanf("%f",&b);
printf("\n Enter c: ");
scanf("%f",&c);
dlt=b*b-4*a*c;
if(dlt==0)
{
printf("\n ALPHA=BETA=%f",-b/(2*a));
}
else if(dlt<0)
{
printf("\n Imaginary Roots");
}
else
{
alf=(-b+sqrt(dlt))/(2*a);
bt=(-b-sqrt(dlt))/(2*a);
printf("\n\n Alpha = %f\n Beta=%f\n",alf,bt);
}
getch();
return 0;
}
16 | P a g e
[Type text]
C Programming
Output:
(1)
Enter a: 12
Enter b: 2
Enter c: 34
Imaginary Roots
(2)
Enter a: 2
Enter b: 6
Enter c: 2
Alpha = -0.381966
Beta = -2.618034
(3)
Enter a: 2
Enter b: 4
Enter c: 2
ALPHA=BETA= -1.000000
17 | P a g e
[Type text]
C Programming
16) Write a program that accept basic, HRA, and convergence from the
user and calculate total salary.
#include<stdio.h>
#include<conio.h>
int main()
{
float basic,HRA,cnvg,totsal;
clrscr();
printf("\n Enter basic salary : ");
scanf("%f",&basic);
printf("\n Enter HRA : ");
scanf("%f",&HRA);
printf("\n Enter convergence : ");
scanf("%f",&cnvg);
HRA=(basic*HRA)/100;
cnvg=(basic*cnvg)/100;
totsal=basic+HRA+cnvg;
printf("\n\n Total salary is %.2f",totsal);
getch();
return 0;
}
Output:
Enter basic salary : 3000
Enter HRA : 5
Enter convergence : 2
Total salary is 3210.00
18 | P a g e
[Type text]
C Programming
17) Print the following triangle.
a b c d e
a b c d
a b c
a b
a
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,j,k;
clrscr();
printf("Enter the number : ");
scanf("%d",&n);
printf("\n");
for(i=0;i<n;i++)
{
for(k=1;k<=i;k++)
{
printf(" ");
}
for(j=1;j<=n-i;j++)
{
printf(" %c",96+j);
}
printf("\n");
}
getch();
return 0;
}
Output:
Enter the number : 5
a b c d e
a b c d
a b c
a b
a
19 | P a g e
[Type text]
C Programming
18) Write a program that prints the following Floyd’s triangle.
1
2 3
4 5 6
7 8 9 10
11 ………..15
.
.
79 …………………91
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,n,k;
clrscr();
printf("\n\n Enter the number : ");
scanf("%d",&n);
for(i=1,k=1;i<=n;i++)
{
for(j=1;j<=i;j++,k++)
{
printf(" %d ",k);
}
printf("\n");
}
getch();
return 0;
}
Output:
Enter the number : 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
20 | P a g e
[Type text]
C Programming
19) Write a program to find maximum element from 1-Dimensional array.
#include<stdio.h>
#include<conio.h>
int main()
{
int a[20],n,i,max=0; // declared the array a with size 20
clrscr();
printf("\n Enter the number of elements for 1-D array : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter element [%d] : ",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
if(max<a[i])
max=a[i];
}
printf("\n Maximum element from above array inserted is : %d",max);
getch();
return 0;
}
Output:
Enter the number of elements for 1-D array : 5
Enter element [1] : 1
Enter element [2] : 8
Enter element [3] : 2
Enter element [4] : 5
Enter element [5] : 9
Maximum element from above array inserted is: 9
21 | P a g e
[Type text]
C Programming
20) Write a program to sort given array in ascending order.
#include<stdio.h>
#include<conio.h>
int main()
{
int a[20],i,j,n,temp;
clrscr();
printf("\n Enter no. of elements for 1-D array : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf(" Enter element[%d] : ",i+1);
scanf("%d",&a[i]);
}
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;
}
}
}
printf("\n\n Ascending order of inserted array is : ");
for(i=0;i<n;i++)
{
printf("\n %d ",a[i]);
}
getch();
return 0;
}
22 | P a g e
[Type text]
C Programming
Output:
Enter no. of elements for 1-D array : 5
Enter element [1] : 2
Enter element [2] : 10
Enter element [3] : 4
Enter element [4] : 13
Enter element [5] : 7
Ascending order of inserted array is:
2
4
7
10
13
23 | P a g e
[Type text]
C Programming
21) 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);
24 | P a g e
[Type text]
C Programming
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();
return 0;
}
25 | P a g e
[Type text]
C Programming
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
26 | P a g e
[Type text]
C Programming
22. Write a program to add two matrices.
#include<stdio.h>
#include<conio.h>
int main()
{
int a[5][5],b[5][5],c[5][5],i,j,m,n;
clrscr();
printf("Enter the no of rows:");
scanf("%d",&m);
printf("Enter the no of columns:");
scanf("%d",&n);
printf("Enter First matrix\n");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
scanf("%d",&a[i][j]);
}
printf("Enter Second matrix\n");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
scanf("%d",&b[i][j]);
}
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
c[i][j]=a[i][j]+b[i][j];
}
printf("Addition of matrix\n");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
printf("%d ",c[i][j]);
printf("\n");
}
getch();
return 0;
}
27 | P a g e
[Type text]
C Programming
Output:
Enter the no of rows:3
Enter the no of columns:3
Enter First matrix
593
725
931
Enter Second matrix
412
669
529
Addition of matrix
9 10 5
13 8 14
14 5 10
28 | P a g e
[Type text]
C Programming
23. Write a program to find string length.
#include<stdio.h>
#include<conio.h>
int main()
{
char str[100];
int l;
clrscr();
printf("Enter the string:");
gets(str);
for(l=0; str[l]; l++);
printf("The length of %s is -> %d",str,l);
getch();
return 0;
}
Output:
Enter the string;Dipen
The length of Dipen is -> 5
29 | P a g e
[Type text]
C Programming
24. Write a program to print size of int, float, double variable.
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
printf("Size of int is -> %d\n",sizeof(int));
printf("Size of long is -> %d\n",sizeof(float));
printf("Size of double is -> %d\n",sizeof(double));
getch();
return 0;
}
Output:
Size of int is -> 2
Size of long is -> 4
Size of double is -> 8
30 | P a g e
[Type text]
C Programming
25. Write a program that will read a text and count all
occurrences of a particular word.
#include<stdio.h>
#include<conio.h>
int main()
{
char str[100],find[100];
int i,j,cnt=0,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++)
{
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();
return 0;
}
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
31 | P a g e
[Type text]
C Programming
26. 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>
int 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();
return 0;
}
Output:
Enter the string :Kartik
Kartik in ascending order is -> Kaikrt
32 | P a g e
[Type text]
C Programming
27. Write a program that appends the one string to another
string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char str[100],app[20];
clrscr();
printf("Enter the string :");
gets(str);
printf("Enter the srting that you want to append : ");
gets(app);
printf("The string is : %s\n",str);
printf("The string you want to append is : %s\n",app);
printf("The string after append is : %s\n",strcat(str,app));
getch();
return 0;
}
Output:
Enter the string :Dipen
Enter the srting that you want to append : Prajapati
The string is : Dipen
The string you want to append is : Prajapati
The string after append is : Dipenprajapati
33 | P a g e
[Type text]
C Programming
28. Write a program that finds a given word in a string.
#include<stdio.h>
#include<conio.h>
int 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();
return 0;
}
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
34 | P a g e
[Type text]
C Programming
29. 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;
}
35 | P a g e
[Type text]
C Programming
/*
******Output******
Enter X:1.2
Enter n:5
Ans 0.932736
36 | P a g e
[Type text]
C Programming
30. 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
37 | P a g e
[Type text]
C Programming
31. Write a program to add first n numbers.
#include<stdio.h>
#include<conio.h>
void sum(int);
int main()
{
int n;
clrscr();
printf("Enter a number : ");
scanf("%d",&n);
sum(n);
getch();
return 0;
}
void sum(int n)
{
int i,s=0;
for(i=1;i<=n;i++)
{
s+=i;
}
printf("Sum of first %d integer is %d\n",n,s);
}
Output:
Enter a number : 10
Sum of first 10 integer is 55
38 | P a g e
[Type text]
C Programming
32. 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 : 151
151 is pelindrome
39 | P a g e
[Type text]
C Programming
33. 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 : Dipen
Dipen in upper case is DIPEN
40 | P a g e
[Type text]
C Programming
34. 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 : Dipen
Dipen in reverse order is nepiD
41 | P a g e
[Type text]
C Programming
35. 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;
}
42 | P a g e
[Type text]
C Programming
Output:
Enter the string :Dipen
Enter the srting that you want to find : p
p is found in Dipen
Enter the string :Dipen
Enter the srting that you want to find : w
w is not found in Dipen
43 | P a g e
[Type text]
C Programming
36. 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++)
{
44 | P a g e
[Type text]
C Programming
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 : Dipen
Enter the team : India
Enter the batting average : 100
For player 2
Enter the name of player : Dhaval
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
Dipen
100
Pakistan Dhaval
5
India
Tendulkar
45
India
Dhoni
48
India
Yuvi
39
45 | P a g e
[Type text]
C Programming
37. 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);
46 | P a g e
[Type text]
C Programming
}
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 : Dipen
Enter the code : D
Enter the age : 21
Enter the weight : 50
Enter the height : 175
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
-------------------------------Dipen D 21 175 50
47 | P a g e
[Type text]
C Programming
38. 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;
}
48 | P a g e
[Type text]
C Programming
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
49 | P a g e
[Type text]
C Programming
39. 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]);
}
50 | P a g e
[Type text]
C Programming
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
51 | P a g e
[Type text]
C Programming
40. Write a function using pointers to add two matrices and to
return the resultant
matrix to the calling function.
#include<stdio.h>
#include<conio.h>
int a[5][5],b[5][5],row,col;
void add(int(*)[5]);
int main()
{
int c[5][5],i,j;
clrscr();
printf("Enter row : ");
scanf("%d",&row);
printf("Enter column : ");
scanf("%d",&col);
printf("Enter matrix A :\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter matrix B :\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&b[i][j]);
}
}
add(c);
printf("Addition :\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
52 | P a g e
[Type text]
C Programming
return 0;
}
void add(int c[5][5])
{
int i,j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
}
/*
Enter row : 3
Enter column : 3
Enter matrix A :
333
333
333
Enter matrix B :
555
555
555
Addition :
8
8
8
8
8
8
8
8
8
*/
53 | P a g e
[Type text]
C Programming
41. Write a program to read data from keyboard, write it to a file
named STUDENT again read the same data from STUDENT file
and write it into DATA file. Same data should be displayed on
the screen.
#include<stdio.h>
#include<conio.h>
#include<process.h>
struct stud
{
int rno;
char *nm;
};
void main()
{
struct stud *s;
int n,i;
FILE *fp,*fp1;
clrscr();
printf("Enter how many record you want to input : ");
scanf("%d",&n);
s=(struct stud *)malloc(n*sizeof(struct stud));
fp=fopen("STUD.txt","w");
for(i=0;i<n;i++)
{
printf("\n\tInformation for student : %d\n",i+1);
printf("Enter Roll No : ");
scanf("%d",&s[i].rno);
printf("Enter Name : ");
fflush(stdin);
gets(s[i].nm);
fprintf(fp,"%5d %-20s\n",s[i].rno,s[i].nm);
}
fclose(fp);
fp=fopen("STUD.txt","r");
fp1=fopen("DATA.txt","w");
printf("\nContent of the STUD.txt file is\n");
printf("Roll No Name\n");
printf("---------------------------\n");
while(!feof(fp))
{
fscanf(fp,"%5d %20s\n",&s[i].rno,s[i].nm);
fprintf(fp1,"%5d %-20s\n",s[i].rno,s[i].nm);
printf("%7d %-20s\n",s[i].rno,s[i].nm);
}
54 | P a g e
[Type text]
C Programming
fcloseall();
getch();
}
Output:
Enter how many record you want to input : 3
Information for student : 1
Enter Roll No : 1
Enter Name :Dipen
Information for student : 2
Enter Roll No : 2
Enter Name : Umesh
Information for student : 3
Enter Roll No : 3
Enter Name : Mehul
Content of the STUD.txt file is
Roll No Name
--------------------------1 Dipen
2 Umesh
3 Mehul
55 | P a g e
[Type text]
C Programming
42. Write a program to create linear linked list interactively and
print out the list and total number of items in the list.
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
struct node
{
int info;
struct node *next;
}*head,*last,*temp,*n;
void menu(); // to select the choise
void create(); // insert at last
void display(); // to print the link list
int i;
void main()
{
i=0;
clrscr();
menu();
getch();
}
void menu()
{
int ch;
printf("1. Create\n");
printf("2. Display\n");
printf("3. Exit\n");
printf("Enter choise : ");
scanf("%d",&ch);
switch(ch)
{
case 1 : create();
case 2 : display();
case 3 : printf("You have choose to exit\n");
getch(); exit(0);
default : printf("Invalid choise\n"); menu();
}
}
56 | P a g e
[Type text]
C Programming
void create()
{
n= new node;
printf("Enter the information : ");
scanf("%d",&n->info);
if(i==0)
{
head=n;
i++;
}
else
{
last->next=n;
i++;
}
last=n;
last->next=NULL;
menu();
}
void display()
{
if(head==NULL)
{
printf("No information in list\n");
menu();
}
temp=head;
while(temp!=NULL)
{
printf("%d->",temp->info);
temp=temp->next;
}
printf("\n");
printf("Total number of elements in the link list are : %d\n",i);
menu();
}
57 | P a g e
[Type text]
C Programming
Output:
1. Create
2. Display
3. Exit
Enter choise : 1
Enter the information : 12
1. Create
2. Display
3. Exit
Enter choise : 1
Enter the information : 23
1. Create
2. Display
3. Exit
Enter choise : 1
Enter the information : 45
1. Create
2. Display
3. Exit
Enter choise : 2
12->23->45->
Total number of elements in the link list are : 3
1. Create
2. Display
3. Exit
Enter choise : 3
You have choose to exit
58 | P a g e
Related documents