11. Write a program to generate fibonnacci series.
#include<stdio.h>
#include<conio.h>
int main()
{
int n1=0,n2=1,n3=1,n,i;
clrscr();
printf("Enter the Number : ");
scanf("%d",&n);
printf("\n");
for(i=1;i<=n;i++)
{
printf(" %d ",n3);
n3=n1+n2;
n1=n2;
n2=n3;
}
getch();
return 0;
}
Output:
Enter the Number : 5
1 1 2 3 5
12. Write a program to print the multiplication table.
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,n;
clrscr();
printf("Enter the number : ");
scanf("%d",&n);
for(i=1;i<=10;i++)
{
printf("\n %d * %d = %d",n,i,i*n);
}
getch();
return 0;
}
Output:
Enter the number : 9
9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90
13. Write a program to find a factorial of the entered number.
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,f=1;
clrscr();
printf("\n Enter the Number : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
f=f*i;
}
printf("\n The factorial of %d is : %d",n,f);
getch();
return 0;
}
Output:
Enter the Number : 5
The factorial of 5 is : 120
14. Write a program to print all the numbers and sum of all the integers that are greater than 100 and less than 200 and are divisible by 7.
#include<stdio.h>
#include<conio.h>
int main()
{
int i,sum=0;
clrscr();
for(i=100;i<200;i++)
{
if(i%7==0)
{
printf(" %d ",i);
sum=sum+i;
}
}
printf("\n\n Sum of all above integers that are divisible by 7 is %d",sum);
getch();
return 0;
}
Output:
105 112 119 126 133 140 147 154 161 168 175 182 189 196
Sum of all above integers that are divisible by 7 is 2107
15. Write a program to find the roots of an equation ax2 + bx + c = 0.
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
float a,b,c,alf,bt,dlt;
clrscr();
printf("\n Enter a: ");
scanf("%f",&a);
printf("\n Enter b: ");
scanf("%f",&b);
printf("\n Enter c: ");
scanf("%f",&c);
dlt=b*b-4*a*c;
if(dlt==0)
{
printf("\n ALPHA=BETA=%f",-b/(2*a));
}
else if(dlt<0)
{
printf("\n Imaginary Roots");
}
else
{
alf=(-b+sqrt(dlt))/(2*a);
bt=(-b-sqrt(dlt))/(2*a);
printf("\n\n Alpha = %f\n Beta=%f\n",alf,bt);
}
getch();
return 0;
}
Output:
(1)
Enter a: 12
Enter b: 2
Enter c: 34
Imaginary Roots
(2)
Enter a: 2
Enter b: 6
Enter c: 2
Alpha = -0.381966
Beta = -2.618034
(3)
Enter a: 2
Enter b: 4
Enter c: 2
ALPHA=BETA= -1.000000
16. Write a program that accept basic, HRA, and convergence from the user and calculate total salary.
#include<stdio.h>
#include<conio.h>
int main()
{
float basic,HRA,cnvg,totsal;
clrscr();
printf("\n Enter basic salary : ");
scanf("%f",&basic);
printf("\n Enter HRA : ");
scanf("%f",&HRA);
printf("\n Enter convergence : ");
scanf("%f",&cnvg);
HRA=(basic*HRA)/100;
cnvg=(basic*cnvg)/100;
totsal=basic+HRA+cnvg;
printf("\n\n Total salary is %.2f",totsal);
getch();
return 0;
}
Output:
Enter basic salary : 3000
Enter HRA : 5
Enter convergence : 2
Total salary is 3210.00
17. Print the following triangle.
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,j,k;
clrscr();
printf("Enter the number : ");
scanf("%d",&n);
printf("\n");
for(i=0;i<n;i++)
{
for(k=1;k<=i;k++)
{
printf(" ");
}
for(j=1;j<=n-i;j++)
{
printf(" %c",96+j);
}
printf("\n");
}
getch();
return 0;
}
Output:
Enter the number : 5
a b c d e
a b c d
a b c
a b
a
18. Write a program that prints the following Floyd’s triangle.
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,n,k;
clrscr();
printf("\n\n Enter the number : ");
scanf("%d",&n);
for(i=1,k=1;i<=n;i++)
{
for(j=1;j<=i;j++,k++)
{
printf(" %d ",k);
}
printf("\n");
}
getch();
return 0;
}
Output:
Enter the number : 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
19. Write a program to find maximum element from 1-Dimensional array.
#include<stdio.h>
#include<conio.h>
int main()
{
int a[20],n,i,max=0; // declared the array a with size 20
clrscr();
printf("\n Enter the number of elements for 1-D array : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter element [%d] : ",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
if(max<a[i])
max=a[i];
}
printf("\n Maximum element from above array inserted is : %d",max);
getch();
return 0;
}
Output:
Enter the number of elements for 1-D array : 5
Enter element [1] : 1
Enter element [2] : 8
Enter element [3] : 2
Enter element [4] : 5
Enter element [5] : 9
Maximum element from above array inserted is: 9
20. Write a program to sort given array in ascending order.
#include<stdio.h>
#include<conio.h>
int main()
{
int a[20],i,j,n,temp;
clrscr();
printf("\n Enter no. of elements for 1-D array : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf(" Enter element[%d] : ",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\n\n Ascending order of inserted array is : ");
for(i=0;i<n;i++)
{
printf("\n %d ",a[i]);
}
getch();
return 0;
}
Output:
Enter no. of elements for 1-D array : 5
Enter element [1] : 2
Enter element [2] : 10
Enter element [3] : 4
Enter element [4] : 13
Enter element [5] : 7
Ascending order of inserted array is:
2
4
7
10
13