Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
17 views77 pages

Orignal File

Uploaded by

riddhipatel5820
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views77 pages

Orignal File

Uploaded by

riddhipatel5820
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 77

230840131067

PRACTICAL SET -1
1.1 Write a program to that performs as calculator (addition, subtraction,
multiplication, division).

Code:

#include <stdio.h>

void main ()
{
float a,b, add, sub,multiply, division;
printf ("Enter two numbers");
scanf ("%f %f",&a ,&b);
add= a+b;
sub= a-b;
multiply= a*b;
division= a/b;
printf (" \n add of two numbers= %f",add);
printf ("\n subtraction of two numbers= %f",sub);
printf ("\n multiplication of two numbers= %f",multiply);
printf ("\n division of two numbers= %f",division);
}

Output:

1
230840131067

1.2 Write a program to find area of triangle (a=h*b*.5)


a= area
h= height
b= base

Code:

#include <stdio.h>

void main()
{
int b ,h, area;
printf ("enter the b and h");
scanf ("%d %d", &b, &h);
area=0.5*b*h;
printf("the area of triangle= %d", area);
}

Output:

2
230840131067

1.3 Write a program to calculate simple interest (I = (p*r*n)/100)


i= simple interest
p= principal amount
r= rate of interest
n= number of years

Code:

#include <stdio.h>

void main()
{
float p, r, n, simple_interest;
printf ("enter p: ");
scanf ("%f",&p);
printf ("enter r:");
scanf ("%f",&r);
printf ("enter n:");
scanf ("%f",&n);
simple_interest= (p*r*n)/100;
printf ("calculated simple interest =%f", simple_interest);
}

Output:

3
230840131067

1.4 Write a C program to interchange two numbers.

Code:

#include<stdio.h>
int main ()
{
int a=10,b=99;
printf("before swap: a=%d || b=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\nafter swap: a=%d || b=%d ",a,b);
return 0;
}

Output:

4
230840131067

1.5 Write a C program to enter distance in kilometer and convert it in to


meter, feet, inches and centimetre.

Code:

#include <stdio.h>

void main()
{
float km, m, inches, feet, cm;
printf("enter the distance in km:");
scanf("%f", &km);
m= 0.001*km;
inches= 0.0000254*km;
feet= 0.000305*km;
cm= 0.00001*km;
printf("\n the converted value in meter is %f ",m);
printf("\n the converted value in inches is %f ",inches);
printf("\n the converted value in feet is %f ",feet);
printf("\n the converted value in centimeter is %f ",cm);
}

Output:

5
230840131067

1.6 Write a program to compare Fahrenheit from centigrade (f=1.8*c+32).

Code:

#include <stdio.h>

void main()
{
int c, f;
printf("enter the value of c:");
scanf("%d",&c);
f= 1.8*c+32;
printf("the computed value is: %d", f);
}

Output:

6
230840131067

1.7 Write a C program to find out distance travelled by the equation


d=ut+at^2.

Code:

#include <stdio.h>

void main()
{
int u, a, t, distance;
printf("enter value of u:");
scanf("%d", &u);
printf("enter value of a:");
scanf("%d", &a);
printf("enter value of t:");
scanf("%d", &t);
distance=(u*t)+(a*t*t);
printf("distance travelled: %d", distance);
}

Output:

7
230840131067

PRACTICAL SET -2
2.1 Write a C program to find that the accepted number is negative , or
positive or zero.

Code:

#include <stdio.h>

void main()
{
int a;
printf("enter the value of a:");
scanf("%d", &a);
if (a>0)
{
printf("the number is positive");
}
else if(a<0)
{
printf("the number is negative");
}
else
{
printf("the number entered is zero");
}
}

Output:

8
230840131067

2.2 Write a program to read marks of a student from keyboard whether the
student is fail or pass. (using if else)

Code:

#include <stdio.h>

void main()
{
int marks;
printf("enter the marks:");
scanf("%d",&marks);
if (marks>23)
{
printf("the student is pass...");
}
else
{
printf("the student is fail!!!");
}
}

Output:

9
230840131067

2.3 Write a program to read three numbers from keyboard and find out
maximum out of these three. (using nested if else)

Code:

#include <stdio.h>

void main()
{
int x, y, z;
printf("enter x:");
scanf("%d",&x);
printf("enter y:");
scanf("%d",&y);
printf("enter z:");
scanf("%d",&z);
if (x>y & x>z)
{
printf("the maximum out of three is: %d",x);
}
else if (y>x & y>z)
{
printf("the maximum out of three is: %d",y);
}
else
{
printf("the maximum out of three is : %d",z);
}
}

Output:

10
230840131067

2.4 Write a C program to print whether the entered character is capital, small
letter, digit or any special character.

Code:

#include<stdio.h>
int main()
{
char ch;
printf("enter character ch:");
scanf("%c",&ch);
if(ch >=65 && ch <=90)
printf("upper");
else if(ch >= 97 && ch <=122)
printf("lower");
else if(ch >= 48 && ch <= 57)
printf("number");
else
printf("symbol");
return 0;
}

Output:

11
230840131067

2.5 Write a program to read marks from keyboard and your program should
display equivalent grade according to the following table. (if else ladder)
Marks Grade
100-80 Distinction
79-60 First class
59-40 Second class
<40 Fail

Code:

#include <stdio.h>

void main()
{
int marks;
printf("enter the marks:");
scanf("%d", &marks);
if(marks>80)
{ printf("distinction grade..."); }
else if(marks<79 & marks>60)
{ printf("first class!!!"); }
else if (marks<59 & marks>40)
{ printf("second class!!!"); }
else
{ printf("fail..."); }
}

Output:

12
230840131067

2.6 Write a C program to print a pay slip using following data:


Da= 10% of basic, Hra= 7.50% of basic, Ma=300,
Pf= 12.50% of basic, Gross= basic+Da+ Hra+Ma,
Nt= Gross- Pf.

Code:

#include <stdio.h>
void main()
{
float b;
printf("enter your basic salary:");
scanf("%f", &b);
printf("\n ===============================");
printf("\n SALARY SLIP");
printf("\n ===============================");
printf("\n Basic: %f", b);
printf("\n Da: %f", b*0.10);
printf("\n Hra: %f", b*0.075);
printf("\n Ma: %f", 300.00);
printf("\n ===============================");
printf("\n Gross: %f", b+ (b*0.10)+ (b*0.075)+ 300.00);
printf("\n ===============================");
printf("\n Pf: %f", b*0.125);
printf("\n ===============================");
printf("\n Nt: %f", (b+ (b*0.10)+ (b*0.075)+ 300.00)- (b*0.125));
}

Output:

13
230840131067

2.7 Write a C program to read no. 1 to 7 and print relatively day Sunday to
Saturday.

Code:

#include <stdio.h>
void main()
{
int week;
printf("enter the day 1 to 7");
scanf("%d", &week);
switch(week)
{
case 1: printf("\n Monday");
break;
case 2: printf("\n Tuesday");
break;
case 3: printf("\n Wednesday");
break;
case 4: printf("\n Thursday");
break;
case 5: printf("\n Friday");
break;
case 6: printf("\n Saturday");
break;
case 7: printf("\n Sunday");
break;
default:
printf("please enter valid number!!!");
}
}

Output:

14
230840131067

PRACTICAL SET -3
3.1 Write a C program to find out the maximum and minimum number from
given 10 numbers.

Code:
#include <stdio.h>
void main()
{

int a[10],i,min,max;
for(i=0;i<10;i++)
{ printf("Enter Integer Value %d : ",i+1);
scanf("%d",&a[i]);
if(i==0)
{

min=max=a[i];
}
else
{ if(min>a[i])
{ min=a[i]; }
if(max<a[i])
{ max=a[i]; }
}
}

printf("\n Minimum : %d",min);


printf("\n Maximum : %d",max);
}

15
230840131067

Output:

16
230840131067

3.2 Write a C program to input an integer number and check the last digit of
number is even or odd.

Code:
#include <stdio.h>
int main()
{
int i;

printf(" Enter any Number : ");


scanf("%d",&i);
if((i%10)%2==0)

{ printf(" Last Digit of Number is Even"); }


else
{ printf("Last Digit of Number is Odd"); }
return 0;
}

Output:

17
230840131067

3.3 Write a C program to find the factorial of a given number.

Code:
#include <stdio.h>
int main()
{
int no,fact=1;

printf("Enter no to find its factorial:");


scanf("%d",&no);
while(no>1)
{

fact=fact*no;
no=no-1;
}

printf("factorial of entered no is :%d",fact);


return 0;
}

Output:

18
230840131067

3.4 Write a program to reverse a number.

Code:
#include <stdio.h>
int main()
{
int no,rev=0;

printf("Enter No to make it Reverse : ");


scanf("%d",&no);
while(no>0)
{
rev=(rev*10)+(no%10);
no=no/10;
}

printf("Reverse of entered no is : %d",rev);


return 0;
}

Output:

19
230840131067

3.5 Write a program to generate first n number of Fibonacci series.

Code:
#include <stdio.h>
int main()
{
int no=10,i=0,j=1;
printf(" %d %d",i,j);
while(no>0)
{
printf(" %d",i+j);
j=i+j;
i=j-i;
no--;
}
return 0;
}

Output:

20
230840131067

3.6 Write a program to find out the sum of first and last digit of a given
number.
Code:
#include <stdio.h>
int main()
{
int no,sum=0;
printf("Enter Any Number :");
scanf("%d",&no);
if(no<10)
{ sum = sum + (no*2); }
else
{
sum = sum + (no%10);
while(no>9)
{ no = no /10; }
sum = sum + no;
}
printf("Sum of First & Last Digit is : %d",sum);
return 0;
}

Output:

21
230840131067

3.7 Write a C program to find the sum and average of different numbers
which are accepted by user as many as user wants.

Code:
#include <stdio.h>
int main()
{
int no,sum=0,i=0,val;

printf("How Many Number You Want to Enter : ");


scanf("%d",&no);
while(i<no)

{
printf("Enter No [%d]:",i+1);
scanf("%d",&val);
sum=sum+val;
i++;
}
printf("Sum = %d",sum);
printf(" \n Avg = %.2f",((float)sum)/no);
return 0;
}

Output:

22
230840131067

3.8 Write a program to calculate average and total of 5 students for 3 subjects
( use nested for loops).

Code:
#include<stdio.h>
int main()
{
int student=0,sum=0,marks=0,sub;
for(student=0;student<5;student++)
{
sum=0;

printf("Enter Marks for Student - %d ",student+1);


for(sub=0;sub<3;sub++)
{

printf("Enter Marks for Subject - %d ",sub+1);


scanf("%d",&marks);
sum=sum+marks;

}
printf("For Student - %d : ",student+1);
printf("\n Sum = %d",sum);
printf(" Average = %.2f",((float)sum)/sub);
}
return 0;
}

23
230840131067

Output:

24
230840131067

3.9 Read 5 persons height and weight and count the number of person having
height greater than 170 and weight less than 50.

Code:
include<stdio.h>

int main()
{

int person,height,weight,count=0;
for(person=0;person<5;person++)
{
printf("Enter Detail of Person - %d",person+1);
printf("\n Enter Height : ");
scanf("%d",&height);
printf("Enter Weight : ");
scanf("%d",&weight);
if(height>170)
{
if (weight<50)
{ count++; }
}

}
printf("Total Person having Height > 170 and Weight < 50 : %d",count);
return 0;
}

25
230840131067

Output:

26
230840131067

3.10 Write a program to check whether the given number is prime or not.

Code:
#include<stdio.h>
int main()
{
int no,i;
printf("Enter No to check wheather its prime or not :");
scanf("%d",&no);
for(i=2;i<no;i++)
{
if(no%i==0)
{
printf("%d is not prime",no);
break;
}
}
if(no==i)
{ printf("%d is prime",no); }
return 0;
}

Output:

27
230840131067

3.11 Write a program to evaluate the series 1^2+2^2+3^2+…..+n^2.

Code:
#include<stdio.h>
int main()
{
int n,i;
float sum=0;
printf("\n enter value of n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{ sum=sum+(i*i); }
printf("\n sum of series=%f",sum);
return 0;

Output:

28
230840131067

3.12 Write a C program to find 1+1/2+1/3+1/4+….+1/n.

Code:
#include<stdio.h>
int main()
{
int n,i;
float sum=0;
printf("\n Enter Value of n : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{ sum=sum+(1.0/i); }
printf("\n Sum of Series = %f",sum);
return 0;

Output:

29
230840131067

3.13 Write a C program to find 1+1/2!+1/3!+1/4!+…..+1/n!.

Code:
#include<stdio.h>

int main()
{
int n, i, j, fact = 1;
float sum = 0;
printf("Enter Value of n : ");
scanf( "%d", & n);
for (i = 1; i <= n; i++)
{
fact = 1;
for (j = i; j > 0; j--)
{ fact = fact * j; }
sum = sum + (1.0 / fact);
}

printf("Sum of Series = %f", sum);


return 0;
}

Output:

30
230840131067

3.14 Write a program to evaluate the series sum 1-x+x^2/2!-x^3/3!+…..

Code:
#include <stdio.h>
#include <math.h>
void main()
{
int n, i, x, fact=1;
float sum=0;
printf(" enter value of n:");
scanf("%d", &n);
printf(" enter value of x:");
scanf("%d", &x);
for(i=0; i<=n; i++);
{
fact=1;
for(j=i;j>0 ; j--)

{ fact=fact*j; }
if(i%2==0)
{ sum= sum+ (pow(x,i)/fact); }
else
{ sum= sum- (pow(x,i)/fact); }
}
printf("\n sum of series=%f", sum);
}

31
230840131067

Output:

32
230840131067

PRACTICAL SET -4
4.1 Write a program to print following patterns:
(i) *
**
***
****
*****

Code:

#include <stdio.h>
void main()
{
int i, j;
for(i=1; i<=5; i++)
{ for(j=1; j<=i; j++)
{ printf("*"); }
printf("\n");
}
}

Output:

33
230840131067

(ii) *
**
***
****
*****

Code:

#include <stdio.h>
#include <conio.h>
int main()
{
int i ,j, k=0;
for( i=1; i<=5; i++, k=0)
{
for ( j=1; j<=5-i ; j++)
{ printf(" "); }
while (k!=2*i-1)
{
printf("*");
k++;
}
printf("\n");
}
return 0;
}

Output:

34
230840131067

(iii) *****
****
***
**
*

Code:

#include <stdio.h>
void main()
{
int i, j;
for(i=1; i<=5; i++)
{ for(j=1; j<=6-i; j++)
{ printf("*"); }
printf("\n");
}
}

Output:

35
230840131067

4.2 Write a program to print following patterns:


(i)1
12
123
1234
12345

Code:

#include <stdio.h>
void main()
{
int i, j;
for(i=1; i<=5; i++)
{ for(j=1; j<=i; j++)
{ printf("%d", j); }
printf("\n");
}
}

Output:

36
230840131067

(ii) 12345
1234
123
12
1

Code:

#include <stdio.h>
void main()
{
int i, j;
for(i=1; i<=5; i++)
{ for(j=1; j<=6-i; j++)
{ printf("%d", j); }
printf("\n");
}
}

Output:

37
230840131067

(iii) 55555
4444
333
22
1

Code:

#include <stdio.h>
void main()
{
int i, j;
for(i=5; i>=1; i--)
{ for(j=1; j<=i; j++)
{ printf("%d", i); }
printf("\n");
}
}

Output:

38
230840131067

4.3 Write a program to print the following patterns:


(i) AAAAA
BBBB
CCC
DD
E

Code:

#include <stdio.h>
void main()
{
int i, j ,rows;
printf("enter the number of rows:");
scanf("%d", &rows);
for(i=1; i<=rows; i++)
{
for( j=1; j<=6-i; j++)
{
printf("%c",'A'+i-1);
}
printf("\n");
}
}

Output:

39
230840131067

(ii) ABCDE
ABCD
ABC
AB
A

Code:

#include <stdio.h>
void main()
{
int i, j ,rows;
printf("enter the number of rows:");
scanf("%d", &rows);
for(i=1; i<=rows; i++)
{
for( j=1; j<=6-i; j++)
{
printf("%c",'A'+j-1);
}
printf("\n");
}
}

Output:

40
230840131067

PRACTICAL SET -5
5.1 Write a C program to read and store the roll no. and marks of 20 students
using array.

Code:

#include <stdio.h>
void main()
{
int rollno[20],marks[20],i;
for(i=0;i<20;i++)
{
printf("Enter Roll of Student [%d]",i+1);
scanf("%d",&rollno[i]);
printf("Enter Mark of Student [%d]",i+1);
scanf("%d",&marks[i]);
}
for(i=0;i<20;i++)
{ printf("\n Roll No : %d Marks : %d",rollno[i],marks[i]); }
}

Output:

41
230840131067

42
230840131067

5.2 Write a program to find out which number is even or odd from list of 10
numbers using array.

Code:

#include <stdio.h>
void main()
{
int a[10],i;
for(i=0;i<=9;i++)
{
printf("Enter Value in Array at Position [%d] :",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<=9;i++)
{
if(a[i]%2==0)
{ printf("\n %d is an EVEN number.",a[i]); }
else
{ printf("\n %d is an ODD number.",a[i]); }
}
}

Output:

43
230840131067

5.3 Write a program to find maximum element from 1-Dimensional array.

Code:

#include <stdio.h>
void main()
{
int a[50],i,n,max;
printf("Enter How many numbers you want to enter [Max 50] : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter Value in Array at Position [%d] :",i+1);
scanf("%d",&a[i]);
if(i==0)
{ max=a[i]; }
else
{
if(max<a[i])
{ max=a[i];}
}
}
printf("\n Maximum Value in Array = %d",max);
}

Output:

44
230840131067

5.4 Write a C program to calculate the average, geometric and harmonic


mean of n elements in an array.

Code:

#include <stdio.h>
#include <math.h>
void main()
{
float a[50],sum=0,sum1=0,sum2=1;
int i,n;
printf("How many numbers you want to enter :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter Value at Position [%d] : ",i+1);
scanf("%f",&a[i]);
sum=sum+a[i];
sum1=sum1+(1.0/a[i]);
sum2=sum2*a[i];
}
printf("\n Average = %f",sum/n);
printf("\n Geometric Mean = %f",pow(sum2,(1.0/n)));
printf("\n Harmonic Mean = %f",n*pow(sum1,-1));
}

Output:

45
230840131067

PRACTICAL SET -6
6.1 Write a program to find a character from the given string.

Code:

#include <stdio.h>
int main()
{
char str[20],ch,flag=1;
int i=0;
printf("Enter String ");
gets(str);
printf("Enter Character to Search in String :");
scanf("%c",&ch);
printf("\n Character ");
for(i=0;str[i]!='\0';i++)
{
if(str[i]==ch)
{
printf(" %d ",i+1);
flag=0;
}
}
if(flag==1)
{
printf("NOT FOUND");
}
return 0;
}

Output:

46
230840131067

6.2 Write a program to replace a character in given string.

Code:

#include<stdio.h>
#include<stdlib.h>
int main()
{
char str[50],ch1,ch2;
int i;
printf(" Enter String : ");
scanf("%[^\n]s",str);
fflush(stdin);
printf(" Enter Character to Find : ");
scanf("%c",&ch1);
fflush(stdin);
printf(" Enter Character to Replace : ");
scanf("%c",&ch2);
for(i=0;str[i]!='\0';i++)
{
if(str[i]==ch1)
{ str[i]=ch2; }
}
printf("\n Final String = %s",str);
return 0;
}

Output:

47
230840131067

6.3 Write a program to delete a character in given string.

Code:

#include<stdio.h>
#include<stdlib.h>
int main()
{
char str[50],ch;
int i,j;
printf(" Enter String : ");
scanf("%[^\n]s",str);
fflush(stdin);
printf(" Enter Character to Delete : ");
scanf("%c",&ch);
for(i=0;str[i]!='\0';i++)
{
if(str[i]==ch)
{
for(j=i;j<str[j]!='\0';j++)
{
str[j]=str[j+1];
}
i--;
}
}
printf("\n Final String = %s",str);
return 0;
}

Output:

48
230840131067

6.4 Write a program to reverse string.

Code:

# include <stdio.h>
#include <string.h>
void main()
{
char str[20];
printf("enter string to be reversed : ");
scanf("%s",str);
printf(" after the reverse of a string: %s", strrev(str));
}

Output:

49
230840131067

6.5 Write a program to convert string into upper case.

Code:

#include<stdio.h>
int main()
{
char str[50];
int i;
printf(" Enter String : ");
scanf("%[^\n]s",str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]>='a' && str[i]<='z')
{
str[i]=str[i]-32;
}
}
printf("\n Upper Case String = %s",str);
return 0;
}

Output:

50
230840131067

PRACTICAL SET -7
7.1 Write a program that defines a function to add two numbers.

Code:

#include <stdio.h>
int sum( int n);
int main()
{
int n;
printf("enter value of n");
scanf("%d", &n);
int result= sum(n);
printf(" the sum of %d numbers is: %d ",n,result);
return 0;
}
int sum( int n)
{
int sum=0, i;
for (i=1; i<=n; i++)
{sum+=i;}
return sum;
}

Output:

51
230840131067

7.2 Write a function in the program to return 1 if number is prime


otherwise return 0.

Code:

#include <stdio.h>
int prime (int n);
int main()
{
int n;
printf("enter value of n:");
scanf("%d", &n);
int result=prime(n);
if (result==1)
{printf("%d is a prime number",n);}
else
{printf("%d is not a prime number",n);}
return 0;}
int prime(int n)
{
int i;
if (n<=1)
{return 0;}
for (i=2; i<n; i++)
{if (n%i==0)
{return 0;}
}
return 1;
}
Output:

52
230840131067

7.3 Write a function exchange to interchange the values of two variables ,


say x and y, illustrate the use of this function in a calling function.

Code:

#include <stdio.h>
void exch(int x, int y);
void main()
{
int x, y;
printf("enter x and y: ");
scanf("%d %d", &x, &y);
printf("values before exchange x=%d and y=%d", x, y);
exch(x,y);
}
void exch(int x, int y)
{
int z;
z=x;
x=y;
y=z;
printf("values after exchange x=%d and y=%d", x, y);
}

Output:

53
230840131067

7.4 Write a C program to use recursive calls to evaluate F(x)=x-


x^3/3!+x^5/5!-x^7/7!+….x^n/n! .

Code:

#include<stdio.h>
#include<math.h>
float rec_call(int, int);
int fact(int);
int main()
{
int n, x;
float sum = 0;
printf("\n Enter Value of X :");
scanf("%d", & x);
printf("\n Enter no of iteration n :");
scanf("%d", & n);
sum = rec_call(x, n);
printf("Sum = %f", sum);
return 0;
}
float rec_call(int x, int n)
{
static float sum;
if (n == 1)
return sum + x;
if (n % 2 == 0)
{
sum = sum - ((pow(x, (2 * n) - 1) * 1.0) / fact((2 * n) - 1));
}
else
{
sum = sum + ((pow(x, (2 * n) - 1) * 1.0) / fact((2 * n) - 1));
}
rec_call(x, --n);
}
int fact(int n)
{

54
230840131067

if (n == 1)
return 1;
return n * fact(n - 1);
}

Output:

55
230840131067

7.5 Write a program to find factorial of a number using recursion.

Code:

#include <stdio.h>
int fact(int);
int main()
{
int n;
printf("enter value of n:");
scanf("%d", &n);
printf(" factorial =%d", fact(n));
return 0;
}
int fact(int n)
{
if (n==1)
{ return 1;}
return n*fact(n-1);
}

Output:

56
230840131067

7.6 Write a C program using global variable, static variable.

Code:

#include <stdio.h>
int n, fact();
void main()
{
printf("enter n:");
scanf("%d", &n);
printf("factorial =%d", fact());
}
int fact()
{
static int ans=1;
if(n==1)
{ return ans;}
ans=n--*fact();
}

Output:

57
230840131067

7.7 Write a function that will scan a character string passed as an


argument and convert all lowercase characters into their uppercase
equivalents.

Code:
#include <stdio.h>
void UpperCase(char *);
int main()
{
char str[50];
printf("Enter String : ");
scanf("%s",str);
UpperCase(str);
printf("String in Upper Case : %s",str);
return 0;
}
void UpperCase(char *ch)
{
int i=0;
while(ch[i]!='\0')
{
if(ch[i]>='a' && ch[i]<='z')
{
ch[i]=ch[i]-32;
}
i++;
}
}

Output:

58
230840131067

7.8 Write a program to sort given array in ascending order (use insertion
sort, bubble sort, selection sort, merge sort, quick sort, and heap sort).

Code:

#include <stdio.h>
int main()
{
int a[10], i ,j,n , min,temp;
printf("enter how many numbers you want to enter:");
scanf("%d", &n);
for (i=0; i<n; i++)
{
printf("enter value at position[%d]:", i+1);
scanf(" %d",&a[i]);
}
for(i=0;i<n-1; i++)
{
min=a[i];
for(j=i+1; j<n;j++)
{
if(a[j]<a[i])
{
min=j;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
printf("%d -->", a[i]);
}
printf("%d -->", a[i]);
return 0;
}

59
230840131067

Output:

60
230840131067

PRACTICAL SET -8
8.1 Write program to read structure elements from keyboard.

Code:

#include <stdio.h>
struct book
{
int id;
char name[20];
float price;
}b1;

void main()
{
printf("enter book id: ");
scanf(" %d", &b1.id);
printf("enter book name: ");
scanf(" %s", &b1.name);
printf("enter book price: ");
scanf(" %f", &b1.price);
printf(" book id= %d", b1.id);
printf("\n book name= %s", b1.name);
printf(" \n book price= %f", b1.price);
}

Output:

61
230840131067

8.2 Define a structure type struct personal that would contain person
name, date of joining and salary using this structure to read this
information of 5 people and print the same on screen.

Code:

#include <stdio.h>
struct person
{
char name[20];
char doj[10];
float salary;
}p[5];

void main()
{
int i=0;
for(i=0; i<5; i++)
{
printf("enter name:");
scanf("%s", p[i].name);
printf(" enter date of joining ");
scanf("%s", p[i].doj);
printf(" enter salary:");
scanf("%f", &p[i].salary);}
for(i=0; i<5; i++)
{
printf("\n person %d detail", i+1);
printf(" \n name=%s", p[i].name);
printf(" \n name=%s", p[i].doj);
printf(" \n name=%f", p[i].salary);
}}

62
230840131067

Output:

63
230840131067

8.3 Define structure data type called time_struct containing three numbers
integer hour, integer minute and integer second. Develop a program
that would assign values to the individual member and display the time
in following format: 16:40:51.

Code:

#include <stdio.h>
struct time
{
int hour;
int minute;
int second;
}t1;
void main()
{
printf(" enter hour:");
scanf("%d", &t1.hour);
printf(" enter minute:");
scanf("%d", &t1.minute);
printf(" enter second:");
scanf("%d", &t1.second);
printf(" time %d:%d:%d", t1.hour,t1.minute, t1.second);
}

Output:

64
230840131067

8.4 Define a structure called cricket that will describe tha following
information :
Player name
Team name
Batting average

Using cricket, declare an array player with 50 elements and write a C


program to read the information about all the 50 players and print team
wise list containing names of players with their batting average.

Code:

#include <stdio.h>
#include <string.h>
struct cricket
{
char player_name[02];
char team_name[02];
float batting_avg;
}p[2],t;
void main()
{
int i=0, j=0, n=02;
for (i=0;i<n; i++)
{
printf("enter player name :");
scanf("%s", p[i].player_name);
printf("enter team name:");
scanf("%s", p[i].team_name);
printf("enter batting average ");
scanf("%f", p[i].batting_avg);
}
for (i=0;i<n; i++)
{
for (j=i;j<n; j++)
{
if(strcmp(p[i].team_name, p[j].team_name)>0)
{
t=p[i];
p[i]=p[j];

65
230840131067

p[j]=t;
}
}
}
j=0;

for(i=0; i<n; i++)


{
if (strcmp(p[i].team_name, p[j].team_name)!=0||i==0)
{
printf("\n team name %s",p[i].team_name);
j=i;
}
printf("\n player name= %s", p[i].player_name);
printf("\n batting average= %f", p[i].batting_avg);
}
}

Output:

66
230840131067

8.5 Design a structure student record to contain name, branch and total
marks obtained.
Develop a program to read data for 10 students in a class and print
them.

Code:

#include <stdio.h>
struct student_record
{
char name[10];
char branch[10];
int marks;
}p[10];
void main()
{
int i=0, n=10;
for(i=0; i<n; i++)
{
printf("enter student name:");
scanf("%s",p[i].name);
printf("enter student branch:");
scanf("%s",p[i].branch);
printf("enter student marks:");
scanf("%d",&p[i].marks);

}
for(i=0;i<n;i++)
{
printf("\n student %d detail",i+1);
printf("\n name= %s", p[i].name);
printf("\n name= %s", p[i].branch);
printf("\n name= %d", p[i].marks);
}
}

67
230840131067

Output:

68
230840131067

69
230840131067

PRACTICAL SET -9
9.1 Write a program to print address of variable using pointer.

Code:

#include <stdio.h>
int main()
{
int i=15;
int *p;
p=&i;
printf("\n Address of Variable i = %u",p);
return 0;
}

Output:

70
230840131067

9.2 Write a C program to swap the two values using pointer.

Code:

#include <stdio.h>
void swap(int *,int *);
void main()
{
int i=15,j=20;
printf("\n Before Swapping i = %d j = %d",i,j);
swap(&i,&j);
printf("\n After Swapping i = %d j = %d",i,j);
}
void swap(int *a,int *b)
{
*a=*a + *b;
*b=*a - *b;
*a=*a - *b;
}

Output:

71
230840131067

9.3 Write a C program to print the address of character and the character
of string using pointer.

Code:

#include <stdio.h>
int main(void)
{
char str[50];
char *ch;
printf("Enter String : ");
scanf("%s",str);
ch=&str[0];
while(*ch!='\0')
{
printf("\n Position : %u Character : %c",ch,*ch);
ch++;
}
return 0;
}

Output:

72
230840131067

9.4 Write a program to access the elements using pointer.

Code:

#include <stdio.h>
int main(void)
{
char str[50];
char *ch;
printf("Enter String : ");
scanf("%s",str);
ch=&str[0];
while(*ch!='\0')
{
printf("\n Position : %u Character : %c",ch,*ch);
ch++;
}
return 0;
}

Output:

73
230840131067

9.5 Write a program for sorting using pointer.

Code:

#include <stdio.h>
int main()
{
int a[10]={2,54, 23, 78, 32, 10};
int *p,i=0,j=0;
p=&a[0];
for(i=0;i<9;i++)
{
for(j=i+1;j<10;j++)
{
if(*(p+i) > *(p+j))
{
*(p+i) = *(p+i) + *(p+j);
*(p+j) = *(p+i) - *(p+j);
*(p+i) = *(p+i) - *(p+j);
}
}
}
printf("\n Sorted Values : ");
for(i=0;i<10;i++) {
printf("%d ",*(p+i)); }
return 0;
}

Output:

74
230840131067

PRACTICAL SET -10

10.1 Write a program to write a string in file.

Code:

#include<stdio.h>
#include<stdlib.h>
int main()
{
char str[50],ch[50];
FILE *fp;
printf("Enter String : ");
scanf("%[^\n]s",str);
fp = fopen("Test.txt","w");
fputs(str,fp);
fclose(fp);
fp = fopen("Test.txt","r");
fgets(ch,50,fp);
fclose(fp);
printf("\n String from File : %s",ch);
return 0;
}

Output:

75
230840131067

10.2 A file named data contains series of integer numbers. Write a C


program to read all numbers from file and then write all odd numbers
into file names “odd” and write all even numbers into file named
“even”. Display the contents of these file on screen.

Code:

#include<stdio.h>

int main()
{
FILE * f1, * f2, * f3;
int number, i, n = 10;

printf("Contents of DATA file\n\n");

f1 = fopen("DATA", "w");

for (i = 0; i < n; i++)


{
scanf("%d", & number);
if (number == -1)
{ break; }
putw(number, f1);
}
fclose(f1);

f1 = fopen("DATA", "r");
f2 = fopen("ODD", "w");
f3 = fopen("EVEN", "w");

while ((number = getw(f1)) != EOF) {


if (number % 2 == 0) {
putw(number, f3);
} else {
putw(number, f2);
}
}

76
230840131067

fclose(f1);
fclose(f2);
fclose(f3);

f2 = fopen("ODD", "r");
f3 = fopen("EVEN", "r");

printf("\n\n Contents of ODD file \n\n");

while ((number = getw(f2)) != EOF)


{ printf("%d ", number); }

printf("\n\nContents of EVEN file \n\n");

while ((number = getw(f3)) != EOF)


{ printf("%d ", number); }

fclose(f2);
fclose(f3);
return 0;
}

Output:

77

You might also like