Practical 3
Practical 3
void main()
int i;
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;
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;
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;
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;
scanf("%d",&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;
scanf("%d",&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;
scanf("%d",&n);
for (i=1;i<=n;i++)
fact=fact*i;
Output:-
Enter the number
void main()
int i=1,n,fact=1;
scanf("%d",&n);
while (i<=n)
fact=fact*i;
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;
scanf("%d",&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
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;
scanf("%d",&n);
while(i<=n)
fact=fact*i;
i=i+1;
sum=sum+fact;
Output:-
Enter the limit: 5
The factorial of 1 is 1.
The factorial of 2 is 2.
The factorial of 3 is 6.
void main()
int i,s,n,sum;
scanf("%d",&n);
for(i=1;i<=n; i++)
s=i*i;
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;
scanf("%d",&n);
for(i=1;i<=n; i++)
s=i*i*i;
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
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;
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;
scanf("%d",&n);
for(i=1;i<n;i++)
if(n%i==0){
sum+=i;
if(sum==n)
else
Output:-
Enter the Number to be checked: 25
void main()
int i,n,flag=0;
scanf("%d",&n);
for(i=2; i<n;i++){
if(n%i==0)
flag=1;
break;
if (flag==1)
else
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;
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)
else{
Output:-
Enter the number:
153
351
Entered number is not a palindrome number
void main()
int n,a,d,r=0;
scanf("%d",&n);
a=n;
while(n!=0){
d=n%10;
n=n/10;
r+=d*d*d;
if (r==a)
else
Output:-
Enter the number:
153
void main()
int n,i,fact,j;
scanf("%d",&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
7
11
13
void main()
int a,b,prod,lcm,x,y;
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;
lcm = prod/a;
Output:-
Enter two numbers:
20 30
GCD of 20 and 30 is 10
LCM of 20 and 30 is 60