0% found this document useful (0 votes)
2 views43 pages

CSE 1282 - Computer Programming Lab

Uploaded by

soumikce23ruet
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)
2 views43 pages

CSE 1282 - Computer Programming Lab

Uploaded by

soumikce23ruet
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

Lab Manual

Computer Programming Lab


CSE 1282
© Dept. of EEE, Varendra University

Prepared by

Shah Md. Moin Shiraj


Executive(Lab), Dept. of EEE
Varendra University
CSE 134

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.

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

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

Date of Experiment: June 25, 2019

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

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?

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

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

Date of Experiment: June 25, 2019

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

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.

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

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

Date of Experiment: June 25, 2019

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

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

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

8
Computer Programming Lab Manual

Questions
• How a variable can be declared?
• What is the difference between integer and floating type variable?

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

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

Date of Experiment: June 25, 2019

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

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)

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

11
CSE 134

printf("%d is a negative number \n", num);


else
printf("0 is neither positive nor negative");
}
Output

Questions
a) Why if and if..else statement is used?

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

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

Date of Experiment: June 25, 2019

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

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?

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

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

Date of Experiment: June 25, 2019

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

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

for (exp1; exp2; expr3)


{ statements; }
Where,
exp1 – variable initialization
for ( Example: i=0, j=2, k=3 )
exp2 – condition checking
( Example: i>5, j<3, k=3 )
exp3 – increment/decrement
( Example: ++i, j–, ++k )

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)

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

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?

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

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

Date of Experiment: June 25, 2019

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

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

Array declaration syntax: Integer array example:


data_type arr_name [arr_size];Array initialization int age [5];
syntax: int age[5]={0, 1, 2, 3, 4};
age[0]; /*0 is accessed*/
data_type arr_name [arr_size]=(value1, value2,
age[1]; /*1 is accessed*/
value3,….);Array accessing syntax: age[2]; /*2 is accessed*/
arr_name[index]; Character array example:
char str[10];
char str[10]={‘H’,‘a’,‘i’};
(or)
char str[0] = ‘H’;
char str[1] = ‘a’;
char str[2] = ‘i;

Program
#include <stdio.h>
int main()
{
int i, n;
float arr[100];
printf("Enter total number of elements(1 to 100): ");
scanf("%d", &n);

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

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?

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

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

Date of Experiment: June 25, 2019

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

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

Name (char type) msum (float) name (char type) stdio.h


rollno (int) msub1, rollno (int) conio.h
msub2, msub3, score(float) scanf() &printf() for
msub4, msub5 (float) formatted i/o.

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.*/

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

22
Computer Programming Lab Manual

printf ("\nRoll Number: ");


scanf("%d", &rollno);
printf ("\nEnter Marks in 5 Subjects:\n");
scanf("%f%f%f%f%f", &msub1, &msub2, &msub3, &msub4, &msub5);
msum=msub1+msub2+msub3+msub4+msub5; score = msum/500*100;
printf("\nName of Student: %s", name);
printf("\nRoll Number: %d", rollno);
printf ("\nPercentage Score Secured: %2.2f%c", score,'%');
return 0;
}
Output
Enter Name of Student: Shree HariKoirala
Roll Number: 522
Enter Marks in 5 Subjects:
45.5
50
63
76
62.5
Name of Student: Shree HariKoirala
Roll Number: 522
Percentage Score Secured: 59.40%
Discussion
In this second lab of C Programming, based on the focused objective(s) to understand about C
data types with formatted input/output functions, the additional lab exercises made me more confident
towards the fulfillment of the objectives.
Questions
a) What is algorithm?
b) what is flowchart?

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

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

Date of Experiment: June 25, 2019

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

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?

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

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

Date of Experiment: June 25, 2019

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

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;

printf("Enter the number of terms\n");


scanf("%d", &n);

printf("First %d terms of Fibonacci series are:\n", n);

for (c = 0; c < n; c++)


{
if (c <= 1)
next = c;
else
{
next = first + second;
first = second;
second = next;
}
printf("%d\n", next);
}

return 0;
}

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

27
CSE 134

Output

Questions
a) What do you mean about Fibonacci number?
b) How to calculate Fibonacci series?

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

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

Date of Experiment: June 25, 2019

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

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;

printf("Enter the number of terms\n");


scanf("%d", &n);

printf("First %d terms of Fibonacci series are:\n", n);

for (c = 0; c < n; c++)


{
if (c <= 1)
next = c;
else
{
next = first + second;
first = second;
second = next;
}
printf("%d\n", next);
}

return 0;
}

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

30
Computer Programming Lab Manual

Output

Questions
a) What do you mean about Fibonacci number?
b) How to calculate Fibonacci series?

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

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

Date of Experiment: June 25, 2019

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

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;

printf("Enter a year to check if it's a leap year\n");


scanf("%d", &year);

if (year%400 == 0) // Exactly divisible by 400, e.g., 1600, 2000


printf("%d is a leap year.\n", year);
else if (year%100 == 0) // Exactly divisible by 100 and not by
400, e.g., 1900, 2100
printf("%d isn't a leap year.\n", year);
else if (year%4 == 0) // Exactly divisible by 4 and neither by 100
nor 400, e.g., 2016, 2020
printf("%d is a leap year.\n", year);
else // Not divisible by 4 or 100 or 400, e.g., 2017, 2018, 2019
printf("%d isn't a leap year.\n", year);

return 0;
}

Output

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

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?

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

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

Date of Experiment: June 25, 2019

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

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.

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

36
Computer Programming Lab Manual

Program
#include <stdio.h>

int main()
{
int c, n, f = 1;

printf("Enter a number to calculate its factorial\n");


scanf("%d", &n);
for (c = 1; c <= n; c++)
f = f * c;

printf("Factorial of %d = %d\n", n, f);

return 0;
}

Output

Questions
a) What do you know about factorial?
b) How to calculate a factorial number?

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

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

Date of Experiment: June 25, 2019

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

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("Enter the number of rows in pyramid of stars to print\n");


scanf("%d", &n);

for (row = 1; row <= n; row++) // Loop to print rows


{
for (c = 1; c <= n-row; c++) // Loop to print spaces in a row
printf(" ");

for (c = 1; c <= 2*row - 1; c++) // Loop to print stars in a row


printf("*");

printf("\n");
}

return 0;
}

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

39
CSE 134

Example 2:
#include <stdio.h>
int main()
{
int n, c, k;

printf("Enter number of rows\n");


scanf("%d", &n);

for (k = 1; k <= n; k++)


{
for (c = 1; c <= n-k; c++)
printf(" ");

for (c = 1; c <= 2*k-1; c++)


printf("*");

printf("\n");
}

for (k = 1; k <= n - 1; k++)


{
for (c = 1; c <= k; c++)
printf(" ");

for (c = 1 ; c <= 2*(n-k)-1; c++)


printf("*");

printf("\n");
}

return 0;

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

40
Computer Programming Lab Manual

Output
Example 1:

Example 2:

Questions
a) What is pattern?

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

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

Date of Experiment: June 25, 2019

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

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?

Computer Programming Lab


Power Elect ronics Lab
Dept. of EEE, Varendra University.
Dept . of EEE, Varendra Univerit y

43

You might also like