[go: up one dir, main page]

0% found this document useful (0 votes)
15 views25 pages

Practical 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views25 pages

Practical 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

PRACTICAL 4 - Definition of The Loops

Name:- Mayuresh Pandey Date: 4th September 2024 Roll no:-


1. Write a c program to print first 10 Natural numbers using for loops
Input:-
#include<stdio.h>

void main()

int i;

printf("First 10 natural numbers are.......\n");

for(i=1;i<=10;i++)

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

Output:-
First 10 natural numbers are.......

10
2. Write a c program to print first 10 Natural numbers using while loops
Input:-
#include<stdio.h>

void main()

int i=1;

printf("First 10 natural numbers are.......\n");

while(i<=10)

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

i=i+1;

Output:-
First 10 natural numbers are.......

10
3. Write a c program to print first 10 Natural numbers using do while loops
Input:-
#include<stdio.h>
void main()
{
int i=1;
printf("First 10 natural numbers are.......\n");
do
{
printf("%d\n",i);
i=i+1;

}
while (i<=10);
}
Output:-
First 10 natural numbers are.......

10
4. Write a c program to print odd no between 1 and 30 using for loop
Input:-
#include<stdio.h>

void main()

int i;

printf("First 30 natural odd numbers are.......\n");

for(i=1;i<=30;i+=2)

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

Output:-
First 30 natural odd numbers are.......

11

13

15

17

19

21

23

25

27

29
5. Write a c program to print even no between 1 and 30 using for loop
Input:-
#include<stdio.h>

void main()

int i=2;

printf("first 30 natural numbers are even .......\n");

while(i<=30)

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

i=i+2;

Output:-
first 30 natural numbers are even .......

10

12

14

16

18

20

22

24

26

28

30
6. Write a c program to print a table of squares up to n
Input:-
#include<stdio.h>

void main()

int i,n;

printf("Enter the limit: \n");

scanf("%d",&n);

printf("Table of square up to %d is \n",n);

i=1;

while (i<=n)

printf("%d square=%d\n",i,i*i);

i++;

printf("bye");

Output:-
Enter the limit:

Table of square up to 5 is

1 square=1

2 square=4

3 square=9

4 square=16

5 square=25

bye
7. Write a c program to print a table of cubes up to n
Input:-
#include<stdio.h>

void main()

int i,n;

printf("Enter the limit: \n");

scanf("%d",&n);

printf("Table of cube up to %d is\n",n);

i=1;

do

printf("%d cube=%d\n",i,i*i*i);

i=i+1;

while(i<=n);

printf("byee..\n");

Output:-
Enter the limit:

Table of cube up to 5 is

1 cube=1

2 cube=8

3 cube=27

4 cube=64

5 cube=125

byee..
8. Write a c program to find factorial using for loop
Input:-
#include<stdio.h>

void main()

int i,n,fact=1;

printf("Enter the number \n");

scanf("%d",&n);

for (i=1;i<=n;i++)

fact=fact*i;

printf("The factorial of %d is %d\n",n,fact);

Output:-
Enter the number

The factorial of 5 is 120


9. Write a c program for the Table of factorial to n
Input:-
#include<stdio.h>

void main()

int i=1,n,fact=1;

printf("Enter the number \n");

scanf("%d",&n);

while (i<=n)

fact=fact*i;

printf("The factorial of %d is %d\n",i,fact);

i++;

Output:-
10. Write a C Program to print Fibonacci series upto n
Input:-
#include<stdio.h>

void main()

int i,n,f=0,s=1,t;

printf("Enter The Limit \n");

scanf("%d",&n);

printf("The fibonacci series upto %d is \n",n);

printf("%d\t%d\t",f,s);

for (i=1;i<=n;i++)

t=f+s;

printf("%d\t",t);

f=s;

s=t;

Output:-
Enter The Limit

The fibonacci series upto 5 is

0 1 1 2 3 5 8
11. Write a c program to find the sum of factorials of numbers up to n
Input:-
#include<stdio.h>

void main()

int i=1,n,fact=1,sum=0;

printf("Enter the limit: \t");

scanf("%d",&n);

while(i<=n)

fact=fact*i;

printf("The factorial of %d is %d.\n",i,fact);

i=i+1;

sum=sum+fact;

printf("\n The sum of all factorial is %d.\n\n",sum);

Output:-
Enter the limit: 5

The factorial of 1 is 1.

The sum of all factorial is 1.

The factorial of 2 is 2.

The sum of all factorial is 3.

The factorial of 3 is 6.

The sum of all factorial is 9.

The factorial of 4 is 24.

The sum of all factorial is 33.

The factorial of 5 is 120.

The sum of all factorial is 153.


12. Write a c program to find the sum of squares of numbers up to n
Input:-
#include<stdio.h>

void main()

int i,s,n,sum;

printf("Enter limit: \n");

scanf("%d",&n);

for(i=1;i<=n; i++)

s=i*i;

printf("%d square= %d\n",i,s);

sum=sum+s;

printf("sum= %d\n",sum);

Output:-
Enter limit:

1 square= 1

2 square= 4

3 square= 9

4 square= 16

5 square= 25

sum= 55
13. Write a c program to find the sum of cubes of numbers up to n
Input:-
#include<stdio.h>

void main()

int i,s,n,sum;

printf("Enter limit: \n");

scanf("%d",&n);

for(i=1;i<=n; i++)

s=i*i*i;

printf("%d cube= %d\n",i,s);

sum=sum+s;

printf("sum= %d\n",sum);

Output:-
Enter limit:

1 cube= 1

2 cube= 8

3 cube= 27

4 cube= 64

5 cube= 125

sum= 225
14. Write a c program to illustrate the use of break jump start in for loop
Input:-
#include<stdio.h>

void main()

int i;

for(i=1;i<=10;i++)

if(i==5)

break;

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

printf("Bye...");

Output:-
1

Bye...
15. Write a c program to illustrate the use of continue jump start in for loop
Input:-
#include<stdio.h>

void main()

int i;

for(i=1;i<=10;i++)

if(i==5)

continue;

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

printf("Bye...");

Output:-
1

10

Bye...
16. Write a c program to illustrate the use of nesting of for loop
Input:-
#include<stdio.h>

void main()

int i,j;

for(i=1;i<=5;i++)

for(j=1;j<=5;j++)

printf("%d\t%d\n",i,j);

Output:-
1 1

1 2

1 3

1 4

1 5

2 1

2 2

2 3

2 4

2 5

3 1

3 2

3 3

3 4

3 5

4 1

4 2

4 3
4 4

4 5

5 1

5 2

5 3

5 4

5 5

17. Write a c program to print list of stars using nested loops


Input:-
#include<stdio.h>

void main()

int i,j;

for(i=1;i<=5;i++)

for (j=1;j<=i;j++)

printf("*\t");

printf("\n");

Output:-
*

* *

* * *

* * * *

* * * * *
18. Write a C program to the multiplication table of nos upto n
Input:-
#include<stdio.h>

void main()

int i,n,j,k;

printf("Enter the limit: \n");

scanf("%d",&n);

for(i=1;i<=n;i++)

for (j=1;j<=10;j++)

k=i*j;

printf("%d\t",k);

printf("\n");

Output:-
Enter the limit:
5

1 2 3 4 5 6 7 8 9 10

2 4 6 8 10 12 14 16 18 20

3 6 9 12 15 18 21 24 27 30

4 8 12 16 20 24 28 32 36 40

5 10 15 20 25 30 35 40 45 50
19. Write a c program to check whether number is perfect or not
Input:-
#include<stdio.h>

void main()

int n,i,sum=0;

printf("Enter the Number to be checked:\t");

scanf("%d",&n);

for(i=1;i<n;i++)

if(n%i==0){

sum+=i;

if(sum==n)

printf("The Number is perfect ...\n",n);

else

printf("the Number is not perfect ...\n",n);

Output:-
Enter the Number to be checked: 25

the Number is not perfect ...

20. Write a c program to check whether number is prime or not


Input:-
#include<stdio.h>

void main()

int i,n,flag=0;

printf("Enter the number");

scanf("%d",&n);
for(i=2; i<n;i++){

if(n%i==0)

flag=1;

break;

if (flag==1)

printf("%d is not a prime number ",n);

else

printf("%d is a prime number",n);

Output:-
Enter the number5

5 is a prime number
21. Write a c program to find reverse of n digit number also check palindromic
sequence
Input:-
#include<stdio.h>

void main()

int n,a,rev=0,d;

printf("Enter the number: \n");

scanf("%d",&n);

a=n;

while(n!=0){

d=n%10;

n=n/10;

rev=rev*10+d;

printf("%d\n",rev);

if(a==rev)

printf("Entered number is a palindrome number");

else{

printf("Entered number is not a palindrome number");

Output:-
Enter the number:

153

351
Entered number is not a palindrome number

22. Write a c program to check whether it is Armstrong number or not


Input:-
#include<stdio.h>

void main()

int n,a,d,r=0;

printf("Enter the number: \n");

scanf("%d",&n);

a=n;

while(n!=0){

d=n%10;

n=n/10;

r+=d*d*d;

if (r==a)

printf("%d is an Armstrong Number.",a);

else

printf("%d is not an Armstrong Number.",a);

Output:-
Enter the number:

153

153 is an Armstrong Number.


23. Write a c program to find prime no series up to n
Input:-
#include<stdio.h>

void main()

int n,i,fact,j;

printf("Enter the Number: \n");

scanf("%d",&n);

printf("Prime numbers are: \n");

for (i=1;i<=n;i++)

fact=0;

for(j=1; j<=n;j++)

if(i%j==0)

fact++;

if(fact==2)

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

Output:-
Enter the Number:

15

Prime numbers are:

7
11

13

24. Write a c program to find the GCD and LCM of 2 numbers


Input:-
#include<stdio.h>

void main()

int a,b,prod,lcm,x,y;

printf("Enter two numbers: \n");

scanf("%d%d",&a,&b);

x=a;

y=b;

prod=x*y;

while(a!=b)

if(a>b)

a=a-b;

else

b=b-a;

printf("GCD of %d and %d is %d\n",x,y,a);

lcm = prod/a;

printf("LCM of %d and %d is %d\n",x,y,lcm);

Output:-
Enter two numbers:

20 30

GCD of 20 and 30 is 10

LCM of 20 and 30 is 60

You might also like