Digital Learning

Saturday, April 4, 2020

Prime number,Fibonacci series,break & continue statement:

‘C’ Programs:
1. Write a C program to find Prime or not prime number.
2. Write a C program to print a table of a given number.
3. C program to print Fibonacci series.
4. Write a C program to print number in reverse order.
5. C program for sorting an array.
6. Write a C program using break statement.
7. Write a C program using continue statement.

Prime Number:
Prime numbers are those numbers which can divide by one and itself without remainder. Prime numbers are always greater than one. For examples: number 5 is prime number because it is divisible by 1 and itself only.

Write a C program to find Prime or not prime number.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, no;
clrscr();
printf(“\n Enter a number ”);
scanf(“%d”, &no);
for(i=2; i<=no; i++)
{
if(no%i==0)
break;
}
if(i==no)
printf(“Prime number”);
else
printf(“Not Prime number”);
getch();
}

Output:
Enter a number 5
Prime number
Enter a number 6
Not Prime number
         

Write a C program to print a table of a given number.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, a, no;
clrscr();
printf(“\n Enter number to print a table”);
scanf(“%d”, &no);
for(i=1; i<=10; i++)
{
a= no*i;
printf(“\n %d”,a);
}
getch();
}
Output:
Enter number to print a table 5
5
10
15
20
25
30
35
40
45
50

Fibonacci Series:
Using Fibonacci series we can print various terms of the series. The some numbers of the series are 0, 1, 1, 2, 3, 5, 8, 13…… In Fibonacci series every term is the addition of the previous two terms of series.

C program to print Fibonacci series.
Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int i, n1=0, n2=1, n3;
clrscr();
printf(“%d%d”, n1, n2);
for(i=1;i<=10; i++)
{
n3=n1+n2;
printf(“%d”, n3);
n1=n2;
n2=n3;
}
getch();
}

Write a C program to print number in reverse order.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=20; i>=1;i--)
{
printf(“%d”, i);
}
getch();
}

Output:
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

C program for sorting an array.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int num[5], i, j, t;
clrscr();
for(i=0; i<=4; i++)
{
scanf(“%d”, &num[i]);
}
for(i=0; i<=4; i++)
{
for(j=0; j<=3; j++)
{
if(num[j]>num[j+1])
{
t=num[j];
num[j]=num[j+1];
num[j+1]=t;
}
}
}
printf(“\n”);
for(i=0; i<=4; i++)
{
printf(“%d  \n”, num[i]);
}
getch();
}

Break Statement:
Break statement is used to terminate a loop immediately and program control the next statement. Break statement is also used to terminate a case statement.

Write a C program using break statement.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
for(i=1; i<=30;i++)
{
if(i*i*i==27)
break;
printf(“%d”, i);
}
getch();
}

Continue Statement:
Continue statement is used to continue the next iteration. Continue statement is also used to left some statement of code and continue with other statement.

Write a C program using continue statement.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
for(i=1; i<=30;i++)
{
if(i*i*i==27)
continue;
printf(“%d”, i);
}
getch();
}

No comments:

Post a Comment