CSE 1282 - Computer Programming Lab
CSE 1282 - Computer Programming Lab
Prepared by
List of Experiments
1. Introduction to Code block and C Programming
2. Write a program that will print the addition of two integer/floating numbers
3. Write a program that will print the area of a circle
4. Write a program that will check whether a number is positive or negative
5. Write a program that will print the grade of marks
6. Objective
To know about using conditions in C.
7. Write a program that print the sum of natural numbers
8. Display Largest Element of an array
9. Write a program to take input of name, roll no and marks obtained by a student in 5 subjects
each
10. have its 100 full marks and display the name, roll no with percentage score secured.
11. Write a program to print whether a given number is even or odd.
12. Display Fibonacci Series using C Programming.
13. Write a program that will check whether a year is leap year or not.
14. Write a program that will check whether a year is leap year or not.
15. Write a C program to find the factorial value of a number
16. Write a program to print a pattern.
17. Write a program that will convert a decimal number into binary.
2
Computer Programming Lab Manual
Varendra University
Department of Electrical and Electronic Engineering
Lab Report
Course Title: Computer Programming Lab
Course Code: CSE 1282
Experiment 1
Submitted by Submitted to
Md. Palash Kibria Jannatul Afroz Akhi
Dept. of EEE Lecturer
Varendra University Dept. of EEE
Roll: 123456789 Varendra University
3
CSE 134
Experiment No.: 01
Experiment name
Introduction to Code block and C Programming
Objective
• To know about code block software and C Programming.
• Theory :
• All valid C programs must contain the main () function. The code execution begins from the
start of the main() function.
• The printf() is a library function to send formatted output to the screen. The function prints the
string inside quotations.
• To use printf() in our program, we need to include stdio.h header file using #inclue
<stdio.h> statement.
• The return 0; statement inside the main() function is the "Exit status" of the program.
Program:
#include<stdio.h>
main()
{
printf("my name is nurul");
return 0;
}
Output
Questions
a) What is header file?
b) What is printf and scanf?
4
Computer Programming Lab Manual
Varendra University
Department of Electrical and Electronic Engineering
Lab Report
Course Title: Computer Programming Lab
Course Code: CSE 1282
Experiment 2
Submitted by Submitted to
Md. Palash Kibria Jannatul Afroz Akhi
Dept. of EEE Lecturer
Varendra University Dept. of EEE
Roll: 123456789 Varendra University
5
CSE 134
Experiment No.: 02
Experiment name
Write a program that will print the addition of two integer/floating numbers
Objective
• To know about data types and variables.
Theory
A variable is a container (storage area) to hold data. To indicate the storage area, each variable
should be given a unique name. Variable names are just the symbolic representation of a memory
location. In C programming, data types are declarations for variables. This determines the type and size
of data associated with variables
Program
#include<stdio.h>
main()
{
int a;
int b;
int c;
printf("enter two integer number");
scanf("%d %d",&a,&b);
c=a+b;
printf("the result is %d",c);
return 0;
}
Output
Questions
a) What is variable?
b) Discuss about different types of data types.
6
Computer Programming Lab Manual
Varendra University
Department of Electrical and Electronic Engineering
Lab Report
Course Title: Computer Programming Lab
Course Code: CSE 1282
Experiment 3
Submitted by Submitted to
Md. Palash Kibria Jannatul Afroz Akhi
Dept. of EEE Lecturer
Varendra University Dept. of EEE
Roll: 123456789 Varendra University
7
CSE 134
Experiment No.: 03
Experiment name
Write a program that will print the area of a circle
Objective
• To know how to declare a variable.
Theory
C variable is a named location in a memory where a program can manipulate the data. This
location is used to hold the value of the variable. The value of the C variable may get change in the
program variable might be belonging to any of the data type like int, float, char etc.
3.4.1 RULES FOR NAMING C VARIABLE
1. Variable name must begin with letter or underscore.
2. Variables are case sensitive
3. They can be constructed with digits, letters.
4. No special symbols are allowed other than underscore.
5. sum, height, _value are some examples for variable name
3.4.2 DECLARING & INITIALIZING C VARIABLE
• Variables should be declared in the C program before to use.
• Memory space is not allocated for a variable while declaration. It happens only on variable
definition.
• Variable initialization means assigning a value to the variable.
Program
#include<stdio.h>
main()
{
float a,c;
printf("enter the value of a ");
scanf("%f",&a);
c=3.1416*a*a;
printf("the area of the triangle is=%f",c);
return 0;
}
Output
8
Computer Programming Lab Manual
Questions
• How a variable can be declared?
• What is the difference between integer and floating type variable?
9
CSE 134
Varendra University
Department of Electrical and Electronic Engineering
Lab Report
Course Title: Computer Programming Lab
Course Code: CSE 1282
Experiment 4
Submitted by Submitted to
Md. Palash Kibria Jannatul Afroz Akhi
Dept. of EEE Lecturer
Varendra University Dept. of EEE
Roll: 123456789 Varendra University
10
Computer Programming Lab Manual
Experiment No.: 04
Experiment name
Write a program that will check whether a number is positive or negative
Objective
To know about decision making and branching.
Theory
In decision control statements (if-else and nested if), group of statements are executed when
condition is true. If condition is false, then else part statements are executed.
There are 3 types of decision making control statements in C language. They are,
• if statements
• if else statements
• nested if statements
Syntax:
if (condition)
if { Statements;} Description:
In these types of statements, if condition is true, then respective block of code is
executed.
Syntax:
if (condition)
{ Statement1; Statement2;}
if…else else
{ Statement3; Statement4;} Description:
In these type of statements, group of statements are executed when condition is
true. If condition is false, then else part statements are executed.
Syntax:
if (condition1){ Statement1; }
else_if(condition2)
nested if { Statement2; }
else Statement 3;Description:
If condition 1 is false, then condition 2 is checked and statements are executed if it
is true. If condition 2 also gets failure, then else part is executed.
Program
#include <stdio.h>
void main()
{
int num;
printf("Enter a number: \n");
scanf("%d", &num);
if (num > 0)
printf("%d is a positive number \n", num);
else if (num < 0)
11
CSE 134
Questions
a) Why if and if..else statement is used?
12
Computer Programming Lab Manual
Varendra University
Department of Electrical and Electronic Engineering
Lab Report
Course Title: Computer Programming Lab
Course Code: CSE 1282
Experiment 5
Submitted by Submitted to
Md. Palash Kibria Jannatul Afroz Akhi
Dept. of EEE Lecturer
Varendra University Dept. of EEE
Roll: 123456789 Varendra University
13
CSE 134
Experiment No.: 05
Experiment name
Write a program that will print the grade of marks
Objective
To know about using conditions in C.
Program
#include <stdio.h>
int main(void)
{
int num;
printf("Enter your mark ");
scanf("%d",&num);
printf(" You entered %d", num); // printing outputs
if(num >= 80)
{
printf(" You got A+ grade"); // printing outputs
}
else if ( num >=60)
{ // Note the space between else & if
printf(" You got B grade");
}
else if ( num >40)
{
printf(" You got D grade");
}
else if ( num < 40)
{
printf(" You Failed in this exam");
}
}
Output
Questions
• What are the decision control statements in C?
14
Computer Programming Lab Manual
Varendra University
Department of Electrical and Electronic Engineering
Lab Report
Course Title: Computer Programming Lab
Course Code: CSE 1282
Experiment 6
Submitted by Submitted to
Md. Palash Kibria Jannatul Afroz Akhi
Dept. of EEE Lecturer
Varendra University Dept. of EEE
Roll: 123456789 Varendra University
15
CSE 134
Experiment No.: 06
Experiment name
Write a program that print the sum of natural numbers
Objective
To know about looping in C.
Theory
Loop control statements in C are used to perform looping operations until the given condition is
true. Control comes out of the loop statements once condition becomes false.
6.4.1 TYPES OF LOOP CONTROL STATEMENTS IN C:
There are 3 types of loop control statements in C language. They are,
• for
• while
• do-while
Syntax for each C loop control statements are given in below table with description.
Loop Name Syntax
while (condition)
{ statements; }
while
where,
condition might be a>5, i<10
do { statements; }
while (condition);
do while
where,
condition might be a>5, i<10
Program
#include <stdio.h>
int main()
{
int n, i, sum = 0;
printf("Enter a positive integer: ");
scanf("%d",&n);
for(i=1; i <= n; ++i)
16
Computer Programming Lab Manual
{
sum += i; // sum = sum+i;
}
printf("Sum = %d",sum);
return 0;
}
Output
Questions
• What is the difference between while and do while loop?
17
CSE 134
Varendra University
Department of Electrical and Electronic Engineering
Lab Report
Course Title: Computer Programming Lab
Course Code: CSE 1282
Experiment 7
Submitted by Submitted to
Md. Palash Kibria Jannatul Afroz Akhi
Dept. of EEE Lecturer
Varendra University Dept. of EEE
Roll: 123456789 Varendra University
18
Computer Programming Lab Manual
Experiment No.: 07
Experiment name
Display Largest Element of an array
Objective
To know about array in C.
Theory
C Array is a collection of variables belongings to the same data type. You can store group of data
of same data type in an array. Array might be belonging to any of the data types. Array size must be a
constant value.
Always, Contiguous (adjacent) memory locations are used to store array elements in memory.
It is a best practice to initialize an array to zero or null while declaring, if we don’t assign any
values to array.
There are 2 types of C arrays. They are,
• One dimensional array
• Multi-dimensional array
✓ Two dimensional array
✓ Three dimensional array
✓ four-dimensional array etc…
7.4.1 ONE DIMENSIONAL ARRAY IN C:
Syntax: data-type arr_name[array_size];
Array declaration, initialization and accessing Example
Program
#include <stdio.h>
int main()
{
int i, n;
float arr[100];
printf("Enter total number of elements(1 to 100): ");
scanf("%d", &n);
19
CSE 134
printf("\n");
// Stores number entered by the user
for(i = 0; i < n; ++i)
{
printf("Enter Number %d: ", i+1);
scanf("%f", &arr[i]);
}
// Loop to store largest number to arr[0]
for(i = 1; i < n; ++i)
{
// Change < to > if you want to find the smallest element
if(arr[0] < arr[i])
arr[0] = arr[i];
}
printf("Largest element = %.2f", arr[0]);
return 0;
}
Output
Questions
a) What is array?
b) How array can be declared?
20
Computer Programming Lab Manual
Varendra University
Department of Electrical and Electronic Engineering
Lab Report
Course Title: Computer Programming Lab
Course Code: CSE 1282
Experiment 8
Submitted by Submitted to
Md. Palash Kibria Jannatul Afroz Akhi
Dept. of EEE Lecturer
Varendra University Dept. of EEE
Roll: 123456789 Varendra University
21
CSE 134
Experiment No.: 08
Experiment name
Write a program to take input of name, roll no and marks obtained by a student in 5 subjects each
have its 100 full marks and display the name, roll no with percentage score secured
Objective
To be familiar with different data types, Operators and Expressions in C.
Theory
Based on the problem, it is required to get the input of name, roll number and marks in 5 subjects
of a student. The program should display the name; roll number and percentage of marks secured
by that student as output. The input variables shall be: name, roll no, msub1, msub2, msub3, msub4,
msub5. We need to calculate percentage of marks obtained. So, the variable ‘score’ holds the
percentage to be displayed.
Percentage of marks obtained = (total marks on 5 subjects/ total full marks) × 100
Hence, msum = msub1 + msub2 + msub3 + msub4 + msub5; Score = (msum /500) × 100
Input variables Processing Output variables Necessary header
variables/calculations files /functions
/macros
Algorithm
[Link]
2. Define variables: name, rollno, msub1, msub2, msub3, msub4, msub5, msum, score
3. Take input from keyboard for all the input variables
4. Calculate the sum of marks of 5 subjects and also calculate the percentage score as: Msum =
msub1 + msub2 + msub3 + msub4 + msub5; Score = msum 500 × 100
5. Display the name, roll number and percentage score. 6. Stop
Program
#include<stdio.h>
#include<conio.h>
int main(void)
{
char name[20];
int rollno;
float msub1, msub2, msub3, msub4, msub5, msum, score;
printf("Enter Name of Student: ");
scanf("%[^\n]", name); /*can use scanf(“%s”,name) but it reads single word only.*/
22
Computer Programming Lab Manual
23
CSE 134
Varendra University
Department of Electrical and Electronic Engineering
Lab Report
Course Title: Computer Programming Lab
Course Code: CSE 1282
Experiment 9
Submitted by Submitted to
Md. Palash Kibria Jannatul Afroz Akhi
Dept. of EEE Lecturer
Varendra University Dept. of EEE
Roll: 123456789 Varendra University
24
Computer Programming Lab Manual
Experiment No.: 09
Experiment name
Write a program to print whether a given number is even or odd
Objective
To understand the programming knowledge using Decision Statements (if, if-else, if-else-if
ladder, switch and GOTO).
Theory
If a number is exactly divisible by 2 then its an even number, else it is an odd number.
Program
#include <stdio.h>
int main()
{
int num;
printf("Enter the number: ");
scanf(“%d”,&num);
if(num%2==0)
printf(“\n %d is even”, num);
else printf(“\n %d is odd”, num);
getch()
return 0;
}
Output
Enter the number: 6
6 is even
Questions
a) How even and odd could be define?
25
CSE 134
Varendra University
Department of Electrical and Electronic Engineering
Lab Report
Course Title: Computer Programming Lab
Course Code: CSE 1282
Experiment 10
Submitted by Submitted to
Md. Palash Kibria Jannatul Afroz Akhi
Dept. of EEE Lecturer
Varendra University Dept. of EEE
Roll: 123456789 Varendra University
26
Computer Programming Lab Manual
Experiment No.: 10
Experiment name
Display Fibonacci Series using C Programming
Objective
To know about Fibonacci series.
Theory
The Fibonacci sequence is a sequence where the next term is the sum of the previous two terms.
The first two terms of the Fibonacci sequence are 0 followed by 1. The Fibonacci sequence: 0, 1, 1, 2,
3, 5, 8, 13, 21.
Program
#include <stdio.h>
int main()
{
int n, first = 0, second = 1, next, c;
return 0;
}
27
CSE 134
Output
Questions
a) What do you mean about Fibonacci number?
b) How to calculate Fibonacci series?
28
Computer Programming Lab Manual
Varendra University
Department of Electrical and Electronic Engineering
Lab Report
Course Title: Computer Programming Lab
Course Code: CSE 1282
Experiment 11
Submitted by Submitted to
Md. Palash Kibria Jannatul Afroz Akhi
Dept. of EEE Lecturer
Varendra University Dept. of EEE
Roll: 123456789 Varendra University
29
CSE 134
Experiment No.: 11
Experiment name
Display Fibonacci Series using C Programming
Objective
To know about Fibonacci series.
Theory
The Fibonacci sequence is a sequence where the next term is the sum of the previous two terms.
The first two terms of the Fibonacci sequence are 0 followed by 1. The Fibonacci sequence: 0, 1, 1, 2,
3, 5, 8, 13, 21.
Program
#include <stdio.h>
int main()
{
int n, first = 0, second = 1, next, c;
return 0;
}
30
Computer Programming Lab Manual
Output
Questions
a) What do you mean about Fibonacci number?
b) How to calculate Fibonacci series?
31
CSE 134
Varendra University
Department of Electrical and Electronic Engineering
Lab Report
Course Title: Computer Programming Lab
Course Code: CSE 1282
Experiment 12
Submitted by Submitted to
Md. Palash Kibria Jannatul Afroz Akhi
Dept. of EEE Lecturer
Varendra University Dept. of EEE
Roll: 123456789 Varendra University
32
Computer Programming Lab Manual
Experiment No.: 12
Experiment name
Write a program that will check whether a year is leap year or not
Objective
To learn program to check leap year.
Theory
Leap year program in C to check if a year is a leap year or not, a leap year is one that has 366
days, and the extra day is in the month of February (29th February).
Condition or logic for a leap year: A year that is exactly divisible by four is a leap year, except
for a year that is exactly divisible by 100 but, the year is a leap year if it is exactly divisible by 400.
Read more about a Leap year. The program is based on the Gregorian Calendar.
Program
#include <stdio.h>
int main()
{
int year;
return 0;
}
Output
33
CSE 134
Questions
a) What is leap year?
b) How to check a year if it’s leap year or not?
c) what is the work of ‘%’ sign?
34
Computer Programming Lab Manual
Varendra University
Department of Electrical and Electronic Engineering
Lab Report
Course Title: Computer Programming Lab
Course Code: CSE 1282
Experiment 13
Submitted by Submitted to
Md. Palash Kibria Jannatul Afroz Akhi
Dept. of EEE Lecturer
Varendra University Dept. of EEE
Roll: 123456789 Varendra University
35
CSE 134
Experiment No.: 13
Experiment name
Write a C program to find the factorial value of a number
Objective
To find factorial using C programming.
Theory
Factorial is represented by '!', so five factorial is written as (5!), n factorial as (n!). Also, n! =
n*(n-1)*(n-2)*(n-3)...3.2.1 and zero factorial is defined as one, i.e., 0! = 1. From the flow chart, it would
be easy to find factorials.
36
Computer Programming Lab Manual
Program
#include <stdio.h>
int main()
{
int c, n, f = 1;
return 0;
}
Output
Questions
a) What do you know about factorial?
b) How to calculate a factorial number?
37
CSE 134
Varendra University
Department of Electrical and Electronic Engineering
Lab Report
Course Title: Computer Programming Lab
Course Code: CSE 1282
Experiment 14
Submitted by Submitted to
Md. Palash Kibria Jannatul Afroz Akhi
Dept. of EEE Lecturer
Varendra University Dept. of EEE
Roll: 123456789 Varendra University
38
Computer Programming Lab Manual
Experiment No.: 14
Experiment name
Write a program to print a pattern
Objective
To learn a program that will print a pattern
Theory
Pattern programs in C language, showing how to create various patterns of numbers and stars.
The programs require nested loops (a loop inside another loop). A design of numerals, stars, or
characters is a way of arranging these in some logical manner, or they may form a sequence. Some of
these are triangles that have particular importance in mathematics. Some patterns are symmetrical, while
others are not.
Program
Example 1:
#include <stdio.h>
int main()
{
int row, c, n;
printf("\n");
}
return 0;
}
39
CSE 134
Example 2:
#include <stdio.h>
int main()
{
int n, c, k;
printf("\n");
}
printf("\n");
}
return 0;
40
Computer Programming Lab Manual
Output
Example 1:
Example 2:
Questions
a) What is pattern?
41
CSE 134
Varendra University
Department of Electrical and Electronic Engineering
Lab Report
Course Title: Computer Programming Lab
Course Code: CSE 1282
Experiment 15
Submitted by Submitted to
Md. Palash Kibria Jannatul Afroz Akhi
Dept. of EEE Lecturer
Varendra University Dept. of EEE
Roll: 123456789 Varendra University
42
Computer Programming Lab Manual
Experiment No.: 15
Experiment name
Write a program that will convert a decimal number into binary
Objective
To learn decimal to binary conversion.
Theory
Decimal to binary in C to convert an integer from decimal number system (base-10) to binary
number system (base-2). The size of an integer is assumed to be 32 bits. We use the bitwise operator
"AND" to perform the desired task. We right shift the original number by 31, 30, 29, ..., 1, 0 bits using
a for loop and bitwise AND the number obtained with 1(one) if the result is 1, then that bit is one
otherwise zero (0).
Program
#include <stdio.h>
int main()
{
int n, c, k;
printf("Enter an integer in decimal number system\n");
scanf("%d", &n);
printf("%d in binary number system is:\n", n);
for (c = 31; c >= 0; c--)
{
k = n >> c;
if (k & 1)
printf("1");
else
printf("0");
}
printf("\n");
return 0;
}
Output
Questions
a) What is decimal number?
b) What is binary number?
43