CPF L;AB MANNUAL

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Computer Programming Fundamentals () Enrollment No:

Apollo Institute of Engineering &


Technology

Lab Manual
D.E Semester: -I
Subject: - Computer Programming
Fundamentals ()

Name:
Branch:

Batch:
Enrollment No:
Computer Programming Fundamentals () Enrollment No:

CERTIFICATE

This is to certify that Mr./Ms.___________________________


Enrolment No.______________________ Semester_________

Branch_______________________ has satisfactorily completed


his/her term work in Course of “Computer Programming
Fundamentals” with subject code( ) for the academic term
2024-2025.

Staff In-charge Date of Submission Head of Department


APOLLO INSTITUTE OF ENGINEERING &
TECHNOLOGY
D.E Semester: - 1ST
Academic Term: 2024 – 25
Subject: - Software Engineering (3150711)
INDEX
Sr. No. Aim Page Date Sign
No.
1 Write a C program to print ― Hello World.

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)

8 Write a C program to to obtain grade and percentage of a


student marks of 3subject according to given. Using if else
ladder.
9 Write a C program to perform Addition, Subtraction,
Multiplication andDivision Using Switch case.
10 Write a C program to print Fibonacci series 1, 1, 2, 3, 5, . . . ,
N.
11 Write a C program to display primenumber among 1 to 100.
12 Write a C program to find the factorial of a given number,
where the numberi s entered by user. (Using While Loop)
13 Write a program to print following Patterns.
14 Write a C program to read an array of integers from user and
sort that array in ascending order.
15 Write a C program to read an array of integers and print them
in reverse order.
16 .Write a C program to count total words in a string & Write a
C program to join two strings
17 Write a C program to find whether given string is palindrome
or not using the string library function.
18 Write a c program to find factorial of a given number using
recursion
19 Write a c program that used user defined function swap() and
interchange the value of two variable.
20 Write a C program to access elements of an array using
pointer.
Practical – 4

1, Write a C program to print ― Hello World.

#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

10.00 Celsius = 50.00 Fahrenheit

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

5.Write a c program to find out the area and perimeter of a circle.

#include <stdio.h>
#define PI 3.14f int
main()
{

float rad,area, perm; printf("Enter


radius of circle: ");scanf("%f",&rad);
area=PI*rad*rad; perm=2*PI*rad;
printf("Area of circle: %f \nPerimeter of circle: %f\n",area,perm);return 0;
}

Output:
Enter the radius of a circle: 2.34
Area of a circle: 17.193384
Perimeter of circle: 14.695200

Practical –6

6.Write a c program to print the number entered by user is even or odd.


#include<stdio.h>Int
main()
{
Int num;
Printf(“enter integer number:”)
Scanf(“%d”, &num);
If(num % 2 == 0)
Printf|(“%d is even.”, num);
Else
Printf(“%d is odd.”,num);
Return 0;

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

if (n2 >= n3)


printf("%.2lf is the largest number.", n2);
else printf("%.2lf is the largest number.", n3);
}
getch ();
}

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

11.Write a C program to display prime number among 1 to 100.


#include<conio.h>
void main()
{
Int i,j;
for(i=1; i<=100; i++)
{
For(j=2; j<I; j++)
{
If(i%j==0)
} Break;
If(i==j)
Printf(“%d”,i);
}getch ();
}
Output:
1,2,3,5,7,11,13,17,19,23,29,31,37,41,
43,47,53,59,61,67,71,73,79,83,89,97,
Practical –12

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

a. Write a program to print following Patterns.


* 12345 1
** 1234 01
a *** b 123 c 101
. **** . 12 0101
***** 1 .10101

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;

for (i = 5; i >= 1; --i)


{
for (j = 1; j <= i; ++j)
{
printf("%d ", j);
}
printf("\n");
}
return 0;
}

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

(A)Write a C program to count total words in a string


#include <stdio.h> #define MAX_SIZE 100
int main()
{
char str[MAX_SIZE];int i,
words;
/* Input string from user */
printf("Enter any string: ");
gets(str);
i = 0;
words = 1;
/* Runs a loop till end of string */
while(str[i] != '\0')
{
/* If the current character(str[i]) is white space */
if(str[i]==' ' || str[i]=='\n' || str[i]=='\t')
{
words++;
}
i++;
}
printf("Total number of words = %d", words);
return 0;
}
Output:
Enter any string: Basic Computer ProgrammingTotal number
of words = 3

(B)Write a C program to join two strings.


#include <stdio.h>
#include <string.h>
int main()
{
char destination[] = "Computer ";char
source[] = "Department!";
strcat(destination,source);
printf("Concatenated String: %s\n", destination);
return 0;
}

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

for(i=0;i < length ;i++){


if(string1[i] != string1[length-i-1]){
flag = 1;
break;
}
}

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 swap (int *n1, int *n2)


{
int temp;
temp = *n2;
*n2 = *n1;
*n1 = temp;
}

void main ()
{
int num1, num2;
printf("Enter first number:\n");
scanf ("%d", &num1);

printf("Enter second number:\n");


scanf ("%d", &num2);

/* Printing the use input before swapping. */


printf("\nBefore Swapping\n");
printf("First number: %d\n", num1);
printf("Second number: %d\n\n", num2);

/* Swapping the variables. */


swap (&num1, &num2);

/* Printing the variables after the swapping. */


printf("\nAfter Swapping\n");
printf("First number: %d\n", num1);
printf("Second number: %d\n\n", num2);
}
Output:
Enter first number:
789
Enter second number:
525
Before Swapping
First number: 789
Second number: 525

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy