CPF L;AB MANNUAL
CPF L;AB MANNUAL
CPF L;AB MANNUAL
Lab Manual
D.E Semester: -I
Subject: - Computer Programming
Fundamentals ()
Name:
Branch:
Batch:
Enrollment No:
Computer Programming Fundamentals () Enrollment No:
CERTIFICATE
2
Write a C program that read two numbers from key board and
give their Addiction, Subtraction, multiplication and division.
3 .Write a C program to convert Celsius to Fahrenheit.
4 Write a c program to calculate compound interest.
5 Write a c program to find out the area and perimeter of a
circle.
6 .Write a c program to print the number entered by user is even
or odd.
7 Write a C program to print the largest number out of three
numbers givenby user. (Using nested if statement)
#include<stdio.h>Int
main()
{
Printf(“hello world”);
}
Output:
Hello world
Practical –2
2Write a C program that read two numbers from key board and give their Addiction, Subtraction,
multiplication and division.
#include <stdio.h>
int main()
{
int num1, num2;
int sum, sub, mult;
float div;
printf("Input any two numbers separated by comma : ");
scanf("%d,%d", &num1, &num2);
sum = num1 + num2;
sub = num1 - num2;
mult = num1 * num2;
div = (float)num1 / num2;
printf("Addition : %d\n", sum);
printf("Subtraction : %d\n", sub);
printf("Multiplication : %d\n", mult);
printf("Division : %f\n", div);
return 0;
}
Output:
Enter two numbers seprated by commas 10, 5Addition:15
Subtraction :5
Multiplication :50
Division:2.00
Practical –3
3. Write a C program to convert Celsius to Fahrenheit .
#include<stdio.h>
void main()
{
float celsius, fahrenheit;
printf("\n Enter Temp in Celsius : ");
scanf("%f", &celsius);
fahrenheit = (1.8 * celsius) + 32;
printf("\n Temperature in Fahrenheit : %.2f ", fahrenheit);
//getch();
}
Output:
` Enter temperature in Celsius: 10
Practical –4
4.Write a c program to calculate compound interest.
#include <stdio.h>
#include <math.h>
int main()
{
float principle, rate, time, CI;
printf("Enter principle (amount): ");
scanf("%f", &principle);
printf("Enter time: ");
scanf("%f", &time);
printf("Enter rate: ");
scanf("%f", &rate);
CI = principle* (pow((1 + rate / 100), time));
printf("Compound Interest = %f", CI);
return 0;
}
Output:
Enter Principle (amount): 1200Enter
time : 2
Enter rate : 5.4
Compound Interest = 1333.099243
Practical –5
#include <stdio.h>
#define PI 3.14f int
main()
{
Output:
Enter the radius of a circle: 2.34
Area of a circle: 17.193384
Perimeter of circle: 14.695200
Practical –6
}
Output: Enter an integer: 77 is odd
Practical –7
7. Write a C program to print the largest number out of three numbers givenby user. (Using
nested if statement)
#include <stdio.h>
#include<conio.h>
void main()
{
double n1, n2, n3;
printf("Enter three numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
if (n1 >= n2) {
if (n1 >= n3)
printf("%.2lf is the largest number.", n1);
else printf("%.2lf is the largest number.", n3);
}
else {
Output:
Enter three numbers: 50 80 120
120.00 is the largest number.
Practical –8
8. Write a C program to to obtain grade and percentage of a student marks of 3subject
according to given. Using if else ladder.
Percentage Grade
67% and up Distinction
From 60% up to 67% First Class
From 50% up to 60% Second Class
From 40% up to 50% Pass Class
Otherwise Fail
#include <stdio.h>
#include<conio.h>
void main()
{
int phy, chem, bio,total;
float per;
printf("Enter three subjects marks: \n");
scanf("%d%d%d", &phy, &chem, &bio);
total = phy+chem+bio;
printf("total marks:%d\n",total);
per = (phy + chem + bio) / 3.0;
printf("Percentage = %.2f\n", per); Output:
if (per >=66) Enter three subjects marks:80 90
printf("Distinction"); 88
else if (per <66 && total >=60) total marks:258
printf("first class"); Percentage = 86.00
else if (per <60 && total >=50) Distinction
printf("second class");
else if (per <50 && total >=40)
printf("pass class");
else
printf("fail");
getch ();
}
Practical –9
9. Write a C program to perform Addition, Subtraction, Multiplication andDivision
Using Switch case.
#include<stdio.h>
#include<conio.h>
void main()
{
int
a,b; int
op;
printf(" 1.Addition\n 2.Subtraction\n 3.Multiplication\n
4.Division\n");printf("Enter the values of a & b: ");
scanf("%d %d",&a,&b);
printf("Enter your Choice :
");scanf("%d",&op);
switch(op)
{
case 1 :
printf("Sum of %d and %d is :
%d",a,b,a+b); break;
case 2 :
printf("Difference of %d and %d is : %d",a,b,a-
b);break;
case 3 :
printf("Multiplication of %d and %d is :
%d",a,b,a*b); break;
case 4 : Output:
printf("Division of Two Numbers is %d : 1. Addition
",a/b);break;
2.Subtraction
3.Multiplication
default :
4.Division
printf(" Enter Your Correct
Choice."); break; Enter the values of a & b: 10 2010 20
}
Enter your Choice : 1 Sum
getch ();
of 10 and 20 is : 30
}
Practical –10
10.Write a C program to print Fibonacci series 1, 1, 2, 3, 5, . . . , N.
#include<stdio.h>
#include<conio.h>
void main() {
int i, n;
terms int t1 = 0, t2 = 1;
int nextTerm = t1 + t2;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: %d, %d, ", t1, t2);
for (i = 3; i <= n; ++i)
{
printf("%d, ",
nextTerm);t1 = t2;
t2 = nextTerm;
nextTerm = t1 +
t2;
}getch ();
}
Output:
Enter the number of terms: 15
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377,
Practical –11
12. Write a C program to find the factorial of a given number, where the numberi s entered by
user. (Using While Loop)
#include <stdio.h>
#include<conio.h>
void main()
{
int n,i,f;
f=i=1;
printf("Enter a Number to Find Factorial: ");
scanf("%d",&n);
while(i<=n)
{
f*=i;
i++;
}
printf("The Factorial of %d is : %d",n,f);
getch ();
}
Output:
Enter a Number to Find Factorial: 5
The Factorial of 5 is : 120
Practical –13
i. a)
#include <stdio.h>
int main() {
int i, j;
for (i = 1; i <= 5; ++i)
{ for (j = 1; j <=i; ++j)
{ printf("* ");
}
printf("\n");
}
return 0;
}
b)
#include
<stdio.h> int
main()
{
int i, j;
c)
#include<stdio.h>
int main()
{
int i, j;
int count = 1;
for (i = 0; i < 5; i++)
{
for (j = 0; j <= i; j++)
{
printf("%d ", count);
count = !count;
}
count = i % 2;
printf("\n");
}
return 0;
}
Practical –14
14.Write a C program to read an array of integers from user and sort that array in ascending
order.
#include <stdio.h>
void main (){
int num[20];
int i, j, a, n;
printf("enter number of elements in an array\n");
scanf("%d", &n);
printf("Enter the elements\n");
for (i = 0; i < n; ++i)
scanf("%d", &num[i]);
for (i = 0; i < n; ++i){
for (j = i + 1; j < n; ++j){
if (num[i] > num[j]){
a = num[i];
num[i] = num[j];
num[j] = a;
}
}
}
printf("The numbers in ascending order is:\n");
for (i = 0; i < n; ++i){
printf("%d\n", num[i]);
}
}
Output:
enter number of elements in an array
5
Enter the elements
78 85 14 78 12
The numbers in ascending order is:
12 14 78 78 85
Practical –15
15.Write a C program to read an array of integers and print them in reverse order.
#include <stdio.h>void
main (){
int num[20];int i,
j, a, n;
printf("enter number of elements in an array\n");
scanf("%d", &n);
printf("Enter the elements\n");for
(i = 0; i < n; ++i)
scanf("%d", &num[i]);for
(i = 0; i < n; ++i){
for (j = i + 1; j < n; ++j){ if
(num[i] > num[j]){
a = num[i]; num[i] =
num[j];num[j] = a;
}
}
}printf("The numbers in ascending order is\n:");
for (i = 0; i < n; ++i){
printf("%d", num[i]); }
}
Output:
Enter elements into the array:
array[0] :45
array[1] :14
array[2] :12
array[3] :78
array[4] :91
The elements from the array displayed in the reverse order are :
array[4] :91 array[3] :78 array[2] :12 array[1] :14 array[0] :45
Practical –16
16.Write a C program to count total words in a string & Write a C program to join two strings
Output:
Concatenated String: Computer Department!
cal – 23
Practical –17
17.Write a C program to find whether given string is palindrome or not using thestring library
function.
#include <stdio.h>
#include <string.h>
int main(){
char string1[20];
int i, length;
int flag = 0;
printf("Enter a string:");
scanf("%s", string1);
length = strlen(string1);
if (flag) {
printf("%s is not a palindrome", string1);
}
else {
printf("%s is a palindrome", string1);
}
return 0;
}
Output:
Enter a string:wow
wow is a palindrome
Enter a string:good
good is not a palindrome
Practical -18
18.Write a c program to find factorial of a given number using recursion
#include<stdio.h>
int fact(int);
int main()
{
int x,n;
printf(" Enter the Number to Find Factorial :");
scanf("%d",&n);
x=fact(n);
printf(" Factorial of %d is %d",n,x);
return 0;
}
int fact(int n)
{
if(n==0)
return(1);
return(n*fact(n-1));
}
Output:
Enter the Number to Find Factorial :10
Factorial of 10 is 3628800
Practical-19
19.Write a c program that used user defined function swap() and interchange the value of two variable.
#include <stdio.h>
void main ()
{
int num1, num2;
printf("Enter first number:\n");
scanf ("%d", &num1);
After Swapping
First number: 525
Second number: 789
Practical – 20
20.Write a C program to access elements of an array using pointer.
#include <stdio.h>
int main() {
int data[5];
printf("Enter elements: ");
for (int i = 0; i < 5; ++i)
scanf("%d", data + i);
printf("You entered: \n");
for (int i = 0; i < 5; ++i)
printf("%d\n", *(data + i));
return 0;
}
Output:
Enter elements: 1 2 3 4 5
entered:
1
2
3
4
5