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

Week 2

The document outlines three C programming tasks: checking if a number is even or odd, determining if a year is a leap year, and calculating the factorial of a number. Each task includes a problem statement, aim, software requirements, algorithm, source code, and example outputs. The programs utilize conditional statements and loops to achieve their respective functionalities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Week 2

The document outlines three C programming tasks: checking if a number is even or odd, determining if a year is a leap year, and calculating the factorial of a number. Each task includes a problem statement, aim, software requirements, algorithm, source code, and example outputs. The programs utilize conditional statements and loops to achieve their respective functionalities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

WEEK 2

Problem Statement:

(A) Write a Program which determines the Conditional Statements Using If, If-
else, and Nested If –else Statements Weather the given Condition is
True/False Statements?

Aim:

Write a C Program to check the given number is Even or Odd.

Software Requirements:

Operating system: Windows 11

Dev C++ 5.6

Compiler Version: TDM-GCC 4.8.1, 64-bit debug.

Description:

An even number is an integer that is exactly divisible by 2. For example: 0, 2, 4,


6,8,10…
An odd number is an integer that is not exactly divisible by 2. For example: 1,
3, 5, 7,9,11…

Algorithm:

Step 1: Start.

Step 2: Declare the Variable.

Step 3: Enter the number.

Step 4: Read the values.

Step 5: Compute the code part.

Step 6: Display the given value is Even or Odd.

Flow Chart:

1
Source Code:

#include <stdio.h>
#include<conio.h>
int main()
{
int n;

printf("Enter an integer:\n");
scanf("%d", &n);

if (n%2 == 0)
printf("Even\n");
else
printf("Odd\n");

return 0;
}

Save the file: Even and Odd.c

Compiler (F9): Click on F9 to compile the program which helps us to find the
errors so that the file extends to Even and Odd.exe file name.

Run(F10): After compilation of the program click on F10 to run the program
and gives output.

Output 1:

Enter an integer:
2
Even

--------------------------------
Process exited after 2.009 seconds with return value 0
Press any key to continue . . .

Output 2:

Enter an integer:
3
Odd

2
--------------------------------
Process exited after 4.185 seconds with return value 0
Press any key to continue . . .

3
(B)Write a Program which determines the Conditional Statements Using If, If-
else, and Nested If –else Statements Weather the given Condition is
True/False Statements?

Aim:

Write a C Program to check the given year is Leap year or not.

Software Requirements:

Operating system: Windows 11

Dev C++ 5.6

Compiler Version: TDM-GCC 4.8.1, 64-bit debug.

Description:

 A leap year is a year, which is different than a normal year having 366 days
instead of 365.
 A leap year comes once in four years, in which February month has 29 days.
With this additional day in February, a year becomes a Leap year.
 Some leap years examples are - 1600, 1988, 1992, 1996, and 2000.
 Although 1700, 1800, and 1900 are century years, not leap years.
 Below conditions are used to check that year is a leap year or not.
i) Year must be divisible by 4
ii) Year is divisible by 400 and not divisible by 100.

Algorithm:

Step 1: Start

Step 2: Take a year as input.

Step 3: Check whether a given year is divisible by 400.

Step 4: Check whether a given year is divisible by 100.

Step 5: Check whether a given year is divisible by 4.

Step 6: If the condition at step 3 and 5 becomes true, then the year is a leap
year.

Step 7: If the condition at step 4 becomes true, then the year is not a leap year.

4
Step 8: Stop.

Flow Chart:

Source Code:

#include<stdio.h>
#include<conio.h>
int main()
{
int year;

printf("Enter a year \n");


scanf("%d", &year);
if ((year % 400) == 0)
printf("%d is a leap year \n", year);
else if ((year % 100) == 0)
printf("%d is a not leap year \n", year);
else if ((year % 4) == 0)
printf("%d is a leap year \n", year);
else
printf("%d is not a leap year \n", year);
return 0;
}

Save the file: leap year.c

Compiler (F9): Click on F9 to compile the program which helps us to find the
errors so that the file extends to leap year.exe file name.

Run(F10): After compilation of the program click on F10 to run the program
and gives output

Output 1:

Enter a year
2000
2000 is a leap year

--------------------------------
Process exited after 3.458 seconds with return value 0
Press any key to continue . . .

Output 2:

5
Enter a year
2014
2014 is not a leap year

--------------------------------
Process exited after 3.181 seconds with return value 0
Press any key to continue . . .

C) Aim: Write a C Program to find Factorial of a Number

Software Requirements:

Operating system: Windows 11

Dev C++ 5.6

Compiler Version: TDM-GCC 4.8.1, 64-bit debug.

Description:

Factorial of n is the product of all positive descending integers. Factorial of n is


denoted by n!. For example:

1. 5! = 5*4*3*2*1 = 120
2. 3! = 3*2*1 = 6
Here, 5! is pronounced as "5 factorial", it is also called "5 bang" or "5 shriek".

The factorial is normally used in Combinations and Permutations


(mathematics).

Algorithm:

Step 1: Start

Step 2: Declare the variables which are used

Step 3: Input the value

Step 4: Read the given fact value

Step 5: Compute the fact part

Step 6: Display the given factorial value on the output screen

6
Step 7: Stop

Flow Chart:

Source Code:

#include <stdio.h>
#include<conio.h>
int main()
{
int i, n, fact = 1;

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


scanf("%d", &n);
for (i=1;i<=n;i++)
fact = fact * i;
printf("Factorial of %d = %d\n", n, fact);
return 0;
}
Save the file: Fact.c

Compiler (F9): Click on F9 to compile the program which helps us to find the
errors so that the file extends to Fact.exe file name.

Run(F10): After compilation of the program click on F10 to run the program
and gives output

Output:

Enter a number to calculate its factorial:


5
Factorial of 5 = 120

--------------------------------
Process exited after 3.55 seconds with return value 0
Press any key to continue . . .

You might also like