Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
119 views

PPL Lab Assignment Code

The document contains code summaries for several C programming experiments involving basic input/output operations, conditional statements, loops, and functions. The experiments include: 1. Calculating mathematical operations and checking even/odd status of numbers using conditional statements. 2. Computing areas of shapes, swapping values with and without a third variable, and converting between Celsius, Fahrenheit and Kelvin scales. 3. Finding the largest of three numbers, checking for leap years, and solving quadratic equations using conditional branching. 4. Checking if a character is a vowel or consonant using switch-case statements. 5. Generating Armstrong numbers between 1 and a given positive number using loops.

Uploaded by

sun
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
119 views

PPL Lab Assignment Code

The document contains code summaries for several C programming experiments involving basic input/output operations, conditional statements, loops, and functions. The experiments include: 1. Calculating mathematical operations and checking even/odd status of numbers using conditional statements. 2. Computing areas of shapes, swapping values with and without a third variable, and converting between Celsius, Fahrenheit and Kelvin scales. 3. Finding the largest of three numbers, checking for leap years, and solving quadratic equations using conditional branching. 4. Checking if a character is a vowel or consonant using switch-case statements. 5. Generating Armstrong numbers between 1 and a given positive number using loops.

Uploaded by

sun
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Name-Shubham Kumar

Batch-B-38
SapID-500086656
Experiment 2
Basics of Problem Solving and Program Control Flows

List of Lab Activities


1. Given 2 numbers. Calculate sum, difference, multiplication and division.
2. Find if the given number is even or not.
3. Find the biggest of three numbers.
4. Multiply two numbers without using arithmetic multiplication operator (*).

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{

printf("c is the largest");


}
}

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 base,height,long_base, short_base, t_height ;

float diagonal1, diagonal2,radius, major_radius, minor_radius;

float S,R,P,TP;

float PI=3.142,E;

printf("Enter base and height of the Parallelogram: \n ");

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 %f %f", &long_base, &short_base, &t_height);

TP = 0.5 * (long_base + short_base) * t_height;

printf("Enter diagonals of the given rhombus: \n ");

scanf("%f%f", &diagonal1, &diagonal2);

R = 0.5 * diagonal1 * diagonal2;

printf("Enter radius of the sphere : \n");

scanf("%f", &radius);

S= 4*(22/7)*radius*radius;

printf(" Enter minor radius of Ellipse : ");

scanf("%f", &minor_radius);
printf("\n\n Enter major_radius of Ellipse : ");

scanf("%f", &major_radius);

E=PI*minor_radius*major_radius;

printf("Area of Parallelogram is:%f \n", P);

printf("\n Area of a trapezium = %f \n", TP);

printf("\n Area of rhombus is:%f \n",R);

printf("\n Surface area of sphere is:%f \n",S);

printf("\nArea of Ellipse= %f",E);

return 0;

2 (i).
#include<stdio.h>

int main()

int num1, num2, t=0;

printf("Enter two integers \n");

scanf("%d%d", &num1, &num2);

printf("Before Swapping \n First variable = %d \nSecond variable= %d \n",


num1, num2);
t = num1;

num1 = num2;

num2 = t;

printf("After Swapping \n First variable = %d \n Second variable =%d \n",


num1, num2);

return 0;

2 (ii).
#include <stdio.h>

int main()

int num1, num2;

printf("Enter two integers\n");

scanf("%d%d", &num1, &num2);

printf("Before Swapping \n First variable = %d\n Second variable = %d\n", num1, num2);

num1 = num1 + num2;

num2 = num1 - num2;

num1 = num1 - num2;

printf("After Swapping \n First variable = %d \nSecond 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{

printf("c is the largest");


}
}
2.
#include <stdio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);

if (year % 400 == 0)
{
printf("%d is a leap year.", year);
}

else if (year % 100 == 0) {


printf("%d is not 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;

printf("Enter any alphabet: ");


scanf("%c", &ch);
switch(ch)
{
case 'a':
printf("Vowel");
break;
case 'e':
printf("Vowel");
break;
case 'i':
printf("Vowel");
break;
case 'o':
printf("Vowel");
break;
case 'u':
printf("Vowel");
break;
case 'A':
printf("Vowel");
break;
case 'E':
printf("Vowel");
break;
case 'I':
printf("Vowel");
break;
case 'O':
printf("Vowel");
break;
case 'U':
printf("Vowel");
break;
default:
printf("Consonant");
}

return 0;
}

EXPERIMENT-5
Working with Loops/Iterations
List of Lab Activities

1. Given positive number ‘n’, generate all the Armstrong numbers


between 1 and n. [Hint: A 3-digit number (Ex. 153) is an Armstrong
number if the sum of cube of each digit (1 3 +5 3 +3 3 ) is equal to 153]
2. Multiple two given numbers without using the arithmetic binary
multiplication operator using for loop.
3. Find the sum of digits of a number using a while loop.
4. Given the value of ‘n’, find the sum of the series 1+ 1/2 + 1/3 + 1/4
+ 1/5 + …+ 1/n.
5. Print the given pattern using nested for loop.

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>

void armstrong(int n1);


void coprime(int n2);
int factorial(int n3);

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;
}

void armstrong(int n1)


{
int originalNum, remainder, n = 0;
float result = 0.0;

originalNum = n1;

for (originalNum = n1; originalNum != 0; ++n)


{
originalNum /= 10;
}
for (originalNum = n1; originalNum != 0; originalNum /= 10)
{
remainder = originalNum % 10;
result += pow(remainder, n);

if ((int)result == n1)
{
printf("%d is an Armstrong number.\n\n", n1);

}
else
{
printf("%d is not an Armstrong number.\n\n", n1);

}
}

void coprime(int n2)


{
int orgn,reverse=0,rem,i,hcf;
orgn=n2;
while (n2!=0)
{
rem=n2%10;
reverse=reverse*10+rem;
n2/=10;
}
printf("Original Number is: %d\n",orgn);
printf("Reversed Number: %d\n",reverse);

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 factorial(int n3)


{
int i,fact=1;
for(i=1;i<=n3;i++)
{
fact=fact*i;
}
printf("\nFactorial of %d is: %d",n3,fact);
return 0;
}
2.
#include<stdio.h>

int triangle_area(int base, int height);


void swap(int *, int*);
float* remainder (int a, int b);

int main()
{
int num1,num2;
printf("Enter First number:");
scanf("%d",&num1);
printf("Enter Second number:");
scanf("%d",&num2);

printf("\n\nArea of Right angled triangle having base %d and height %d


is-",num1,num2);
triangle_area(num1,num2);
printf("\n\nRemainder when %d is divided by %d is-",num1,num2);
remainder(num1, num2);
printf("\n\nSwapping the two numbers using bitwise operator-");
swap(&num1,&num2);

return 0;
}

int triangle_area(int base,int height)


{
float area;
area=0.5*base*height;
printf("\nArea of triangle is:%f",area);
return 0;
}
void swap(int*x,int*y)
{
printf("\nBefore swapping: %d,%d",*x,*y);
*x = *x ^ *y;
*y = *x ^ *y;
*x = *x ^ *y;
printf("\nAfter swapping: %d,%d",*x,*y);
}
float* remainder(int a, int b)
{
float rem;
rem=a%b;
printf("\nRemainder=%f",rem);
}

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;

return (arr[start] + sum(arr, start + 1, len));


}

2.
#include <stdio.h>

int main()
{
int arr[100] = { 0 };
int i, x, pos, n = 10;

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


arr[i] = i + 1;

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


printf("%d ", arr[i]);
printf("\n");

x = 50;

pos = 5;

n++;

for (i = n-1; i >= pos; i--)


arr[i] = arr[i - 1];

arr[pos - 1] = x;

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


printf("%d ", arr[i]);
printf("\n");

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;
}
}

printf("Lower case string: %s", str);

return 0;
}

EXPERIMENT-8
2D Arrays & Searching

List of Lab Activities:


1. Find the sum of rows and columns of matrix of given order.
2. Count how many even numbers are there in a given integer
array.
3. Store ‘n’ integers in an array in ascending or descending order.
Search for a number with binary search technique.

1.
#include<stdio.h>

int main()
{
int i, j, rows, columns, a[10][10], Sum;

printf("\n Please Enter Number of rows and columns : ");


scanf("%d %d", &i, &j);

printf("\n Please Enter the Matrix Elements \n");


for(rows = 0; rows < i; rows++)
{
for(columns = 0; columns < j; columns++)
{
scanf("%d", &a[rows][columns]);
}
}

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


{
Sum = 0;
for(columns = 0;columns < j; columns++)
{
Sum = Sum + a[rows][columns];
}
printf("\n The Sum of Elements of a Rows in a Matrix = %d", Sum );
}

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


{
Sum = 0;
for(columns = 0;columns < j; columns++)
{
Sum = Sum + a[columns][rows];
}
printf("\n The Sum of Elements of a Columns in a Matrix = %d", Sum );
}

return 0;
}

2.
#include<stdio.h>
int main()
{
int Size, i, a[10];
int Even_Count = 0, Odd_Count = 0;

printf("\n Please Enter the Size of an Array : ");


scanf("%d", &Size);

printf("\nPlease Enter the Array Elements\n");


for(i = 0; i < Size; i++)
{
scanf("%d", &a[i]);
}

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


{
if(a[i] % 2 == 0)
{
Even_Count++;
}
else
{
Odd_Count++;
}
}

printf("\n Total Number of Even Numbers in this Array = %d ", Even_Count);


printf("\n Total Number of Odd Numbers in this Array = %d ", Odd_Count);
return 0;
}

3.
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int arr[MAX_SIZE];
int size;
int i, j, temp;

printf("Enter size of array: ");


scanf("%d", &size);

printf("Enter elements in array: ");


for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

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


{

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


{

if(arr[i] > arr[j])


{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}

printf("\nElements of array in ascending order: ");


for(i=0; i<size; i++)
{
printf("%d\t", arr[i]);
}

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("Enter product name: ");


scanf("%s", &pro.name);
printf("Enter price of one unit: ");
scanf("%f", &pro.pricePerUnit);
printf("Enter number of item purchased: ");
scanf("%d", &pro.noOfItems);

printf("\n-------------------------------\n");
printf("Displaying information..\n");
printf("-------------------------------\n\n");

printf("Product name: %s\n", b->name);


float x = b->pricePerUnit;
printf("Price per unit: %f\n", b->pricePerUnit);
int y = b->noOfItems;
printf("Number of items: %d\n", y);
printf("\nTotal amount to be paid: %.2f", x*y);
}

You might also like