WEEK 6
Objective: Explore the full scope of iterative constructs namely while loop, do-while loop and for loop in
addition to structured jump constructs like break and continue including when each of these statements is
more appropriate to use.
Suggested Experiments/Activities:
Tutorial 6: Loops, while and for loops
Lab 6: Iterative problems e.g., the sum of series
i) Find the factorial of given number using any loop.
ii) Find the given number is a prime or not.
iii) Compute sine and cos series
iv) Checking a number palindrome
v) Construct a pyramid of numbers.
[Link] a C program to find the factorial of a given number using any loop.
Algorithm:
Step 1: Start
Step 2: Declare the variables n,i,fact
Step 3: Read the value of variable n
Step 4: Assign fact=1 and i=1
Step 5: Compare i is greater than or equal to n then goto step6 otherwise goto step8
Step 6: fact=fact*i
Step 7: i=i+1 goto step5
Step 8: Display " Factorial of Given number n is fact"
Step 9: Stop
Source Code:
#include<stdio.h>
int main()
{ int i,n,fact=1;
printf("enter the value of n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{ fact=fact*i;
}
printf(" Factorial of Given number %d is %d", n,fact);
return 0;
}
Input: enter the value of n: 5
Output:Factorial of Given number 5 is 120
[Link] a C program to find if the given number is a prime or not.
Algorithm:
Step 1: Start
Step 2: Declare the variables n,i,count
Step 3: Read the value of variable n
Step 4: Assign count=0 and i=1
Step 5: Compare i is greater than or equal to n then goto step6 otherwise goto step9
Step 6: check whether n is divisible by i goto step 7 otherwise goto step8
Step 7: count=count+1
Step 8: i=i+1 goto step5
Step 9: compare count is equal to 2 then goto step 10 otherwise goto step 11
Step 10:Display " Given number n is a prime number"
Step 11:Display " Given number n is not a prime number"
Step 12: Stop
Source Code:
#include<stdio.h>
int main()
{ int i,n,count=0;
printf("enter the value of n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{ if(n%i==0)
{ count++;
}
}
if(count==2)
{ printf("\nGiven number %d is a prime number",n);
}
else
{ printf(" \n Given number %d is not a prime number",n);
}
return 0;
}
Input: enter the value of n: 7
Output: Given number 7 is a prime number
Input: enter the value of n: 15
Output: Given number 15 is not a prime number
III. Write a C program to Compute sine and cos series
Algorithm:
Step 1: Start
Step 2: Declare the variables n,i,x,sum,t
Step 3: Read the value of variable x,n
Step 4: Assign sum=1 and t=1,i=1
Step 5: compute x=x*3.14/180
Step 6: Compare i is greater than or equal to n then goto step7 otherwise goto step
Step 7: compute t=t*(-1)*x*x/(2*i*(2*i-1))
Step 8: sum=sum+t goto step6
Step 9: Display " The value of Cos is sum"
Step 10:Stop
Source Code:
#include<stdio.h>
int main()
{ int i, n;
float x, sum=1, t=1;
printf(" Enter the value for x : ");
scanf("%f",&x);
printf(" Enter the value for n : ");
scanf("%d",&n);
x=x*3.14159/180;
for(i=1;i<=n;i++) /* Loop to calculate the value of Cosine */
{ t=t*(-1)*x*x/(2*i*(2*i-1));
sum=sum+t;
}
printf(" The value of Cos(%f) is : %.4f", x,sum);
return 0;
}
Input: Enter the value of x: 30
Enter the value for n : 4
Output: The value of Cos(0.52359) is : 0.8660
SINE SERIES
Algorithm:
Step 1: Start
Step 2: Declare the variables n,i,x,sum,t
Step 3: Read the value of variable x,n
Step 4: compute x=x*3.14/180
Step 5: Assign sum=x and t=x,i=1
Step 6: Compare i is greater than or equal to n then goto step7 otherwise goto step
Step 7: compute t=(t*(-1)*x*x)/(2*i*(2*i+1))
Step 8: sum=sum+t goto step6
Step 9: Display " The value of Sin is sum"
Step 10:Stop
Source Code:
#include<stdio.h>
int main()
{
int i, n;
float x, sum, t;
printf(" Enter the value for x : ");
scanf("%f",&x);
printf(" Enter the value for n : ");
scanf("%d",&n);
x=x*3.14159/180;
t=x;
sum=x;
for(i=1;i<=n;i++) /* Loop to calculate the value of Sine */
{ t=(t*(-1)*x*x)/(2*i*(2*i+1));
sum=sum+t;
}
printf(" The value of Sin(%f) = %.4f",x,sum);
return 0;
}
Input: Enter the value of x: 45
Enter the value for n : 4
Output: The value of Sin(0.785398) = 0.7071
IV. Write a C program on checking a number palindrome.
Algorithm:
Step 1: Start
Step 2: Declare the variables n,temp,rem,rev
Step 3: Read the value of variable n
Step 4: Assign temp=n
Step 5: Compare n is greater than zero then goto step6 otherwise goto step9
Step 6: compute rem=n%10
Step 7: compute rev=rev*10+rem
Step 8: n=n/10 goto step5
Step 9: compare temp is equal to rev then goto step 10 otherwise goto step 11
Step 10:Display " Given number n is a palindrome number"
Step 11:Display " Given number n is not a palindrome number"
Step 12: Stop
Source Code:
#include<stdio.h>
int main()
{ int temp,n,rem,rev;
printf("Enter the number n:");
scanf("%d",&n);
temp=n;
while(n>0)
{ rem=n%10;
rev=rev*10+rem;
n=n/10;
}
if(temp==rev)
{ printf("\n Given number %d is a palindrome number",temp);
}
else
{ printf("Given number %d is not a palindrome number",temp);
}
return 0;
}
Input1: Enter the number n: 121
Output1: Given number 121 is a palindrome number
Input2: Enter the number n: 134
Output2: Given number 134 is not a palindrome number
V. Write a C program to construct a pyramid of numbers
A full pyramid pattern of numbers is similar to an equilateral triangle.
Pyramid Example:
1
222
33333
4444444
555555555
Algorithm:
Step 1: Start
Step 2: Declare the variables rows,i,j,k
Step 3: Read the value of variable rows
Step 4: Assign i=1
Step 5: Compare i is less than or equal to rows then goto step6 otherwise goto step 15
Step 6: assign j=1
Step 7: Compare j is less than or equal to 2*(rows-i) then goto step8 otherwise goto step10
Step 8: Display " one space"
Step 9: j=j+1 goto step 7
Step 10: Assign k=1
Step 11:Compare k is less than 2*i then goto step12 otherwise goto step14
Step 12:Display "i value"
Step 13:k=k+1 goto step 11
Step 14: i=i+1 goto step 5
Step 15: Stop
Source Code:
#include <stdio.h>
int main()
{ int rows;
printf("Number of rows: ");
scanf("%d", &rows);
// first loop to print all rows
for (int i = 1; i <= rows; i++)
{ // inner loop 1 to print white spaces
for (int j = 1; j <= 2 * (rows - i); j++)
{ printf(" ");
}
// inner loop 2 to print numbers
for (int k = 1; k < 2 * i; k++) {
printf("%d ", i);
}
printf("\n");
}
return 0;
}
Input: Number of rows: 5
1
222
33333
4444444
555555555