0% found this document useful (0 votes)
7 views6 pages

Assignment 1 - Problem Solving and Programming (PSP)

Uploaded by

jeemains622
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)
7 views6 pages

Assignment 1 - Problem Solving and Programming (PSP)

Uploaded by

jeemains622
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
You are on page 1/ 6

Assignment 1: Problem Solving and Programming (PSP)

PART – A (5 Marks Each)

1. Define an algorithm. Write an algorithm and draw a flowchart to check whether the
number is divisible by 7.

Definition:​
An algorithm is a step-by-step procedure or set of instructions designed to perform a specific
task or solve a particular problem.

Algorithm:

1.​ Start
2.​ Input a number n
3.​ If n % 7 == 0, then​
Print “Number is divisible by 7”​
Else​
Print “Number is not divisible by 7”
4.​ Stop

Flowchart:

┌────────────┐
│ Start │
└─────┬──────┘

┌─────▼──────┐
│ Input n │
└─────┬──────┘

┌─────▼──────┐
│ n % 7 == 0?│
└───┬───┬────┘
│ │
Yes ──┘ └── No
│ │
▼ ▼
Print “Divisible” Print “Not Divisible”


┌──────┐
│ Stop │
└──────┘

2. Explain the basic structure of a C program with an example.

Structure of a C program:

1.​ Documentation Section – contains comments about the program.


2.​ Preprocessor Section – includes header files.
3.​ Global Declaration Section – variables declared outside main().
4.​ main() Function Section – execution starts from here.
5.​ Subprogram Section – user-defined functions.

Example:

#include <stdio.h> // Preprocessor section

int main() // main function


{
int a, b, sum; // variable declaration
printf("Enter two numbers: ");
scanf("%d%d", &a, &b);
sum = a + b;
printf("Sum = %d", sum);
return 0;
}

3. What are constants in C? Write 2 ways to define a constant.

Definition:​
A constant is a fixed value that does not change during program execution.

Two ways to define constants:

Using #define preprocessor directive:​


#define PI 3.14159

1.​
Using const keyword:​
const int MAX = 100;

2.​

4. Differentiate between while and do-while loops with suitable examples.


While Loop Do-While Loop

Condition is checked before executing the Condition is checked after executing the loop
loop body. body.

May execute 0 or more times. Executes at least once.

Example: Example:

```c ```c

int i=1; int i=1;

while(i<=5){ do{

printf("%d ",i); printf("%d ",i);

i++; i++;

} }while(i<=5);

``` ```

5. Define an operator. State any 2 types of operators with examples.

Definition:​
An operator is a symbol that performs an operation on one or more operands.

Types and Examples:

1.​ Arithmetic Operators:​


+ , - , * , / , %​
Example: sum = a + b;​

2.​ Relational Operators:​


== , != , > , < , >= , <=​
Example: if(a > b)​

6. Write a C program to read two numbers and display their sum and difference.
#include <stdio.h>
int main()
{
int a, b, sum, diff;
printf("Enter two numbers: ");
scanf("%d%d", &a, &b);
sum = a + b;
diff = a - b;
printf("Sum = %d\n", sum);
printf("Difference = %d\n", diff);
return 0;
}

PART – B (10 Marks Each)

7. Write a C program to check whether a given number is even or odd using an if-else
statement.
#include <stdio.h>
int main()
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
if(n % 2 == 0)
printf("%d is Even", n);
else
printf("%d is Odd", n);
return 0;
}

8. Write a C program to calculate the factorial of a number using a for loop.


#include <stdio.h>
int main()
{
int n, i;
long long fact = 1;
printf("Enter a number: ");
scanf("%d", &n);
for(i = 1; i <= n; i++)
fact *= i;
printf("Factorial of %d = %lld", n, fact);
return 0;
}

9. Write a C program to display the series 1 2 3 4 5 … n (where n is entered by the user).


#include <stdio.h>
int main()
{
int n, i;
printf("Enter the value of n: ");
scanf("%d", &n);
for(i = 1; i <= n; i++)
printf("%d ", i);
return 0;
}

10. Write a C program to check whether a number is prime or not using a for loop.
#include <stdio.h>
int main()
{
int n, i, count = 0;
printf("Enter a number: ");
scanf("%d", &n);

if(n <= 1)
printf("%d is not a prime number.", n);
else
{
for(i = 2; i <= n/2; i++)
{
if(n % i == 0)
{
count = 1;
break;
}
}
if(count == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}
return 0;
}

You might also like