5. Algorithm : To check whether a number is prime or not.
step1: start
step2: declare n, i
step3: set count=0
setp4: input n
setp5: for i=1 to n
if(n%i==0)
count++
step6: if(count==2)
then print prime number
step7: else
print not prime number
step9: end
5. Flowchart : To check whether a number is prime or not.
Start
Declare n, i
Set Count 0
Input n
for i 1 to n
False
Is n%i==0 ?
True
Count ++
True False
Is count = =2 ?
Prime number Not a Prime Number
number
Stop
5. C program to check whether a number is prime or not.
#include<stdio.h>
int main()
{
int n,i,count=0;
printf(“Enter the number\n”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
{
count++;
}
}
if(count==2)
{
printf(“%d is prime number”,n);
}
else
{
printf(“%d is not a prime number”,n);
}
return 0;
}
Output:
Enter a number
5
5 is prime number
Enter a number
9
9 is not a prime number
6. Algorithm : To Generate Fibonacci sequence
step1: start
step2: declare a, b, c, n, i
step3: set a=0 and b=1
step4: input n
step5: for i=0 to n
print a
c=a+b
a=b
b=c
step6: end
6. Flowchart : To Generate Fibonacci sequence
Start
Declare a, b, c, n, i
Set a 0 b 1
Input n
for i 0 to n
Print a
c a+b
ab
bc
Stop
6. C Program to Generate Fibonacci sequence
include<stdio.h>
int main()
{
int n,i,a=0,b=1,c;
printf(“Enter the limit:\n”);
scanf(“%d”,&n);
for(i=0;i<=n;i++)
{
printf(“%d\n”,a);
c=a+b;
a=b;
b=c;
}
return 0;
}
Ouput:
Enter the limit:
7
The Fibonacci sequence is:
0
1
1
2
3
5
8
13
7. Algorithm: To find even or odd number
step1: start
step2: declare n
step3: if(n%2==0)
then print even number
step4: else
print odd number
step6: end
7. C program to check whether a number is even or odd.
#include<stdio.h>
int main()
{
int n;
printf(“Enter the number: ”);
scanf(“%d”,&n);
if(n%2==0)
printf(“%d is even number”,n);
else
printf(“%d is odd number”,n);
return 0;
}
Output:
Enter the number: 6
6 is even number
Enter the number: 9
9 is odd number
7. Flowchart : To check whether a number is even or odd.
Start
Declare n
Input n
True False
Is n%2==0 ?
n is even number n is odd number
Stop
8. C Program to Exchange the values of two variables
#include<stdio.h>
int main()
{
int x, y, temp;
printf(“Enter two numbers:\n ”);
scanf(“%d%d”,&x,&y);
printf(“Before swapping x=%d y=%d\n”,x,y);
temp=x;
x=y;
y=temp;
printf(“After swapping x=%d y=%d”,x,y);
return 0;
}
Output:
Enter two numbers:
25
50
Before swapping x=25 y=50
After swapping x=50 y=25
8. Algorithm: To Exchange the values of two variables
step1: start
sept2: declare x, y, temp
step3: input value of x and y
step4: Assign temp=x
x=y
y=temp
step5: print x and y
step6: end
8. Flowchart to exchange the values of two variables
Start
Declare x , y, temp
Input x , y
temp x
xy
y temp
Print x & y
Stop
9. Flowchart to print numbers from 100 to 200 which are divisible by 7 and
display their sum and count using for loop.
Start
Declare i, sum, count
Set sum0
for i100 to 200
Is i%7==0 ?
true
Print i
sumsum+i
count + +
Print sum and count
Stop
9. Algorithm: To Print number from 100 to 200 which are divisible by 7 and
display their sum and count using for loop.
step1: start
step2: Declare i, sum, count
step3: set sum=0
step4: for i=100 to 200
if(i%7==0)
then print i
sum= sum+i
count++
step4: print sum and count
step5: end
9. C Program to print numbers from 100 to 200 which are divisible by 7
and display their sum and count using for loop.
#include<stdio.h>
int main()
{
int i, sum=0, count;
for(i=100; i<200; i++)
{
if(i%7==0)
{
printf(“%d\n”,i);
sum+=i;
count++;
}
}
printf(“sum=%d count=%d”,sum,count);
return 0;
}
Output:
105
112
119
126
133
140
147
154
161
168
175
182
189
196
Sum=2107 count=14
10. Algorithm: To find the summation of n natural numbers
Step 1: Start
Step 2: Declare variables n, i, and sum
Step 3: Initialize sum 0
Step 4: Read the value of n
Step 5: Repeat steps 6 and 7 for i = 1 to n
Step 6: sum = sum + i
Step 7: Increment i by 1
Step 8: After the loop ends, display the value of sum
Step 9: Stop
10. C program to find the summation of n natural numbers.
#include<stdio.h>
int main()
{
int i, n, sum=0;
printf(“Enter a number: ”);
scanf(“%d”,&n);
for(i=0;i<=n;i++)
{
sum=sum+i;
}
printf(“"The summation of first %d natural numbers is: %d\n", n, sum);
return 0;
}
Output:
Enter the number: 5
The summation of first 5 natural numbers is: 15
10. Flowchart to find the summation of n natural numbers.
Start
Declare n, i, sum
Set sum=0
Read n
for i0 to n
sum=sum + i
Print sum
Stop