1) / Program To Print 1 12 123 1234 12345 Using For Loop
1) / Program To Print 1 12 123 1234 12345 Using For Loop
1
12
123
1234
12345 using for loop*/
#include<stdio.h>
#include<conio.h>
void main( )
{
int i,j,n;
clrscr( );
printf("enter the value of n\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
printf("%d",j);
printf("\n");
}
getch( );
}
2) /*program to print
12345
1234
123
12
1 using for loop*/
#include<stdio.h>
#include<conio.h>
void main( )
{
int i,j,n;
clrscr( );
printf("enter the value of n\n");
scanf("%d",&n);
for(i=n;i>=1;i--)
{
for(j=1;j<=i;j++)
printf("%d",j);
printf("\n");
}
getch( );
}
3) /*program to print
1
22
333
4444
55555 using for loop*/
#include<stdio.h>
#include<conio.h>
void main( )
{
int i,j,n;
clrscr( );
printf("enter the value of n\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
printf("%d",i);
printf("\n");
}
getch( );
}
4) /*program to print
*
**
***
****
***** using for loop*/
#include<stdio.h>
#include<conio.h>
void main( )
{
int i,j,n;
clrscr( );
printf("enter the value of n\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
printf("*");
printf("\n");
}
getch( );
}
5) /*program to print
*****
****
***
**
* using for loop*/
#include<stdio.h>
#include<conio.h>
void main( )
{
int i,j,n;
clrscr( );
printf("enter the value of n\n");
scanf("%d",&n);
for(i=n;i>=1;i--)
{
for(j=1;j<=i;j++)
printf("*");
printf("\n");
}
getch( );
}
6) /*program to print
*
**
***
****
* * * * * using for loop*/
#include<stdio.h>
#include<conio.h>
void main()
{
int blank=4,i,j,k,n=5;
clrscr();
for(i=1;i<=n;i++)
{
for(j=1;j<=blank;j++)
printf(" ");
for(k=1;k<=i;k++)
printf(" * ");
printf("\n");
blank=blank-1;
}
getch();
}
/*program to print
4321
321
21
1
*/
#include<stdio.h>
#include<conio.h>
void main( )
{
int i,j,n;
clrscr( );
printf("enter the value of n\n");
scanf("%d",&n);
for(i=n;i>=1;i--)
{
for(j=i;j>=1;j--)
printf("%d",j);
printf("\n");
}
getch( );
}
7) /* program to print sine series x-x^3/3! + x^5/5! - ... upto N terms accuracy */
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define PI 3.142
void main( )
{
int n,i;
float degree,x,term,numerator,denominator,sum;
clrscr( );
printf("Enter the number of terms to be added\n");
scanf("%d",&n);
printf("Enter the value of x in degrees\n");
scanf("%f",°ree);
x=degree * PI/180; //calculate x
numerator=x;
denominator=1;
sum=x;
for(i=3;i<=n;i+=2)
{
numerator=-numerator*x*x; //generate -x3,x5,-x7...
denominator=denominator*(i-1)*i; //generate 3!,5!,7!...
term=numerator/denominator; //obtain -x3/3!,x5/5!,-x7/7!...
sum=sum+term; //add the above terms
}
printf("sin(%f) = %f \n",degree,sum);
printf("Using library function sin(%f)=%f\n",degree,sin(x));
getch( );
}
8) /* program to print cosine series 1-x2/2! + x4/4! ... upto N terms accuracy */
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define PI 3.142
void main( )
{
int n,i;
float degree,x,term,numerator,denominator,sum;
clrscr( );
printf("Enter the number of terms to be added\n");
scanf("%d",&n);
printf("Enter the value of x in degrees\n");
scanf("%f",°ree);
x=degree * PI/180; //calculate x
numerator=1;
denominator=1;
sum=1;
for(i=2;i<=n;i+=2)
{
numerator=-numerator*x*x; //generate -x2,x4,-x6...
denominator=denominator*(i-1)*i; //generate 2!,4!,6!...
term=numerator/denominator; //obtain -x2/2!,x4/4!,x6/6!...
sum=sum+term; //add the above terms
}
printf("cos(%f) = %f \n",degree,sum);
printf("Using library function cos(%f)=%f\n",degree,cos(x));
getch( );
}