Questions:
Lab works-03
- Write a C program to calculate and display the following series: 1 3 5 ...... to 10th terms.
- Write a C program to display square series of first 'n' natural numbers.
- Write a C program to calculate factorial of any number given by the user.
- Write a C program to calculate and display the multiplication table of a number.
- Write a C program to display Fibonacci series having 'n' terms.
- Write a C program to check the given number is palindrome or not palindrome.
- Write a C program to calculate sum of the following series.
sum=1+1/2+1/3+1/4+....+1/n - Write a C program to display the following output:
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15 - Write a C program to check whether the given number is prime or not.
- Write a C program to display the prime series:
2 3 5 7 11 up to n.
Write a C program to calculate and display the following series:
1 3 5 …… to 10th terms
ANSWER:
Algorithm to calculate and display : 1 3 5 …. to 10th terms
Step1: Start
Step 2: Set i = 0 and a=1
Step 3: Check, is i<10?
3.1 If yes, display ‘a’ and calculate a=a+2 and goto step 4
3.2 If no, go to step 5.
STep 4: Calculate i=i+1 and go to step 3
Step 5: Stop
/*C Code to calculate and display : 1 3 5 …. to 10th terms*/
#include <stdio.h>
int main()
{
int i=0,a=1;
for(i=0;i<10;i++)
{
printf(" %d ",a);
a=a+2;
}
return 0;
}
Write a C program to display a square series of first ‘n’ natural numbers.
ANSWER:
Algorithm to display a square series of first ‘n’ natural numbers.
Step1: Start
Step 2: Input a number as n
Step 3: Set i=1
Step 4: Check, is i<=n?
4.1 If yes, Calculate sq=i*i ; Display “ sq ” and goto step 5
4.2 If no, goto step 6
Step 5: Calculate i=i+1 and go to step 4
Step 6: Stop
/*C Code to display a square series of first ‘n’ natural numbers.*/
#include <stdio.h>
int main()
{
int i,n,sq;
printf("Enter the value of n : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sq=i*i;
printf(" %d ",sq);
}
return 0;
}
Write a C program to calculate the factorial of any number given by the user. [Factorial means multiple of all positive whole numbers integer upto given number. E.g. Factorial of 5=1*2*3*4*5=120]
ANSWER:
Algorithm to calculate the factorial of any number given by the user
Step1: Start
Step 2: Input a number as n
Step 3: Set f=1,i=1
Step 4: Check, is i <=n?
4.1 If yes, Calculate f=f*i and goto step 5
3.2 If no, goto step 6
Step 5: Calculate i=i+1 and go to step 4
Step 6: Display f
Step 7: Stop
/*C Code to calculate the factorial of any number given by the user*/
#include <stdio.h>
int main()
{
int i,n,f=1;
printf("Enter the value of n : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
f=f*i;
}
printf("Factorial of %d is %d ",n,f);
return 0;
}
Write a C program to calculate and display the multiplication table of a number.
ANSWER:
Algorithm to calculate and display the multiplication table of a number
Step1: Start
Step 2: Input a number as n
Step 3: Set i=1
Step 4: Check, is i <=10?
4.1 If yes, Calculate and display n*i and goto step 5
3.2 If no, goto step 6
Step 5: Calculate i=i+1 and go to step 4
Step 6: Stop
/*C Code to calculate and display the multiplication table of a number*/
#include <stdio.h>
int main()
{
int i,n;
printf("Enter the value of n : ");
scanf("%d",&n);
for(i=1;i<=10;i++)
{
printf("%d x %d = %d \n", n,i,n*i);
}
return 0;
}
Write a C program to display Fibonacci series having ‘n’ terms (Fibonacci series means the sum of two preceding numbers will create a succeeding number. Example: 0,1,1,2,3,5,.....)
ANSWER:
Algorithm to display Fibonacci series having ‘n’ terms
Step 1: Start
Step 2: Set a=0,b=1, i=0
Step 3: Ask the value of ‘n’
Step 4: Is i<n?
4.1 If yes, calculate c=a+b and display “a” . Also set a=b , b=c
4.2 If no, go to step 6
Step 5: Calculate i=i+1 and go to step 4
Step 6: Stop
/*C Code to display Fibonacci series having ‘n’ terms*/
#include <stdio.h>
int main()
{
int a=0,b=1,c,n,i;
printf(" Enter the value of n : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
c=a+b;
printf(" %d ",a);
a=b;
b=c;
}
return 0;
}
Write a C program to check if a given number is palindrome or not palindrome . (If a number gives the same number when it reversed is called palindrome number .eg 121, 101 etc)
ANSWER:
Algorithm to check if a given number is palindrome or not palindrome
Step 1: Start
Step 2: Set sum=0 and ask to enter any number as n
Step 3: Store a=n
Step 4: Is n !=0?
4.1 If yes, calculate r=n%10 , sum=sum*10+r, n=n\10
4.2 If no, go to step 5
Step 5: Is sum = = a?
5.1 If yes, display “ Palindrome number ”
5.2 If no, display “ Not Palindrome number ”
Step 6: Stop
/*C Code to check if a given number is palindrome or not palindrome*/
#include <stdio.h>
int main()
{
int n,r,sum=0,a;
printf(" Enter the value of n : ");
scanf("%d",&n);
a=n;
while(n!=0)
{
r=n%10;
sum=sum*10+r;
n=n/10;
}
if(sum==a)
{
printf(" Palindrome number ");
}
else
{
printf(" Not Palindrome number ");
}
return 0;
}
Write a C program to calculate sum of the following series:
sum=1+½+⅓+¼+...+1/n
ANSWER:
Algorithm to calculate sum of given series
Step 1: Start
Step 2: Set sum=0,i=1
Step 3: Ask the value of n
Step 4: Is i<=n?
4.1 If yes, calculate sum=sum+1/i and go to step 5
4.2 If no, go to step 6
Step 5: Calculate i=i+1 and go to step 4
Step 6: Display sum
Step 7: Stop
/*C Code to calculate sum of given series*/
#include <stdio.h>
int main()
{
float n,i,sum=0;
printf("Enter the value of n: ");
scanf("%f",&n);
for(i=1;i<=n;i++)
{
sum=sum+1/i;
}
printf(" Sum = %0.2f ",sum);
return 0;
}
Write a C program to display the following output:
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
ANSWER:
Algorithm to display given output:
Step 1: Start
Step 2: Set the outer for loop(i) from 1 to 3
Step 3: Set the inner for loop(j) from 1 to 5
Step 4: Calculate and display i*j
Step 5: Stop
/*C Code to display given output:*/
#include <stdio.h>
int main()
{
int i,j;
for(i=1;i<=3;i++)
{
for(j=1;j<=5;j++)
{
printf("%d\t",i*j);
}
printf("\n");
}
return 0;
}
Write a C program to check whether the given number is prime or not.
ANSWER:
Algorithm to check given number is prime or not
Step 1: Start
Step 2: Initialize I=1, count=0
Step 3: Input any number as N
Step 4: Is I<=N?
4.1: If true, Calculate remainder(R) of num divided by I and check, IS R=0?
If yes, calculate count=count+1 and go to step 5
4.2: If false, go to step 6
Step 5: Calculate I=I+1 and go to step 4
Step 6: Is count equal to two.
6.1: If true, Print "Prime"
6.2: If false, Print "Composite"
Step 7: Stop
/*C Code to check given number is prime or not*/
#include <stdio.h>
int main()
{
int n,count=0,i;
printf("Enter any number : ");
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 composite number.",n);
}
return 0;
}
Write a C program display the prime series:
2 3 5 7 11 up to n.
ANSWER:
Algorithm to display prime series upto n
Step 1: Start
Step 2: Input any number as n
Step 3: Set i=1
Step 4: Is i<=n?
4.1: If true, set count=0, Use inner loop(j) as 1 to i
Calculate remainder(R) of i divided by j and
check IS R=0? If yes, calculate count=count+1 and go to step 5
4.2: If false, go to step 6
Step 5: Calculate i=i+1 and go to step 4
Step 6: Is count equal to two.
6.1: If true, Print "Prime"
6.2: If false, go to step 7
Step 7: Stop
/*C Code to display the prime series up to n*/
#include <stdio.h>
int main()
{
int count,n,i,j;
printf("Enter value of n: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
count=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
count++;
}
if(count==2)
{
printf(" %d ",i);
}
}
return 0;
}
Nicee
ReplyDelete