PPL Lab Assignment Code
PPL Lab Assignment Code
Batch-B-38
SapID-500086656
Experiment 2
Basics of Problem Solving and Program Control Flows
1.
#include<stdio.h>
int main()
{
float a,b ;
printf("Here you have to assign values to a and b and the programme will give you
sum,difference,multiplication & division \nso just write\n");
printf("the value of a is ");
scanf("%f",&a);
printf("\n the value of b is ");
scanf("%f",&b);
printf("\n Sum of a and b is %f",a+b);
printf("\n difference of a and b is %f",a-b);
printf("\n Multiplication of a and b is %f",a*b);
printf("\n Division of a and b is %f",a/b);
if (b==0)
printf("error");
}
2.
#include<stdio.h>
int main()
{
int a ;
printf("To find wheter the no. is even or prime\n\n");
printf("Enter a number\n\n");
scanf("%d",&a);
printf("%d",a);
if(a%2==0)
printf(" is an even no.");
else
printf(" is not an even no.");
}
3.
int main()
{
float a,b,c;
printf("Enter three no\n");
printf("a=");
scanf("%f",&a);
printf("\nb=");
scanf("%f",&b);
printf("\nc=");
scanf("%f",&c);
if(a>b&&a>c){
printf("a is the largest");
}
else
if(b>a&&b>c){
printf("b is the largest");
else{
4.
#include <stdio.h>
int add(int n1, int n2);
int main()
{
int n1,n2,product=0,i;
printf("Enter the first number: ");
scanf("%d",&n1);
printf("Enter the first number: ");
scanf("%d",&n2);
for(i=0; i<n2; i++){
product=add(product,n1);
}
printf("product of %d and %d are: %d\n",n1,n2,product);
return 0;
}
int add(int num1,int num2){
int i;
for(i=0; i<num2; i++)
num1++;
return num1;
}
Experiment 3
Programming Sequential Logic
List of Lab Activities:
1. Obtain the required inputs and compute the areas of the following shapes: (i)
Parallelogram (with base and height), (ii) Trapezoid (with height, long base, short
base), (iii) Rhombus (with height and side), (iv) Sphere (with radius), (v) Ellipse (with
major and minor radius)
2. Given two numbers. Demonstrate the swapping of the values (i) using a third
variable (ii) without using a third variable
3. Convert temperature from Celsius to Fahrenheit and Kelvin.
4. Print the given days in years-month-days format. E.g. 373 days = 1 year, 1 month, 1
day
1.
#include<stdio.h>
int main()
float S,R,P,TP;
float PI=3.142,E;
scanf("%f%f",&base,&height);//(address of ) store
P= base*height;
printf("\n Please Enter long base,short base and height of the trapezium: \n");
scanf("%f", &radius);
S= 4*(22/7)*radius*radius;
scanf("%f", &minor_radius);
printf("\n\n Enter major_radius of Ellipse : ");
scanf("%f", &major_radius);
E=PI*minor_radius*major_radius;
return 0;
2 (i).
#include<stdio.h>
int main()
num1 = num2;
num2 = t;
return 0;
2 (ii).
#include <stdio.h>
int main()
printf("Before Swapping \n First variable = %d\n Second variable = %d\n", num1, num2);
return 0;
}
3(i).
#include<stdio.h>
int main()
{
float celsius, Fahrenheit ;
printf("Enter temperature in celsius: ");
scanf("%f", &celsius);
Fahrenheit = 1.8 * celsius + 32.0;
printf("%0.2f celsius = %0.2f Fahrenheit\n", celsius, Fahrenheit);
return(0);
}
3(ii).
#include<stdio.h>
int main()
{
float c, fh, kel;
printf("Enter temperature in celsius: ");
scanf("%f", &c);
kel = 273.15 + c;
printf("%0.2f C = %0.2f Kelvin",c, kel);
return(0);
}
4.
// Let us assume 30 days in a month
#include <stdio.h>
int main()
{
int n_days, y, m, d;
printf("Input no. of days: ");
scanf("%d", &n_days);
y = (int) n_days/365;
n_days = n_days-(365*y);
m = (int)n_days/30;
d = (int)n_days-(m*30);
printf(" %d Year(s) \n %d Month(s) \n %d Day(s)", y, m, d);
return 0;
}
EXPERIMENT-4
Conditional Branching
List of Lab Activities:
Write algorithm and C program, compile, execute and test the code using Linux C compiler
with suitable test cases.
1. Find the biggest of 3 numbers.
2. Check whether a given year is leap year or not.
3. Find the roots of a quadratic equation.
4. Check if a given character is a vowel or consonant using Switch-Case statement
1.
int main()
{
float a,b,c;
printf("Enter three no\n");
printf("a=");
scanf("%f",&a);
printf("\nb=");
scanf("%f",&b);
printf("\nc=");
scanf("%f",&c);
if(a>b&&a>c){
printf("a is the largest");
}
else
if(b>a&&b>c){
printf("b is the largest");
else{
if (year % 400 == 0)
{
printf("%d is a leap year.", year);
}
else if (year % 4 == 0) {
printf("%d is a leap year.", year);
}
else {
printf("%d is not a leap year.", year);
}
return 0;
}
3.
#include <stdio.h>
#include<math.h>
int main()
{
int a,b,c,d;
float x1,x2;
printf("Input the value of a,b & c : ");
scanf("%d%d%d",&a,&b,&c);
d=b*b-4*a*c;
if(d==0)
{
printf("Both roots are equal.\n");
x1=-b/(2.0*a);
x2=x1;
printf("First Root Root1= %f\n",x1);
printf("Second Root Root2= %f\n",x2);
}
else if(d>0)
{
printf("Both roots are real and diff-2\n");
x1=(-b+sqrt(d))/(2*a);
x2=(-b-sqrt(d))/(2*a);
printf("First Root Root1= %f\n",x1);
printf("Second Root root2= %f\n",x2);
}
else
{
printf("Root are imaginary;\nNo Solution. \n");
}
return 0;
}
4.
#include <stdio.h>
int main()
{
char ch;
return 0;
}
EXPERIMENT-5
Working with Loops/Iterations
List of Lab Activities
1.
#include<stdio.h>
#include<math.h>
main()
{
int num,i,j,temp1,temp2,sum=0;;
printf("Enter a number to know all armstrong number between them\n");
scanf("%d",&num);
printf("Armstrong numbers are:\n");
for(i=1;i<=num;i++)
{
sum=0;
temp1=i;
temp2=i;
while(temp1>0)
{
j=temp1%10;
sum+=pow(j,3);
temp1=temp1/10;
}
if(sum==temp2)
{
printf("%d\n",sum);
}
}
}
2.
#include<stdio.h>
int main()
{
int a,b;
int mul,i;
printf("Enter first number: ");
scanf("%d",&a);
printf("Enter second number: ");
scanf("%d",&b);
mul=0;
for(i=1;i<=b;i++)
{
mul += a;
}
printf("Multiplication of %d and %d is: %d\n",a,b,mul);
return 0;
}
3.
#include<stdio.h>
int main()
{
int a, s;
printf("Enter value of a: ");
scanf("%d",&a);
s = 0;
while(a > 0)
{
s = s + (a%10);
a = a / 10;
}
printf("Sum of digits: %d",s);
return 0;
}
4.
#include <stdio.h>
int main()
{
double number, sum = 0, i;
printf("\n enter the number ");
scanf("%lf", &number);
for (i = 1; i <= number; i++)
{
sum = sum + (1 / i);
if (i == 1)
printf("\n 1 +");
else if (i == number)
printf(" (1 / %lf)", i);
else
printf(" (1 / %lf) + ", i);
}
printf("\n The sum of the given series is %.2lf", sum);
return 0;
}
5.
#include<stdio.h>
int main()
{
int rows, cols, i, j;
printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);
for(i=1; i<=rows; i++)
{
for(j=1; j<=cols; j++)
{
printf("%d", i);
}
printf("\n");
}
return 0;
}
EXPERIMENT-6
Functions, Recursion and Pointers
List of Lab Activities:
Write algorithm and C program, compile, execute and test the code using Linux C
compiler with suitable test cases.
1. Function main() gets a number and calls the following three functions
a. “void armstrong(int)” checks if the given number is a Armstrong number or
not.
b. “void coprime(int) reverses the given number and checks if the given
number and reversed number are coprime.
c.“int factorial(int) computes the factorial of the given number using recursion and
returns to main().
2. Function main() gets two numbers from the user and calls three functions in
the given order:
a. “int triangle_area(int base, int height)” returns the area of the right-angled
triangle to main().
b. “void swap(int *, int*)” swaps the two numbers using bitwise operator and
displays them.
c.“float* remainder (int a, int b)” returns the remainder of a/b to main().
1.
#include<stdio.h>
#include <math.h>
int main()
{
int number;
printf("Enter a two-digit Number:");
scanf("%d", &number);
printf("\nArmstrong check:\n");
armstrong(number);
printf("Coprime check:\n");
coprime(number);
printf("\nFactorial Calculation:");
factorial(number);
return 0;
}
originalNum = n1;
if ((int)result == n1)
{
printf("%d is an Armstrong number.\n\n", n1);
}
else
{
printf("%d is not an Armstrong number.\n\n", n1);
}
}
for(i=1;i<=orgn;i++)
{
if(orgn%i==0 && reverse%i==0)
{
hcf = i;
}
}
if(hcf == 1)
{
printf("%d and %d are CO-PRIME NUMBERS.\n", orgn, reverse);
}
else
{
printf("%d and %d are NOT CO-PRIME NUMBERS.\n", orgn, reverse);
}
}
int main()
{
int num1,num2;
printf("Enter First number:");
scanf("%d",&num1);
printf("Enter Second number:");
scanf("%d",&num2);
return 0;
}
EXPERIMENT-7
1D Arrays & Strings
List of Lab Activities:
Write algorithm and C program, compile, execute and test the
code using Linux C compiler with suitable test cases.
1. Find sum of all array elements using recursion.
2. Create an array ‘a1’ with ‘n’ elements. Insert an element in i th
position of ‘a1’ and also delete an element from jth position of
‘a1’.
3. Convert uppercase string to lowercase using for loop.
1.
#include <stdio.h>
#define MAX_SIZE 100
int sum(int arr[], int start, int len);
int main()
{
int arr[MAX_SIZE];
int N, i, sumofarray;
printf("Enter size of the array: ");
scanf("%d", &N);
printf("Enter elements in the array: ");
for(i=0; i<N; i++)
{
scanf("%d", &arr[i]);
}
sumofarray = sum(arr, 0, N);
printf("Sum of array elements: %d", sumofarray);
return 0;
}
int sum(int arr[], int start, int len)
{
if(start >= len)
return 0;
2.
#include <stdio.h>
int main()
{
int arr[100] = { 0 };
int i, x, pos, n = 10;
x = 50;
pos = 5;
n++;
arr[pos - 1] = x;
return 0;
}
3.
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
char str[MAX_SIZE];
int i;
printf("Enter any string: ");
gets(str);
for(i=0; str[i]!='\0'; i++)
{
if(str[i]>='A' && str[i]<='Z')
{
str[i] = str[i] + 32;
}
}
return 0;
}
EXPERIMENT-8
2D Arrays & Searching
1.
#include<stdio.h>
int main()
{
int i, j, rows, columns, a[10][10], Sum;
return 0;
}
2.
#include<stdio.h>
int main()
{
int Size, i, a[10];
int Even_Count = 0, Odd_Count = 0;
3.
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int arr[MAX_SIZE];
int size;
int i, j, temp;
return 0;
}
EXPERIMENT-9
Structure and Union
List of Lab Activities:
Write algorithm and C program, compile, execute and test the code using Linux C compiler
with suitable test cases.
1. Design a structure ‘product’ to store the details of the product purchased like
product name, price per unit, number of quantities purchased, and amount spent. Get the name,
price per unit, and number of quantities of the product purchased. Calculate the amount spent on
the product and then display all the details of the procured product using structure pointers.
2. Design a structure ‘student_record’ to store student details like name, SAP ID,
enrollment number, date of registration and data of birth. The element date of joining is defined
using another structure ‘date’ to store date details like day, month, and year. Get data of ‘n’
students and then print the entered values [Hint: Use concept of Nested structures and Array of
Structures].
3. Design a union ‘product’ to store the details of the product purchased like product
name, price per unit, number of quantities purchased, and amount spent. Get the name, price per
unit, and number of quantities of the product purchased. Calculate the amount spent on the
product and then display all the details of the procured product using union pointers.
3.
#include <stdio.h>
struct product
{
char name[50];
float pricePerUnit;
int noOfItems;
};
int main(){
struct product pro, *b;
b = &pro;
printf("\n-------------------------------\n");
printf("Displaying information..\n");
printf("-------------------------------\n\n");