Fundamentals of Computer Programming
Fundamentals of Computer Programming
FUNDAMENTALS OF COMPUTER
PROGRAMMING-LAB
COURSE CODE: DSC-3
SR.NO-71
SEMESTER-1st
BRANCH-ECE B3
Program # 1
Output :
Program # 2
Objective : write a program for basic arithmetic operations (addition, subtraction,
multiplication, division).
Program Code :
#include <stdio.h>
int main() {
float a,b,sum,sub,multiply,division;
printf("enter the value of a,b");
scanf("%f%f",&a,&b);
sum = a+b;
sub = a-b;
multiply = a*b;
division = a/b;
printf("the sum is %f\n",sum);
printf("the sub is %f\n",sub );
printf("the multiply is %f\n",multiply );
printf("the divisionis %f\n",division );
return 0;
}
Output :
Program # 3
Objective : Simple Interest Calculation: Calculating simple interest with given
principal, rate, and time.
#include<stdio.h>
int main()
{
float p,r,t,simple_interest;
printf("enter the value of p,r,t");
scanf("%f%f%f",&p,&r,&t);
simple_interest = p*r*t/100;
printf("simple_interest is %f",simple_interest);
return 0;
}
Output :
Program # 4
Objective: Temperature Conversion: Program to convert temperature from Celsius
to Fahrenheit and vice versa.
#include <stdio.h>
int main() {
float c ,f;
printf("enter the value of c ");
scanf("%f",&c);
f = (1.8*c)+32;
printf("temp in farenite is %f",f);
return 0;
}
Output :
Program # 5
Objective : Sum of Natural Numbers: Calculating the sum of natural numbers up to
a given limit using loops.
#include <stdio.h>
int main() {
int n, i, sum = 0;
Program # 6
else
printf("the given number is odd");
return 0;
}
Output :
Program # 7
Objective : Calculate Factorial using Loops: Finding the factorial of a number using
for loop.
#include<stdio.h>
int main()
{
int nbr, i, f = 1;
printf("Enter a number to calculate its factorial: ");
scanf("%d", &nbr);
Program # 8
Objective : Basic Array Operations: Program to define, initialize, and display a 1-D
array.
#include <stdio.h>
int main()
{
int arr[5];
for (int i = 0; i < 5; i++) {
arr[i] = i * i - 2 * i + 1;
}
printf("Elements of Array: ");
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output :
Program # 9
Objective : Array operations: To find smallest, largest, average, max and min from
list of n numbers
#include <stdio.h>
int main() {
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int sum = 0;
int smallest = arr[0];
int largest = arr[0];
for (i = 0; i < n; i++) {
sum += arr[i];
if (arr[i] < smallest) {
smallest = arr[i];
}
// Calculate average
float average = (float)sum / n;
// Display results
printf("Smallest: %d\n", smallest);
printf("Largest: %d\n", largest);
printf("Average: %.2f\n", average);
printf("Max: %d\n", largest);
printf("Min: %d\n", smallest);
return 0;
}
Output :
Program # 10
Objective : Swap two numbers: using and without using third variable.
#include<stdio.h>
int main() {
double a, b, temp;
printf("Enter a: ");
scanf("%lf", &a);
printf("Enter b: ");
scanf("%lf", &b);
temp = a;
a = b;
b = temp;
printf("\nAfter swapping, first numbe = %lf\n",a);
printf("After swapping, second number = %lf\n",b);
return 0;
}
Output :