[go: up one dir, main page]

0% found this document useful (0 votes)
41 views18 pages

Aditya 3

fafaffafaffafffffffffffffffffffffffffffffffsfasssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss

Uploaded by

saksham arora
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)
41 views18 pages

Aditya 3

fafaffafaffafffffffffffffffffffffffffffffffsfasssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss

Uploaded by

saksham arora
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/ 18

Lab Record

of

Programming for Problem Solving


(CSF101)

BACHELOR OF TECHNOLOGY
In

COMPUTER SCIENCE AND ENGINEERING

Session 2023-24

Submitted to: - Submitted by: -

Sumita Lamba Name: Aditya Kumar


Assistant Professor Roll No.: 230102198
School of Computing Sap ID: 1000019772
DIT University, Dehradun Class/Section: S

SCHOOL OF COMPUTING
DIT UNIVERSITY, DEHRADUN
(State Private University through State Legislature Act No. 10 of 2013 of Uttarakhand and approved by UGC)
Mussoorie Diversion Road, Dehradun, Uttarakhand - 248009, India.

August-December 2023
INDEX
CONDUCTION INSTRUCTOR’s
S.NO. NAME OF THE EXPERIMENTS
DATE SIGNATURE
Develop a program to calculate the sum of n array
1
elements in C.
Develop a program to calculate the average of n
2
array elements in C.
Develop a program to find the largest array
3
element in C.
Develop a program to print the sum of an array's
4
second and second-last element.
Develop a program to copy array elements to
5
another array.
Develop a program to count odd and even elements
6
of an array.
Develop a program to perform the addition of two
7
matrices.
Develop a program to perform the multiplication of
8
two matrices.
9 WAP to count simple interest using function.
10 WAP defines a function to add first n numbers.
11 WAP uses a global variable and a static variable.
WAP will scan a character string passed as an
12
argument and convert all lowercase characters into
their uppercase equivalents.
Build a function to check whether the number is
13 prime or not. If the number is prime, the function
returns value 1; otherwise, it returns 0.
Write a program to calculate nCr using a user-
14
defined function. nCr = n! / (r! * (n-r)!)
Create a function to swap the values of two
15
variables.
Write a function that takes two numbers as
16 parameters and returns the gcd of the two
numbers. Call the function in main().

Aditya Kumar 230102198 B. Tech (1st Semester, Section-i)


School of Computing
Practical-01
Objective: Develop a program to calculate the sum of n array elements in C.
Code:

#include <stdio.h>
int main() {
int arr1[5],sum=0;
for(int i=0;i<5;i++)
{
printf("enter the value in array");
scanf("%d",&arr1[i]);
sum=sum+arr1[i];
}
printf("sum is%d",sum);
return 0;
}
Output:

Aditya Kumar 230102198 B. Tech (1st Semester, Section-i)


School of Computing
Practical-02
Objective: Develop a program to calculate the average of n array elements in C.
Code:

#include<stdio.h>
int main()
{
int n,i;
float arr3[n],sum=0,avg;
printf("enter the size of array");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the value");
scanf("%f",&arr3[i]);
sum=sum+arr3[i];
avg=sum/n;
}
printf("average is%f",avg);
return 0;
}
Output:

Aditya Kumar 230102198 B. Tech (1st Semester, Section-i)


School of Computing
Practical-03
Objective: Develop a program to find the largest array element in C.
Code:

#include<stdio.h>
int main()
{
int arr1[5];
for(int n=0;n<5;n++)
{
printf("enter the array elements");
scanf("%d",&arr1[n]);
}
int max=arr1[0];
for(int i=1;i<5;i++)
{
if(arr1[i]>max)
max=arr1[i];
}
printf("largest element is%d",max);
}
Output:

Aditya Kumar 230102198 B. Tech (1st Semester, Section-i)


School of Computing
Practical-04
Objective: Develop a program to print the sum of an array's second and second-last
element.
Code:

#include<stdio.h>
int main()
{
int n;
printf("enter the size of array");
scanf("%d",&n);
int arr1[n];
for(int i=0;i<n;i++)
{
printf("enter the array elements");
scanf("%d",&arr1[i]);
}
int sum=arr1[1]+arr1[n-2];
printf("sum=%d",sum);
}
Output:

Aditya Kumar 230102198 B. Tech (1st Semester, Section-i)


School of Computing
Practical-05
Objective: Develop a program to copy array elements to another array.
Code:

#include<stdio.h>
int main(){
int arr1[3],arr2[3];
for(int x=0;x<3;x++)
{
printf("enter the array element");
scanf("%d",&arr1[x]);
arr2[x]=arr1[x];
}
printf("ARRAY 1:");
for(int i=0;i<3;i++)
{
printf(" %d",arr1[i]);}
printf("\nARRAY 2:");
for(int y=0;y<3;y++)
{
printf(" %d",arr2[y]);
}}
Output:

Aditya Kumar 230102198 B. Tech (1st Semester, Section-i)


School of Computing
Practical-06
Objective:Develop a program to count odd and even elements of an array.
Code:

#include<stdio.h>
int main()
{
int n;
printf("enter the size");
scanf("%d",&n);
int arr1[n],even=0,odd=0;
for(int i=0;i<n;i++)
{ printf("enter the array element");
scanf("%d",&arr1[i]);
if(i/2==0)
even=even+1;
else
odd=odd+1;
}
printf("number of even elements are%d\n",even);
printf("number of odd elements are%d\n",odd);}
Output:

Aditya Kumar 230102198 B. Tech (1st Semester, Section-i)


School of Computing
Practical-07
Objective:Develop a program to perform the addition of two matrices.
Code:

#include<stdio.h>
int main()
{
int arr1[2][2],arr2[2][2],arr3[2][2];
for(int x=0;x<2;x++)
for(int y=0;y<2;y++)
{
printf("enter the element of arr1");
scanf("%d",&arr1[x][y]);
printf("enter the element of arr2");
scanf("%d",&arr2[x][y]);
arr3[x][y]=arr1[x][y]+arr2[x][y];
}
printf("%d",arr3[2][2]);
}

Aditya Kumar 230102198 B. Tech (1st Semester, Section-i)


School of Computing
Practical-09
Objective:WAP to count simple interest using function.
Code:

#include <stdio.h>
float calculateSimpleInterest(float, float, float);
int main()
{
float p, r, t;
printf("Enter principal amount: ");
scanf("%f", &p);
printf("Enter rate of interest: ");
scanf("%f", &r);
printf("Enter time in years: ");
scanf("%f", &t);
float simpleInterest = calculateSimpleInterest(p, r, t);
printf("Simple Interest: %.2f\n", simpleInterest);
return 0;
}
float calculateSimpleInterest(float p, float r, float t)
{ return (p* r * t) / 100.0; }
Output:

Aditya Kumar 230102198 B. Tech (1st Semester, Section-i)


School of Computing
Practical-10
Objective:WAP defines a function to add first n numbers.
Output:

#include<stdio.h>
int add(int);
void main()
{
int n,sum=0;
printf("enter the limit");
scanf("%d",&n);
printf("sum is=%d",add(n));
return 0;
}
int add(int n)
{
int sum=0;
for(int i=0;i<=n;i++)
{
sum=sum+i;
}
return sum;
}
Output:

Aditya Kumar 230102198 B. Tech (1st Semester, Section-i)


School of Computing
Practical-11
Objective:WAP uses a global variable and a static variable.
Code:

#include <stdio.h>
int global_variable = 10;
void exampleFunction()
{
static int static_variable = 5;
printf("Global Variable: %d\n", global_variable);
printf("Static Variable: %d\n", static_variable);
global_variable += 10;
static_variable += 5;
printf("Updated Global Variable: %d\n", global_variable);
printf("Updated Static Variable: %d\n", static_variable);}
int main() {
printf("Function Call 1:\n");
exampleFunction();
printf("\nFunction Call 2:\n");
exampleFunction();
return 0;
}
Output:

Aditya Kumar 230102198 B. Tech (1st Semester, Section-i)


School of Computing
Practical-13
Objective:Build a function to check whether the number is prime or not. If the number
is prime, the function returns value 1; otherwise, it returns 0.
Code:

#include<stdio.h>
int prime(int);
void main()
{
int n;
printf("enter the number");
scanf("%d",&n);
if (prime(n))
{
printf("%d is a prime number.\n", n);
}
else
{
printf("%d is not a prime number.\n", n);
}
}
int prime(int n)
{
int factors=0;
for(int i=1;i<=n;i++)
{
if(n%i==0)
{

Aditya Kumar 230102198 B. Tech (1st Semester, Section-i)


School of Computing
factors=factors+1;
}
}
if(factors==2)
{
return 1;
}
else
{
return 0;
}
}
Output:

Aditya Kumar 230102198 B. Tech (1st Semester, Section-i)


School of Computing
Practical-14
Objective:Write a program to calculate nCr using a user-defined function. nCr = n! / (r!
* (n-r)!)
Code:

#include <stdio.h>
long long factorial(int num)
{
if (num == 0 || num == 1) {
return 1;
} else {
return num * factorial(num - 1);
}
}
long long nCr(int n, int r) {
if (n < r || n < 0 || r < 0) {
printf("Invalid input: n and r must be non-negative and n should be greater
than or equal to r\n");
return -1;
} else {
long long nCr_value = factorial(n) / (factorial(r) * factorial(n - r));
return nCr_value;
}
}
int main() {
int n, r;
printf("Enter the value of n: ");
scanf("%d", &n);

Aditya Kumar 230102198 B. Tech (1st Semester, Section-i)


School of Computing
printf("Enter the value of r: ");
scanf("%d", &r);
long long result = nCr(n, r);
if (result != -1) {
printf("%dC%d is %lld\n", n, r, result);
}
return 0;
}
Output:

Aditya Kumar 230102198 B. Tech (1st Semester, Section-i)


School of Computing
Practical-15
Objective:Create a function to swap the values of two variables.
Code:

#include<stdio.h>
void swap(int,int){
int a,b,temp;
temp=b;
b=a;
a=temp;}
int main(){
int a,b,temp;
printf("enter the value of a");
scanf("%d",&a);
printf("enter the value of b");
scanf("%d",&b);
printf("before swap a=%d\n",a);
printf("before swap b=%d\n",b);
swap(a,b);
printf("after swap a=%d\n",a);
printf("after swap b=%d\n",b);
}
Output:

Aditya Kumar 230102198 B. Tech (1st Semester, Section-i)


School of Computing
Practical-16
Objective:Write a function that takes two numbers as parameters and returns the gcd of
the two numbers. Call the function in main().
Code:

#include <stdio.h>
int calculateGCD(int a, int b)
{
while (b != 0) {
int temp = b;
b = a % b;
a = temp;}
return a;
}
int main() {
int num1, num2;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
int gcd = calculateGCD(num1, num2);
printf("GCD of %d and %d is: %d\n", num1, num2, gcd);
return 0;
}
Output:

Aditya Kumar 230102198 B. Tech (1st Semester, Section-i)


School of Computing

You might also like