/* ********************************************
36. To Find Sum of "n" Natural Numbers
********************************************** */
#include<*stdio.h>
#include<*conio.h>
void main()
{
int n,s = 0,i = 1;
clrscr();
printf("\n Enter n,To Find Sum of First n Natural Numbers : ");
scanf("%d", &n);
while(i <= n)
{
s = s + i;
i++;
}
printf("\n\n Sum of first %d Natural Numbers is : %d", n, s);
getch();
}
Output :
Enter n,To find Sum of First n Natural Numbers : 5
Sum of First 5 NaturalNumbers is : 15
/* ***********************************************
37. To Print n fibonacci numbers using While loop
*********************************************** */
#include<*stdio.h>
#include<*conio.h>
void main()
{
int n,a = 0,b = 1,c,i = 2;
clrscr();
printf("\n Enter n,To Print n Fibonacci Series Numbers : ");
scanf("%d", &n);
printf("\n\n %d \n", a);
printf("\n %d \n", b);
while(i<=n)
{
c = a + b;
printf("\n %d \n", c);
a = b;
b = c;
i++;
}
getch();
}
Output :
Enter n,To Print n Fibonacci Series Numbers : 6
0
1
1
2
3
5
/* **************************************************
38. To Find Factorial of a Given Number using while loop
************************************************** */
#include<*stdio.h>
#include<*conio.h>
void main()
{
int n,s = 1,i = 1;
clrscr();
printf("\n Enter any Number,To Find its Faactorial : ");
scanf("%d", &n);
while(i <= n)
{
s = s * i;
i++;
}
printf("\n\n Factorial of %d is : %d", n, s);
getch();
}
Output :
Enter any Number,To Find its Factorial : 5
Factorial of 5 is 120
/* *************************************************
39. To Print Multiplication Table of a Number using Do-While
39. To Print Multiplication Table of a Number using Do-While
**************************************************/
#include<*stdio.h>
#include<*conio.h>
void main()
{
int n,i = 1;
clrscr();
printf("\n Enter any Number,To Print its Multiplication Table : ");
scanf("%d", &n);
do
{
printf("\n %d * %d = %d \n", n, i, n*i);
i++;
}while(i <= 10);
getch();
}
Output :
Enter any Number,To Print its Multiplication Table : 5
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
/* ****************************************
40. To Print All Even Numbers upto n
***************************************** */
#include<*stdio.h>
#include<*conio.h>
void main()
{
int n,i = 1;
clrscr();
printf("\n Enter n,To Print all Even Numbers upto it : ");
scanf("%d", &n);
do
{
printf("\n %d \n", 2*i);
i++;
}
while(2*i <= n);
getch();
}
Output :
Enter n,To Print all Even Numbers upto it : 9
2
4
6
8
0 comments:
Post a Comment