int main() { float p, r, n, i; printf("Enter principal amount: "); scanf("%f", &p); printf("Enter rate of interest:"> int main() { float p, r, n, i; printf("Enter principal amount: "); scanf("%f", &p); printf("Enter rate of interest:">
[go: up one dir, main page]

0% found this document useful (0 votes)
118 views62 pages

PPS Lab Manual

Here are the steps to write a C program to calculate simple interest: 1. Define variables - principal amount (p), rate of interest (r), number of years (n), simple interest (i) 2. Take input from user for principal amount, rate of interest and number of years 3. Use formula to calculate simple interest: i = (p * r * n) / 100 4. Print the calculated simple interest Solution: #include <stdio.h> int main() { float p, r, n, i; printf("Enter principal amount: "); scanf("%f", &p); printf("Enter rate of interest:

Uploaded by

Thakor Aditya
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)
118 views62 pages

PPS Lab Manual

Here are the steps to write a C program to calculate simple interest: 1. Define variables - principal amount (p), rate of interest (r), number of years (n), simple interest (i) 2. Take input from user for principal amount, rate of interest and number of years 3. Use formula to calculate simple interest: i = (p * r * n) / 100 4. Print the calculated simple interest Solution: #include <stdio.h> int main() { float p, r, n, i; printf("Enter principal amount: "); scanf("%f", &p); printf("Enter rate of interest:

Uploaded by

Thakor Aditya
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/ 62

MERCHANT

ENGINEERING COLLEGE
BASNA
COMPUTER ENGINEERING DEPARTMNENT

Programming for Problem Solving


(3110003)

Lab Manual
CERTIFICATE

This is to certify that


Enrollment No. Of 1st SEM Computer
Engineering has satisfactorily completed the term work in
Subject of “Programming for Problem Solving’’(3110003)
within four walls of Merchant Engineering College, Basna

Date of submission:
Faculty College Seal HOD
Index

Sr. Practical name Date Page Sign


No. no.
1 a.Write a program to that performs as calculator
(addition, multiplication, division, subtraction).
b. Write a program to find area of triangle
(a=h*b*.5) a = area h = height b = base
2 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.
3 a. Write a C program to interchange two numbers.
b. Write a C program to enter a distance in to
kilometer and convert it in to meter, feet, inches and
centimeter.
4 a. Write a program to compute Fahrenheit from
centigrade (f=1.8*c +32)
b. Write a C program to find out distance travelled
by the equation d = ut + at^2
5 a. Write a C program to find that the accepted
number is Negative or Positive or Zero.
b. Write a program to read marks of a student from
keyboard whether the student is pass or fail( using if
else)
c. Write a program to read three numbers from
keyboard and find out maximum out of these three.
(nested if else)
6 Write a C program to check whether the entered
character is capital, small letter, digit or any special
character
7 a. Write a program to read marks from keyboard and
your program should display equivalent grade
according to following table(if else ladder)
Marks Grade
100 - 80 Distinction
79 - 60 First Class
59 - 40 Second Class
< 40 Fail
b. Write a c program to prepare 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.
8 a. Write a C program to read no 1 to 7 and print
relatively day Sunday to Saturday.
b. Write a C program to find out the Maximum and
Minimum number from given 10 numbers
c. Write a C program to input an integer number and
check the last digit of number is even or odd.
d. Write a C program to find factorial of a given
number.
9 a. Write a program to reverse a number.
b. Write a program to generate first n number of
Fibonacci series.
c. Write a program to find out sum of first and last
digit of a given number.
d. Write a C program to find the sum and average of
different numbers which are accepted by user as
many as user wants
e. Write a program to calculate average and total of 5
students for 3 subjects (use nested for loops)
10 a.Read five persons height and weight and count the
number of person having height greater than 170 and
weight less than 50,
b. Write a program to check whether the given
number is prime or not.
11 a. Write a program to evaluate the series
1^2+2^2+3^2+……+n^2
b. Write a C program to find 1+1/2+1/3+1/4+.
........+1/n.
c. Write a C program to find 1+1/2!+1/3!+1/4!+.
........... +1/n!.
d. Write a program to evaluate the series
sum=1-x+x^2/2!-x^3/3!+x^4/4! ......... ..-x^9/9!
12 a.Write a program to print following patterns :
i* ii * iii *****
** ** ****
*** *** ***
**** **** **
***** ***** *

b. Write a program to print following patterns :


i 1 ii 1234 iii 5555 iv) 1
) ) 5 ) 5
12 1234 4444 22
123 123 333 333
1234 12 22 4444
12345 1 1 555555

c. Write a program to print following patterns:


i) AAAAA ii) ABCDE
BBBB ABCD
CCC ABC
DD AB
E A
13 a. Write a C program to read and store the roll no
and marks of 20 students using array.
b. Write a program to find out which number is even
or odd from list of 10 numbers using array
c. Write a program to find maximum element from
1-Dimensional array.
d. Write a C program to calculate the average,
geometric and harmonic mean of n elements in an
array.
14 Write a program to sort given array in ascending
order (Use Insertion sort, Bubble sort, Selection sort,
Merge sort, Quick sort, Heap sort).
15 a. Write a program to find a character from given
string.
b. Write a program to replace a character in given
string.
c. Write a program to delete a character in given
string.
d. Write a program to reverse string.
e. Write a program to convert string into upper case
16 a. Write a program that defines a function to add first
n numbers.
b. Write a function in the program to return 1 if
number is prime otherwise return 0
c. 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.
d. Write a C program to use recursive calls to
evaluate F(x) = x – x3 / 3! + x5 / 5 ! – x7 / 7! + …
xn/ n!.
e. Write a program to find factorial of a number
using recursion.
17 a. Write a C program using global variable, static
variable.
b. Write a function that will scan a character string
passed as an argument and convert all lowercase
character into their uppercase equivalents
c. Write a program to read structure elements from
keyboard.
e. 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.
d. Define structure data type called time_struct
containing three member’s integer hour, integer
minute and integer second. Develop a program that
would assign values to the individual number and
display the time in the following format: 16: 40:51
18 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 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
19 a. 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.
b. Write a program to print address of variable using
pointer.
c. Write a C program to swap the two values using
pointers.
20 a. Write a C program to print the address of character
and the character of string using pointer.
b. Write a program to access elements using pointer.
c. Write a program for sorting using pointer.
d. Write a program to write a string in file
21 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 named “odd”
and write all even numbers into file named “even”.
Display all the contents of these file on screen
PRACTICAL -1
Aim: a.Write a program to that performs as calculator (addition, multiplication, division,
subtraction).

#include <stdio.h>
int main()
{
int number1, number2, addition, subtraction, multiply;
float divide;

printf("Enter Number 1\n");


scanf("%d", &number1);
printf("Enter Number 2\n");
scanf ("%d", &number2);
addition = number1 + number2 ;
subtraction = number1 - number2;
multiply = number1 * number2;
divide = number1 / (float) number2; //typecasting
printf("Addition = %d\n",addition);
printf("Subtraction = %d\n",subtraction);
printf("Multiplication = %d\n",multiply);
printf("Division = %.2f\n",divide);

return 0;
}

Output:
b. Write a program to find area of triangle (a=h*b*.5) a = area h = height b = base.

Description: Below is the step by step descriptive logic to find area of a triangle.

1. Input base of the triangle. Store in some variable say b(base).


2. Input height of the triangle. Store in some variable say h(height).
3. Use triangle area formula to calculate area i.e. a = b * h * 05
4. Print the resultant value of area.

Solution:

#include<stdio.h>
int main()
{
float a,h,b;
printf("\n Enter height :");
scanf("%f",&h);
printf("\n Enter base :");
scanf("%f",&b);
a=b*h*0.5;
printf("\n The Area of triangle = %.2f",a);

return 0;
}

Output:
PRACTICAL -2
Aim: 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.
Description: Step by step descriptive logic to calculate simple interest.

1. Input principle amount in some variable say p(principle)


2. Input years in some variable say n(years).
3. Input rate in some variable say r(rate).
4. Find simple interest using formula I = (p * r * n) / 100).
5. Finally, print the resultant value of I(interest).

Solution:

#include <stdio.h>

int main()
{
int n;
float p, r, I;
printf("\n Enter Amount :");
scanf("%f",&p);
printf("\n Enter No of Years :");
scanf("%d",&n);
printf("\n Enter Rate :");
scanf("%f",&r);
I = (p*r*n)/100;
printf("\n Interest = %.2f",I);

return 0;
}

Output:
PRACTICAL -3
Aim: a. Write a C program to interchange two numbers.

#include <stdio.h>
int main()
{
int a,b;
printf("Enter Value of a :");
scanf("%d",&a);
printf("Enter Value of B :");
scanf("%d",&b);

a=a+b;
b=a-b;
a=a-b;

printf("\nAfter Interchange Values A = %d B = %d",a,b);


return 0;

}
Output:
b. Write a C program to enter a distance in to kilometer and convert it in to meter, feet, inches and
centimeter.

#include <stdio.h>
int main()
{
float km;
printf("Enter Length in Kilometer : ");
scanf("%f",&km);
printf("\n %.2f KM = %.2f Meters",km,km*1000);
printf("\n %.2f KM = %.2f Feets",km,km*3280.84);
printf("\n %.2f KM = %.2f Inches",km,km*39370.08);
printf("\n %.2f KM = %.2f Centimeters",km,km*1000*100);

return 0;
}

Output:
PRACTICAL -4
Aim: a. Write a program to compute Fahrenheit from centigrade (f=1.8*c +32)

#include <stdio.h>
int main()
{
float F,C;
printf("Enter Temperature in Celsius : " );
scanf("%f",&C);
F = (C * 1.8) + 32;
printf("\n %.2f Celsius = %.2f Fahrenheit",C, F);
return 0;
}
Output:

b. Write a C program to find out distance travelled by the equation d = ut + at^2

#include<stdio.h>
int main() {
float u, a, d;
int t;

printf("\nEnter the value of a : ");


scanf("%f", & a);
printf("\nEnter the value of u : ");
scanf("%f", & u);
printf("\nEnter the value of t : ");
scanf("%d", & t);
d = (u * t) + (a * t * t) / 2;
printf("\n The Distance : %.2f", d);
return 0;
}
Output:

PRACTICAL -5
Aim: a. Write a C program to find that the accepted number is Negative or Positive or
Zero.
Description: We will use the above logic inside if to check number for
negative, positive or zero, Step by step descriptive logic to check negative,
positive or zero.
1. Input a number from user in some variable say num.
2. Check if(num < 0), then number is negative.
3. Check if(num > 0), then number is positive.
4. Check if(num == 0), then number is zero

Solution:

#include<stdio.h>
int main()
{
int no;
printf("\n Enter any number : ");
scanf("%d",&no);
if(no==0)
{
printf("\n Entered Number is Zero");
}
else if(no>0)
{
printf("\n Entered Number is Positive");
}
else
{
printf("\n Entered Number is Negative");
}
return 0;
}
Output:

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

#include<stdio.h>
int main()
{
int marks;
printf("\n Enter Marks from 0-70 :");
scanf("%d",&marks);
if(marks<23)
{
printf("\n Sorry ..! You are Fail");
}
else
{
printf("\nCongratulation ...! You are Pass");
}
return 0;
}
Output:

c. Write a program to read three numbers from keyboard and find out maximum out of
these three. (nested if else)
#include<stdio.h>
int main()
{
int a,b,c;
printf("\n Enter First Number :");
scanf("%d",&a);
printf("\n Enter Second Number :");
scanf("%d",&b);
printf("\n Enter Third Number :");
scanf("%d",&c);
if(a>b)
{
if(a>c)
{
printf("\n First Number %d is maximum",a);
}
else
{
printf("\n Third Number %d is maximum",c);
}
}
else
{
if(b>c)
{
printf("\n Second Number %d is maximum",b);
}
else
{
printf("\n Third Number %d is maximum",c);
}
}
return 0;
}
Output:
PRACTICAL -6
Aim: Write a C program to check whether the entered character is capital, small letter, digit or any
special character.

#include<stdio.h>
int main()
{
char ch;
printf("\nEnter Any Character :");
scanf("%c",&ch);
if(ch>='0' && ch<='9')
{
printf("\n Entered Character is Digit");
}
else if(ch>='A' && ch<='Z')
{
printf("\n Entered Character is Capital Letter");
}
else if(ch>='a' && ch<='z')
{
printf("\n Entered Character is Small Letter");
}
else
{
printf("\n Entered Character is Special Character");
}
return 0;
}
Output:

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

#include<stdio.h>
int main() {
int marks;
printf("\n Enter Marks between 0-100 :");
scanf("%d", & marks);
if (marks > 100 || marks < 0) {
printf("\n Your Input is out of Range");
} else if (marks >= 80) {
printf("\n You got Distinction");
} else if (marks >= 60) {
printf("\n You got First Class");
} else if (marks >= 40) {
printf("\n You got Second Class");
} else {
printf("\n You got Fail");
}
return 0;
}
Output:
b. Write a c program to prepare 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.

#include<stdio.h>
int main() {
float basic;
printf("\n Enter Basic Salary :");
scanf("%f", & basic);
printf("\n===================================");
printf("\n SALARY SLIP");
printf("\n===================================");
printf("\n Basic : %.2f", basic);
printf("\n DA : %.2f", basic * 0.10);
printf("\n HRA : %.2f", basic * 0.075);
printf("\n MA : %.2f", 300.00);
printf("\n===================================");
printf("\n GROSS : %.2f", basic + (basic * 0.10) + (basic * 0.075) + 300.00);
printf("\n===================================");
printf("\n PF : %.2f", basic * 0.125);
printf("\n===================================");
printf("\n NET : %.2f", (basic + (basic * 0.10) + (basic * 0.075) + 300.00) - (basic * 0.125));
printf("\n===================================");
return 0;
}
Output:
PRACTICAL -8
Aim:  a. Write a C program to read no 1 to 7 and print relatively day Sunday to Saturday.

#include<stdio.h>
int main()
{
int no;
printf("\n Enter Day no between 1-7 : ");
scanf("%d",&no);
switch(no)
{
case 1:
printf("\n Sunday");
break;
case 2:
printf("\n Monday");
break;
case 3:
printf("\n Tuesday");
break;
case 4:
printf("\n Wednesday");
break;
case 5:
printf("\n Thursday");
break;
case 6:
printf("\n Friday");
break;
case 7:
printf("\n Saturday");
break;
default:
printf("\n Please Enter Proper Input");
break;
}
return 0;
}
Output:

b. Write a C program to find out the Maximum and Minimum number from given 10
numbers

#include <stdio.h>
int main() 
{
int a[10],i,min,max;
for(i=0;i<10;i++)
{
printf("\n Enter Interger 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];
}
}
}Z
printf("\n Minimum : %d",min);
printf("\n Maximum : %d",max);
return 0;
}
Output:

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

#include <stdio.h>
int main() 
{
int i;
printf("\n Enter any Number : ");
scanf("%d",&i);
// Condition can be write as 
// if(i%2==0)
// ultimately Even number has last digit even and same for odd
if((i%10)%2==0)
{
printf("\n Last Digit of Number is Even");
}
else
{
printf("\n Last Digit of Number is Odd");
}
return 0;
}
Output:

d. Write a C program to find factorial of a given number.

#include <stdio.h>
int main()
{
int no,fact=1;
printf("\n Enter No to find its Factorial : ");
scanf("%d",&no);
scanf("%d",&no);
while(no>1)
{
fact=fact*no;
no=no-1;
}
printf("\n Factorial of entered no is : %d",fact);
return 0;
}
Output:

PRACTICAL -9
Aim: a. Write a program to reverse a number.

#include <stdio.h>
int main()
{
int no,rev=0;
printf("\n Enter No to make it Reverse : ");
scanf("%d",&no);
while(no>0)
{
rev=(rev*10)+(no%10);
no=no/10;
}
printf("\n Reverse of entered no is : %d",rev);
return 0;
}
Output:

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

#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:

c. Write a program to find out sum of first and last digit of a given number.

#include <stdio.h>
int main()
{
int no,sum=0;
printf("\n 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("\n Sum of First & Last Digit is : %d",sum);
return 0;
}
Output:

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

#include <stdio.h>
int main()
{
int no,sum=0,i=0,val;
printf("\n How many nos you want to enter : ");
scanf("%d",&no);
while(i<no)
{
printf("Enter No [%d]:",i+1);
scanf("%d",&val);
sum=sum+val;
i++;
}
printf("\n Sum = %d",sum);
printf("\n Sum = %.2f",((float)sum)/no);
return 0;
}
Output:

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

#include<stdio.h>
int main()
{
int student=0,sum=0,marks=0,sub;
for(student=0;student<5;student++)
{
sum=0;
printf("\n Student - %d ",student+1);
for(sub=0;sub<3;sub++)
{
printf("\nEnter Marks for Subject - %d:- ",sub+1);
scanf("%d",&marks);
sum=sum+marks;
}
printf("\n For Student - %d : ",student+1);
printf("\n Sum = %d",sum);
printf("\n Average = %.2f",((float)sum)/sub);
}
return 0;
}
Output:

PRACTICAL -10
Aim: a.Read five persons height and weight and count the number of person having height
greater than 170 and weight less than 50,

#include<stdio.h>
int main()
{
int person,height,weight,count=0;
for(person=0;person<5;person++)
{
printf("\n Enter Detail of Person - %d",person+1);
printf("\n Enter Height : ");
scanf("%d",&height);
printf("\n Enter Weight : ");
scanf("%d",&weight);
if(height>170)
{
if(weight<50)
{
count++;
}
}
}
printf("\n Total Person having Height > 170 and Weight < 50 : %d",count);
return 0;
}
Output:

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

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

PRACTICAL -11
Aim: a. Write a program to evaluate the series 1^2+2^2+3^2+……+n^2

#include<stdio.h>
int main()
{
int n,i,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 = %d",sum);
return 0;
}
Output:

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

#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:

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

#include<stdio.h>   
int main()
{
int n,i,j,fact=1;
float sum=0;
printf("\n 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("\n Sum of Series = %f",sum);
return 0;
}
Output:

d. Write a program to evaluate the series sum=1-x+x^2/2!-x^3/3!+x^4/4! ......... ..-x^9/9!

#include<stdio.h>
#include<math.h>
int main()
{
int n,i,j,x,fact=1;
float sum=0;
printf("\n Enter Highest Power Value (Max 9):");
scanf("%d",&n);
printf("\n Enter the 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);
return 0;
}

Output:
PRACTICAL -12

Aim: a.Write a program to print following patterns :


i* ii * iii *****
** ** ****
*** *** ***
**** **** **
***** ***** *
i.
#include <stdio.h>
int main(void)
{
int i,j;
for(i=0;i<5;i++)
{
for(j=0;j<=i;j++)
{
printf(" *");
}
printf("\n");
}
return 0;
}
Output:

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

b. Write a program to print following patterns :


i) 1 ii) 12345 iii) 55555 iv) 1
12 1234 4444 22
123 123 333 333
1234 12 22 4444
12345 1 1 555555

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

Output:

II. #include <stdio.h>


int main()
{
int i,j;
for(i=0;i<5;i++)
{
for(j=0;j<5-i;j++)
{
printf("%d",j+1);
}
printf("\n");
}
return 0;
}
Output:

III. #include <stdio.h>


int main()
{
int i,j;
for(i=0;i<5;i++)
{
for(j=0;j<5-i;j++)
{
printf("%d",5-i);
}
printf("\n");
}
return 0;
}
Output:

IV. #include <stdio.h>


int main()
{
int i,j;
for(i=0;i<5;i++)
{
for(j=0;j<=i;j++)
{
printf("%d",i+1);
}
printf("\n");
}
return 0;
}
Output:

c. Write a program to print following patterns:


i) AAAAA ii) ABCDE
BBBB ABCD
CCC ABC
DD AB
E A
I.
#include <stdio.h>
int main(void) {
int i, j;
for (i = 0; i < 5; i++) {
for (j = 0; j < 5 - i; j++) {
printf("%c", 'A' + i);
}
printf("\n");
}
return 0;
}
Output:

II.
#include <stdio.h>
int main(void) {
int i, j;
for (i = 0; i < 5; i++) {
for (j = 0; j < 5 - i; j++) {
printf("%c", 'A' + j);
}
printf("\n");
}
return 0;
}

Output:

PRACTICAL -13
Aim: a. Write a C program to read and store the roll no and marks of 20 students using
array.
#include <stdio.h>
int main(void) {
int rollno[20], marks[20], i;
for (i = 0; i < 20; i++) {
printf("\n Enter Roll of Student [%d]", i + 1);
scanf("%d", & rollno[i]);
printf("\n 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]);
} return 0;
}
Output:

b. Write a program to find out which number is even or odd from list of 10 numbers using
array
#include <stdio.h>
int main(void) {
int a[10], i;
for (i = 0; i < 9; i++) {
printf("\n 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]);
}
}
return 0;
}
Output:

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

#include <stdio.h>
int main(void) {
int a[50], i, n, max;
printf("\n Enter How many numbers you want to enter [Max 50] : ");
scanf("%d", & n);
for (i = 0; i < n; i++) {
printf("\n 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);
return 0;
}
Output:

d. Write a C program to calculate the average, geometric and harmonic mean of n elements in an
array.

#include<stdio.h>
#include<math.h>
int main() {
float a[50], sum = 0, sum1 = 0, sum2 = 1;
int i, n;
printf("\n How many numbers you want to enter :");
scanf("%d", & n);
for (i = 0; i < n; i++) {
printf("\n 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));
return 0;
}
Output:

PRACTICAL -14
Aim: Write a program to sort given array in ascending order (Use Insertion sort, Bubble
sort, Selection sort, Merge sort, Quick sort, Heap sort).

// Sorting of 1-D array using Selection sort


#include <stdio.h>

int main() {
int a[10], i, j, n, min, temp;
printf("\n Enter How many numbers you want to enter: ");
scanf("%d", & n);
for (i = 0; i < n; i++) {
printf("\n Enter Value at Position [%d] :", i + 1);
scanf("%d", & a[i]);
}
for (i = 0; i < n - 1; i++) {
// Find the minimum element in unsorted array
min = a[i];
for (j = i + 1; j < n; j++) {
if (a[j] < a[i]) {
min = j;
// Swap the found minimum element with the first element
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
printf(" %d ->", a[i]);
}
printf(" %d ->", a[i]);
return 0;
}
Output:

PRACTICAL -15
Aim: a. Write a program to find a character from given string.
#include <stdio.h>
int main() {
char str[20], ch, flag = 1;
int i = 0;
printf("\n 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:

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


#include<stdio.h>
#include<math.h>

int main(){
char s[10],chr,repl_chr;
int i=0;

printf("\n Enter a String:");


scanf("%s",&s);
printf("\n Enter character t be replaced:");
scanf("%s",&chr);
printf("\n Enter replacement character:");
scanf("%s",&repl_chr);

while(s[i]!='\0'){
if(s[i]==chr){
s[i]=repl_chr;
}
i++;
}
printf("\n Modified string after replacement is: %s",s);
getch();
}
Output:

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

#include <stdio.h>
#include <string.h>
int main()
{
char str[100], ch;
int i, len, j;
printf("\n Enter any String : ");
gets(str);
printf("\n Enter the Character to delete : ");
scanf("%c", &ch);
len = strlen(str);

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


{
if(str[i] == ch)
{
for(j = i; j < len; j++)
{
str[j] = str[j + 1];
}
len--;
i--;
}
}
printf("\n Now the string is '%c' = %s ", ch, str);
return 0;
}
Output:
d. Write a program to reverse string.
#include <stdio.h>
#include <string.h>
int main()
{
  char s[100]; //string declaration
  printf("Enter a string:");
  gets(s); //input
  strrev(s); //reversing string
  printf("The reverse of the string: %s\n", s);
  return 0;
}

Output:

e. Write a program to convert string into upper case.

#include<stdio.h>
#include<string.h>
int main()
{
char str[20];
int i;
printf("Enter String : ");
scanf("%s",str);

for(i=0;i<=strlen(str);i++)
{
if(str[i]>=97&&str[i]<=122)
str[i]=str[i]-32;
}
printf("\nString in Uppercase: %s",str);
return 0;
}
Output:

PRACTICAL -16
Aim: a. Write a program that defines a function to add first n numbers.

#include <stdio.h>

int getsum(int);

int main(void) {
int n;
printf("Enter Any number n = ");
scanf("%d", & n);
printf("\n SUM = %d", getsum(n));
return 0;
}
int getsum(int n) {
return ((n * (n + 1)) / 2);
}

Output:

b. Write a function in the program to return 1 if number is prime otherwise return 0.

#include <stdio.h>
int isprime(int);
void main() {
int n, c = 0;
printf("\nEnter n: ");
scanf("%d", & n);
c = isprime(n);
if (c > 0)
printf("\nNumber %d is not prime.\n", n);
else
printf("\nNumber %d is prime.\n", n);
}
int isprime(int n) {
int i, c = 0;
for (i = 2; i < n; i++) {
if (n % i == 0) {
c++;
break;
}
}
return c;
}

Output:

c. 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.

#include<stdio.h>
void swap(int,int);
int main()
{
int x=10,y=20;
printf("\n value of x and y before swapping:");
printf("\n value of x:\t%d",x);
printf("\n value of y:\t%d",y);

swap(x,y);
printf("\n value of x and y after swapping:");
printf("\n value of x:\t%d",x);
printf("\n value of y:\t%d",y);
getch();
}
void swap(int a,int b){
int temp=a;
a=b;
b=temp;
}
Output:
d. Write a C program to use recursive calls to evaluate F(x) = x – x3 / 3! + x5 / 5 ! – x7 / 7! +
… xn/ n!.

#include<stdio.h>
#include<conio.h>
int main()
{
float series(float,int),x;
int n;
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:

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


#include<stdio.h>

int fact(int);

int main() {
int n;
printf("\n 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:
PRACTICAL -17
Aim: a. Write a C program using global variable, static variable.

I. Use of Static Variable

#include<stdio.h>
int fun()
{
static int count = 0;
count++;
return count;
}
int main(){
printf("%d",fun());
printf("%d",fun());
printf("%d",fun());
getch();
}
Output:

II. Use of Global Variable

#include<stdio.h>
int cnt = 0;
int fun()
{
cnt++;
printf("%d",cnt);
}
int main(){
fun();
cnt++;
printf("%d",cnt);
fun();
getch();
}

Output:

b. Write a function that will scan a character string passed as an argument and convert all
lowercase character into their uppercase equivalents
#include<stdio.h>
int main()
{
char str[20];
printf("\n Enter a string:");
gets(str);
cUpper(str);
getch();
}
cUpper(char str[]){
int i=0;
while(str[i]!='\0'){
if(str[i]>='a'&& str[i]<='z')
printf("%c",str[i]-32);
else
printf("%c",str[i]);
i++;
}
}
Output:

c. Write a program to read structure elements from keyboard.


#include <stdio.h>

struct book {
int id;
char name[20];
float price;
};

int main(void) {
struct book b1;
printf("\n Enter Book Id : ");
scanf("%d", & b1.id);
fflush(stdin);
printf("\n Enter Book Name : ");
scanf("%s", b1.name);
printf("\n Enter Book Price : ");
scanf("%f", & b1.price);

printf("\nBook Id = %d", b1.id);


printf("\nBook Name = %s", b1.name);
printf("\nBook Price = %.2f", b1.price);
return 0;
}
Output:

e. 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.

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

int main(void) {
int i = 0;
for (i = 0; i < 5; i++) {
printf("\n Enter Person Name : ");
scanf("%s", p[i].name);
printf("\n Enter Person Date of Joining (dd-mm-yyyy) : ");
scanf("%s", p[i].doj);
printf("\n Enter Person 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 DOJ = %s", p[i].doj);
printf("\n Salary = %.2f", p[i].salary);
}
return 0;
}
Output:

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

#include <stdio.h>

struct time_struct
{
int hour;
int minute;
int second;
}t;

int main(void)
{
printf("\n Enter Hour : ");
scanf("%d",&t.hour);
printf("\n Enter Minute: ");
scanf("%d",&t.minute);
printf("\n Enter Second : ");
scanf("%d",&t.second);

printf("\n Time %d:%d:%d",t.hour%24,t.minute%60,t.second%60);


return 0;
}

Output:
PRACTICAL -18
Aim: 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 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.

#include <stdio.h>
#include <string.h>

struct cricket {
char player_name[20];
char team_name[20];
float batting_avg;
}
p[50], t;

int main(void) {
int i = 0, j = 0, n = 50;

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


printf("\n Enter Player Name : ");
scanf("%s", p[i].player_name);
printf("\n Enter Team Name : ");
scanf("%s", p[i].team_name);
printf("\n Enter Batting Average : ");
scanf("%f", & p[i].batting_avg);
}

//Sorting of Data based on Team


for (i = 0; i < n - 1; 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];
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);
}
return 0;
}
Output:
PRACTICAL -19
Aim: a. 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.

#include <stdio.h>
struct student_record
{
char name[20];
char branch[20];
int total_marks;
}p[10];

int main(void)
{
int i=0,n=10;

for(i=0;i<n;i++)
{
printf("\n Enter Student Name : ");
scanf("%s",p[i].name);
printf("\n Enter Students Branch : ");
scanf("%s",p[i].branch);
printf("\n Enter Students Marks : ");
scanf("%d",&p[i].total_marks);
}

for(i=0;i<n;i++)
{
printf("\n Student %d Detail",i+1);
printf("\n Name = %s",p[i].name);
printf("\n Branch = %s",p[i].branch);
printf("\n Total marks = %d",p[i].total_marks);
}
return 0;
}

Output:
b. Write a program to print address of variable using pointer.

#include <stdio.h>
int main(void) {
int i = 15;
int * p;
p = & i;
printf("\n Address of Variable i = %u", p);
return 0;
}
Output:
c. Write a C program to swap the two values using pointers.

#include <stdio.h>
void swap(int * , int * );

int main(void) {
int i = 25, j = 35;
printf("\n Before Swapping i = %d j = %d", i, j);
swap( & i, & j);
printf("\n After Swapping i = %d j = %d", i, j);
return 0;
}
void swap(int * a, int * b) {
* a = * a + * b;
* b = * a - * b;
* a = * a - * b;
}
Output:
PRACTICAL -20
Aim: a. Write a C program to print the address of character and the character of string
using pointer.
Output:

b. Write a program to access elements using pointer.

#include <stdio.h>
int main(void)
{
int a[10]={2,4,6,7,8,9,1,2,3,4};
int *p,i=0;
p=&a[0];
while(i<10)
{
printf("\n Position : %d Value : %d",i+1,*(p+i));
i++;
}
return 0;
}
Output:

c. Write a program for sorting using pointer.

#include <stdio.h>
int main(void)
{
int a[10]={2,10,6,7,8,9,5,3,4,1};
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:

d. Write a program to write a string in file

#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:
PRACTICAL -21
Aim: 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 named “odd” and write all even numbers into file
named “even”. Display all the contents of these file on screen

#include <stdio.h>
main()
{
FILE *f1, *f2, *f3;
int number, i;
printf("Contents of DATA file\n\n");
f1 = fopen("DATA", "w"); /* Create DATA file */
for(i = 1; i <= 30; 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); /* Write to EVEN file */
else
putw(number, f2); /* Write to ODD file */
}
fclose(f1);
fclose(f2);
fclose(f3);
f2 = fopen("ODD","r");
f3 = fopen("EVEN", "r");
printf("\n\nContents of ODD file\n\n");
while((number = getw(f2)) != EOF)
printf("%4d", number);
printf("\n\nContents of EVEN file\n\n");
while((number = getw(f3)) != EOF)
printf("%4d", number);
fclose(f2);
fclose(f3);
}
Output:

You might also like