[go: up one dir, main page]

0% found this document useful (0 votes)
53 views42 pages

ESC0102P - Profram For Problem Solving Lab File

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

ESC0102P - Profram For Problem Solving Lab File

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

w.e.f.

:August 2018
Noida International University Form No. Acad. -001
Department of Computer Science & Engineering

SCHOOL OF ENGINEERING & TECHNOLOGY

Lab Code: ESC103P

Lab Name: Programming for Problem Solving Lab


Manual
List of Experiments:

Problems based on if-then-else structure:


1. If the three sides of the triangle are entered through the keyboard, write a program to check
whether the triangle is isosceles or equilateral.
2. In a company an employee is paid under: If his basic salary is less then Rs.1500, then HRA=10% of
basic salary and DA=90% of basic salary .If his salary is either equal to or above Rs 1500, then
HRA=Rs 500 and DA= 98% of basic salary. If the employee’s salary is input through the keyboard
write a program to find his gross salary.
3. The current year and year in which the employee joined the organization are entered through the
keyboard. If the no of years for which the employee has served the organization is greater than 3
then a bonus of Rs.2500/- is given to the employee. If the years of service are not greater than
three, then the program should do nothing. Write a program to perform the said task.
4. Write a program to check whether a triangle is valid or not when the three angles of the triangle
are entered through the keyboard. A triangle is valid if the sum of all the three angles is equal to
180 degree.
5. If cost price and selling price of item is input through the keyboard, write a program to determine
whether the seller gas made profit or incurred loss. Also determine how much profit he made or
loss he incurred.
6. In a company worker efficiency is determined on the basis of the time required for a worker to
complete a particular job. If the time taken by the worker is between 2-3 hours, then the worker is
said to be highly efficient. If the time required by the worker is between 3-4 hours, then the worker
is ordered to improve speed. If the time taken is between 4-5 hours, the worker is given training to
improve his speed, and if the time taken by the worker is more than 5 hours, then the worker has
to leave the company. If time taken by the worker is input through the keyboard, write a program
to find the efficiency of the worker.
Problems based on while loop and for loop:

1. Write a program to print the cube of any number provided by the user.
2. Make a program to calculate the simple interest for 3 sets of p, n, r using while and for loop.
3. Write a program to print the sum of all the digits from 1 to 10 using while loop.
4. Write a program to print the digit from 1 to100 using while and for loop.
5. Using for loop print the following
pattern R=1 c=1 sum=2
R=1 c=2 sum=3
R=2 c=1 sum
=3 R=2 c=2
sum=4
6. Write a program to print the following pattern
7. Write a program to print the square and cube of any given
number. 8.
***** * 1
***** ** 12
***** *** 123
***** **** 1234
***** 12345

Problems based on 1-D Array, Array Manipulation, 2-D Array and String Operations:

1. Write a program to perform following operations on String(s) using a well-defined library function:
● Find the length of the string.
● Concatenate two strings
● Compare two given strings
● Copy the content of string to another string
2. Write a program to find average marks obtained by a class of 30 students in a test.
3. Write a program to find the maximum marks obtained by a student in 5 subjects.
4. Write a program to pick up the largest number from any 5 row by 5 column matrix.
5. Twenty five numbers are entered from the keyboard into an array. Write a program to find out
how many of them are positive, how many of them are negative and how many of them are
zeros.
6. Write a program to store n elements in an array and print all elements.
7. Write a program to compute the sum of all elements in an array.
8. Write a program to print the elements of an array in reverse order.

Problems based on Structures:

1. Write a program to enter name, price and page number of three books using structure.
2. Write a program to enter roll number and average marks of 3 students using structure.
3. Create a structure to specify data of customer in a bank. The data to be stored is: Account
number, Name, Balance in Account. Assume maximum of 200 customers in the bank. Write a
program to print name and account number of each customer with balance below Rs. 100.
4. A record contains name of cricketer, his age, number of test matches that he has played and the
average runs that he has scored. Create an array of structures to hold records of 20 such
cricketers.
5. There is a structure called employee that holds information like employee code, name, and year
of joining. Write a program to create an array of structures and enter some data into it. Then ask
the user to enter current year. Display the names of those employees whose tenure is more than
3 years according to given year.
Problems based on Function, Pointer, Call by Value and Call by Reference

1. Write function which receives a float and an integer from main (), find the product of these two
and returns the product which is printed through main ().
2. Write a function that receives marks received by a student in 3 subjects and returns the average
and percentage of these marks. Call this function from main and print the result in main.
3. Find the smallest number in an array.
4. Any year is entered through the keyboard. Write a function to determine whether the year is a
leap year or not.
5. Write a function that receives 5 integers and returns the sum, average of these numbers. Call
this function from main () and print the result in main ().
6. Write a program to add two numbers using pointers.
7. Write a program to store n elements in an array and print all elements using pointer.
8. Write a program to read array elements and print array addresses using pointer.
9. Write a program to compute the sum of all elements in an array using pointer.
10. Write a program to print the elements of an array in reverse order using pointer.

Problems based on Recursion, recursive functions, file handling operations and numerical method
problems:

1. Write a program to writes records to a file using structure.


2. Write a program for reading a string from the file and display them on screen.
3. Write a program to copy the content of one file to another file.
4. Write a program to display contents of a file on screen.
5. Write a program to count Chars, space, tabs and new lines in a file.
6. Write a program to calculate factorial of any inputted number with recursion and without recursion.
7. Write a program to calculate Fibonacci Series using recursive call.
8. Write a program to calculate Ackerman Function for any two non-negative integers using recursion.
Experiment No. 1

Experiment No.1 : If the three sides of the triangle are entered through the keyboard, write a
program to check whether the triangle is isosceles or equilateral.

Solution:

#include<stdio.h>

#include<conio.h>

void main()

int side1, side2, side3;

printf("Enter sides of triangle:");

scanf("%d%d%d",&side1,&side2,&side3);

if(side1 == side2 && side2 == side3)

printf("The Given Triangle is equilateral

");

else if(side1 == side2 || side2 == side3 || side3 == side1)

printf("The given Triangle is isosceles

");

else

printf("The given Triangle is scalene

");

getch();

}
Experiment No. 2

Following is the C program to check whether the triangle is equilateral, isosceles or scalene –

#include<stdio.h>

int main(){

int side1, side2, side3;

printf("Enter sides of triangle:");

scanf("%d%d%d",&side1,&side2,&side3);

if(side1 == side2 && side2 == side3)

printf("The Given Triangle is equilateral

");

else if(side1 == side2 || side2 == side3 || side3 == side1)

printf("The given Triangle is isosceles

");

else

printf("The given Triangle is scalene

");

return 0;

}
Experiment No. 3

In a company an employee is paid under: If his basic salary is less then Rs.1500, then HRA=10% of basic
salary and DA=90% of basic salary .If his salary is either equal to or above Rs 1500, then HRA=Rs 500 and DA=
98% of basic salary. If the employee’s salary is input through the keyboard write a program to find his gross
salary.

#include<stdio.h>

int main()

int cy,yj;

printf("Enter the current year and the year of joining-");

scanf("%d %d",&cy,&yj);

if(cy-yj>3)

printf("bonus of Rs 2500/-");

else

printf("program error");

}
Experiment No. 4

Write a program to check whether a triangle is valid or not when the three angles of the triangle are entered
through the keyboard. A triangle is valid if the sum of all the three angles is equal to 180 degree.

#include<stdio.h>

#include<conio.h>

int main()

int a, b, c, sum;

printf("Enter three angles of a triangle: ");

scanf("%d %d %d", &a, &b, &c );

sum = a+b+c;

if (sum == 180)

printf("Triangle is valid");

else

printf("Triangle is not valid");

return 0;

}
Experiment No. 5

In a company worker efficiency is determined on the basis of the time required for a worker to complete a
particular job. If the time taken by the worker is between 2-3 hours, then the worker is said to be highly
efficient. If the time required by the worker is between 3-4 hours, then the worker is ordered to improve
speed. If the time taken is between 4-5 hours, the worker is given training to improve his speed, and if the
time taken by the worker is more than 5 hours, then the worker has to leave the company. If time taken by
the worker is input through the keyboard, write a program to find the efficiency of the worker.

#include<stdio.h>

#include<conio.h>

int main()

float hours;

printf("Input the time taken by worker: ");

scanf("%f", &hours);

if(hours>=2 && hours<=3)

printf("Worker is highly efficient");

if(hours>3 && hours <=4)

printf("Worker needs to improve speed");

if(hours>4 && hours <=5)

printf("Give training to worker");

if(hours>5) printf("Worker is terminated");

return(0);

}
Experiment No. 6

Write a program to print the square and cube of any given number

#include<stdio.h>

#include<conio.h>

void main()

clrscr();

float side;

printf(“enter the value of side”);

scanf(“%f”,&side);

printf(“area is: %f”, side*side);

getch();

}
Experiment No. 7

Write a program to find the area of the Circle

#include<stdio.h>

#include<conio.h>

void main()

clrscr();

float radius;

printf(“enter the value of radius”);

scanf(“%f”,&radius);

printf(“area is: %f”, 3.14*radius*radius);

getch();

}
Experiment No. 8
If cost price and selling price of item is input through the keyboard, write a program to determine
whether the seller gas made profit or incurred loss. Also determine how much profit he made or
loss he incurred.

#include<stdio.h>

#include<conio.h>

void main()

int cp,sp,tl,tp;

printf("Enter the Cost price\n");

scanf("%d",&cp);

printf("Enter the Selling price\n");

scanf("%d",&sp);

if(sp>cp)

tp=sp-cp;

printf("The profit is %d",tp);

else if (sp<cp)

tl=cp-sp;

printf("The loss is %d",tl);

else

printf("There is neither profit nor loss");

getch();
}

Experiment No. 9

Write a program to print the square and cube of any given number

#include<stdio.h>

int main()

int number, cube;

printf(" \n Please Enter any integer Value : ");

scanf("%d", &number);

cube = number * number * number;

printf("\n Cube of a given number %d is = %d", number, cube);

return 0;

}
Experiment No. 10
Make a program to calculate the simple interest for 3 sets of p, n, r using while and for loop.

/*Calculate Simple Interest for 3 sets of p, n & r

p, n & r represent principle, no. of years & rate of interest*/

#include<stdio.h>

#include<conio.h>

void main()

int p,n,count=1;

float r,si;

clrscr();

while(count<=3)

printf(“Enter value for p,n & r\n\n”);

scanf(“%d%d%f”,&p,&n,&r);

si=(p*n*r)/100;

printf(“\nSimple Interest =%f”,si);

count++;

getch();

using for loop


Experiment No. 11

/*Calculate Simple Interest for 3 sets of p, n & r

p, n & r represent principle, no. of years & rate of interest*/

#include<stdio.h>

#include<conio.h>

void main()

int p,n,count;

float r,si;

clrscr();

for(count=1;count<=3;count++)

printf(“\n\nEnter the value for p, n & r\n\n”);

scanf(“%d%d%f”,&p,&n,&r);

si=p*n*r/100;

printf(“Simple Interest=%f”,si);

getch();

}
Experiment No. 12

Write a program to print the sum of all the digits from 1 to 10 using while loop.

#include <stdio.h>

int main(){

/* Declare variable i. */

int i;

for(i=1 ; i<=100; i++){

/* Print the value of i. */

printf("%d ",i);

return 0;

}
Experiment No. 13

Write a program to print Positive number.

#include <stdio.h>

int main() {

int n, i, sum = 0;

printf("Enter a positive integer: ");

scanf("%d", &n);

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

sum += i;

printf("Sum = %d", sum);

return 0;

}
Experiment No. 14

Write a program using for loop for printing pattern

#include <stdio.h>

int main()

int rows = 5;

// first loop for printing rows

for (int i = 0; i < rows; i++) {

// second loop for printing character in each rows

for (int j = 0; j <= i; j++) {

printf("* ");

printf("\n");

return 0;

}
Experiment No. 15

Write a program to print the square and cube of any given number.

#include<stdio.h>

#include<conio.h>

void main()

int a,s,c;

clrscr();

printf("\n Enter A Number: ");

scanf("%d",&a);

s=a*a; //Square = number * number

c=s*a; //Cube = Square * number

printf("\n Square of %d is = %d",a,s);

printf("\n\n Cube of %d is = %d",a,c);

getch();

}
Experiment No. 16

Program to print the given number pattern

12

123

1234

12345

#include <stdio.h>

int main()

int i, j, n;

printf ("enter number\n");

scanf ("%d",&n);

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

for (j=1;j<=i;j++)

printf ("%d",j);

printf ("\n");

}
Experiment No. 17

Write a program to find the area of the square

#include<stdio.h>

#include<conio.h>

void main()

clrscr();

float side;

printf(“enter the value of side”);

scanf(“%f”,&side);

printf(“area is: %f”, side*side);

getch();

}
Experiment No. 18

Write a program to find the area of the cube

#include<stdio.h>

#include<conio.h>

void main()

clrscr();

float side;

printf(“enter the value of side”);

scanf(“%f”,&side);

printf(“area is: %f”, side*side*side);

getch();

}
Experiment No. 19

Write a program to find the area of the Circle

#include<stdio.h>

#include<conio.h>

void main()

clrscr();

float radius;

printf(“enter the value of radius”);

scanf(“%f”,&radius);

printf(“area is: %f”, 3.14*radius*radius);

getch();

}
Experiment No. 20

Write a program to print 1 to 10 using do-while loop

#include <stdio.h>

int main() {

int i = 1;

do {

printf("%d\n", i);

i++;

} while (i<= 10);

return 0;

}
Experiment No. 21

Write a program to print 1 to 10 using for loop

#include <stdio.h>

int main() {

int i;

for (i = 1; i<= 10; i++) {

printf("%d\n", i);

return 0;

}
Experiment No. 22

Write a program to print the following pattern

***** * 1
***** ** 12
***** *** 123
***** **** 1234
***** 12345

#include <stdio.h>

int main()

int n;

printf("Enter the number of rows");

scanf("%d",&n);

for(int i=1;i<=n;i++)

for(int j=1;j<=i;j++)

printf("* ");

printf("\n");

return 0;

#include <stdio.h>

int main()

{
int n;

printf("Enter the number of rows");

scanf("%d",&n);

for(int i=1;i<=n;i++)

for(int j=1;j<i;j++)

printf(" ");

for(int k=1;k<=n;k++)

printf("*");

printf("\n");

return 0;

}
Experiment No. 23

Write a C program to calculate the sum of 1 to 50 numbers.

Resources: PC, Power, Compiler


Program:

#include<stdio.h>
#include<conio.h>
void main()
{
Int sum =0;
for(int i=1;i<=50;i++)
{
Sum +=I;
}
printf(“Sum is %d”,sum);
getch();
}

Result:- We have successfully compiled the program


Experiment No. 24

Write a C program to calculate the sum of two-digit numbers.

Resources: PC, Power, Compiler


Program:

#include<stdio.h>
#include<conio.h>
void main()
{
Int a,b,sum=0;
Printf(“Enter the first number:”);
Scanf(%d”, &a);
Printf(“Enter the second number:”);
Scanf(%d”, &b);
Sum=a+b;
Printf(“Sum is %d”,sum);
getch();
}

Result:- We have successfully compiled the program


Experiment No. 25

Write a C program to find the area of square, circle and rectangle.


Resources: PC, Power, Compiler
Program:

#include<stdio.h>
#include<math.h>
float squareArea(float side);
float circleArea(float rad);
float rectangleArea(float a, float b);

int main()
{
float a=5.0;
float b=10.0;
printf(“area is :%f”, rectangleArea(a,b));
return 0;
}
float squareArea(float side)
{
return side*side;
}
float circleArea(float rad)
{
return 3.14*rad*rad;
}
float rectangleArea(float a, float b)
{
return a*b;
}
Result:- We have successfully compiled the program
area is :50.000000
Experiment No. 26

Write a C program to print all natural numbers from 1 to n using loop.


Resources: PC, Power, Compiler

Program:
/**
* C program to print all natural numbers from 1 to n
*/

#include <stdio.h>

int main()
{
int i, n;

/* Input upper limit from user */


printf("Enter any number: ");
scanf("%d", &n);

printf("Natural numbers from 1 to %d : \n", n);

/*
* Start loop counter from 1 (i=1) and go till n (i<=n)
* increment the loop count by 1 to get the next value.
* For each repetition print the value of i.
*/
for(i=1; i<=n; i++)
{
printf("%d\n", i);
}

return 0;
}
Experiment No. 27

Write a C program to check whether a student is pass or fail


Resources: PC, Power, Compiler

Program:
/**
* C program to check whether a student is pass or fail

*/

#include <stdio.h>

int main() {
int marks;
printf("enter marks(0-100) \n");
scanf("%d", &marks );
if (marks <= 30){
printf ("fail\n");
printf("Better Luck Next Time");

}
else if ( marks >30 && marks <= 100)
{ printf(" pass");
printf("\nWell done!");
}
else { printf(" wrong marks");
}
return 0;
}

enter marks(0-100)

67

pass

Well done!
Experiment No. 28

Write a C program to find the factorial of n and n is given


Resources: PC, Power, Compiler

Program:
/**
* C program to find the factorial of n and n is given

*/

int fact(int n);

int main()

printf(“ factorial is: %d”, fact(6));

return 0;

int fact(int n)

if(n==0)

return 1;

else

return n*fact(n-1);

}
Experiment No. 29

Write a C program to find the given number is either positive or negative (even and odd)
Resources: PC, Power, Compiler
Program:

#include <stdio.h>

int main() {

int num;

printf("Enter an integer: ");

scanf("%d", &num);

if(num>=0)

printf("Positive \n");

// true if num is perfectly divisible by 2

if(num % 2 == 0)

printf("%d is even.", num);

else

printf("%d is odd.", num);

return 0;

Enter an integer: 45

Positive

45 is odd.
Experiment No. 30

Write a C program to convert celcius to farhenheit.


Resources: PC, Power, Compiler

Program:
/**
* C program to to convert celcius to farhenheit.

*/

#include <stdio.h>

float convertCelFahrenheit(float c)

return ((c * 9.0 / 5.0) + 32.0);

int main()

float celsius, fahrenheit;

printf("Enter temperature in Celsius: ");

scanf("%f", &celsius);

//called function to convert celsius to fahrenheit

fahrenheit = convertCelFahrenheit(celsius);

printf("%.2f Celsius = %.2f Fahrenheit", celsius, fahrenheit);

return 0;

}
Experiment No. 31

Write a C program to check whether a student is fail or pass.


Resources: PC, Power, Compiler

Program:
/**
* C program to check whether a student is fail or pass.
*/

#include <stdio.h>

int main(void) {

int marks;

printf("Enter Marks : ");

scanf("%d",&Marks);

if(Marks >= 40)

printf("\nResult is pass");

else

printf("\nResult is fail");

return 0;

}
Experiment No. 32

Write a C program to print the table of a number input by user.


Resources: PC, Power, Compiler

Program:
/**
* C program to print the table of a number input by user.
.
*/

#include <stdio.h>

int main()

int num, i; // declare a variable

printf (" Enter a number to generate the table in C: ");

scanf (" %d", &num); // take a positive number from the user

printf ("\n Table of %d", num);

// use for loop to iterate the number from 1 to 10

for ( i = 1; i <= 10; i++)

printf ("\n %d * %d = %d", num, i, (num*i));

return 0;

}
Experiment No. 33

Write a C program using call by value function


Resources: PC, Power, Compiler

Program:
/**
* C program using call by value function
.
*/

#include <stdio.h>

void swap(int , int); //prototype of the function

int main()

int a = 10;

int b = 20;

printf("Before swapping the values in main a = %d, b = %d\n",a,b); // printing the value of a and b in main

swap(a,b);

printf("After swapping values in main a = %d, b = %d\n",a,b); // The value of actual parameters do not change by c
hanging the formal parameters in call by value, a = 10, b = 20

void swap (int a, int b)

int temp;

temp = a;

a=b;

b=temp;

printf("After swapping values in function a = %d, b = %d\n",a,b); // Formal parameters, a = 20, b = 10

}
Experiment No. 34

Write a C program to make pattern of triangle


Resources: PC, Power, Compiler

Program:
/**
* C program to make pattern of triangle
.
*/
Include<stdio.h>

int main()

Int n,I,j;

printf(“Enter the number of rows:”);

scanf(“%d”,&n);

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

printf(“”);

for(j=1;j<=I;j++)

printf(“*”);

printf(“\n”);

‘return 0;

Problems based on 1-D Array, Array Manipulation, 2-D Array and String Operations:
Experiment No. 35

Write a program to perform following operations on String(s) using a well-defined library function:
● Find the length of the string.
● Concatenate two strings
● Compare two given strings
● Copy the content of string to another string

#include <stdio.h>

int main() {

char s[] = "Programming is fun";

int i;

for (i = 0; s[i] != '\0'; ++i);

printf("Length of the string: %d", i);

return 0;

#include <stdio.h>

int main() {

char s1[100] = "programming ", s2[] = "is awesome";

int length, j;

// store length of s1 in the length variable

length = 0;

while (s1[length] != '\0') {

++length;

}
// concatenate s2 to s1

for (j = 0; s2[j] != '\0'; ++j, ++length) {

s1[length] = s2[j];

// terminating the s1 string

s1[length] = '\0';

printf("After concatenation: ");

puts(s1);

return 0;

// Online C compiler to run C program online

#include<stdio.h>

int main()

//Program to print name, DOB and mobile number of user

char name[10];

int date, month, year;

double mob_no;

printf("Name: ");

gets(name);

printf("Date: ");

scanf("%d", &date);

printf("Month: ");

scanf("%d", &month);

printf("Year: ");

scanf("%d", &year);
printf("Mob_no: ");

scanf("%lf", &mob_no);

printf("Hi %s!! \nYour DOB is %d/%d/%d and mobile number is %lf", name, date, month, year, mob_no);

return 0;

You might also like