51. Triangle with only border
#include<stdio.h>
#include<conio.h>
*
* *
* *
* *
* *
* *
* * * * * *
void drawTriangle(char border, char filler, int length)
{
int start = 2;
int base = 4;
int i, sp, j, b;
for (i = start; i <= length; i++)
{
for (sp = 0; sp <= length - i; sp++)
{
printf(" ");
}
if (i > start)
{
printf("%c ", border);
}
if (i > start)
{
for (b = base; b <= i; b++)
{
printf("%c ", filler);
}
}
printf("%c \n", border);
}
for (j = base; j < length + base; j++)
{
printf("%c ", border);
}
printf("\n");
}
void main()
{
int length = 6;
clrscr();
drawTriangle('*', ' ', length);
getch();
}
52. Program to accept number and print it's factorial.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, fact=1, n;
clrscr();
printf("Enter number : ");
scanf("%d", &n);
for(i=1; i<=n; i++)
{
fact = fact*i;
}
printf("Factorial is: %d", fact);
getch();
}
53. Program to accept number and print if it is prime number or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, n;
clrscr();
printf("Enter number : ");
scanf("%d", &n);
for(i=2; i<=n/2; i++)
{
if(n%i==0)
{
printf("Number is not Prime");
getch();
break;
}
}
printf("Number is Prime");
getch();
}
54. Program to print 'n' prime numbers.
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
int i, j, flag=1, n;
clrscr();
printf("Enter number : ");
scanf("%d", &n);
for(i=2; i<=n; i++)
{
flag=1;
for(j=2; j<=i/2; j++)
{
if(i%j==0)
{
flag=0;
break;
}
}
if(flag==1)
printf("%d\n", i);
}
getch();
}
55. Program to accept a number and print Fibonacci sequence.
#include<stdio.h>
#include<conio.h>
void main()
{
int pre=1, cur=1, temp, i, n;
//pre means previous number
//cur means current number
clrscr();
printf("Enter number : ");
scanf("%d", &n);
printf("%d\t%d", pre, cur);
for(i=3; i<=n; i++)
{
temp = cur;
cur = pre + cur;
pre = temp;
printf("\t%d", cur);
}
getch();
}
#include<stdio.h>
#include<conio.h>
*
* *
* *
* *
* *
* *
* * * * * *
void drawTriangle(char border, char filler, int length)
{
int start = 2;
int base = 4;
int i, sp, j, b;
for (i = start; i <= length; i++)
{
for (sp = 0; sp <= length - i; sp++)
{
printf(" ");
}
if (i > start)
{
printf("%c ", border);
}
if (i > start)
{
for (b = base; b <= i; b++)
{
printf("%c ", filler);
}
}
printf("%c \n", border);
}
for (j = base; j < length + base; j++)
{
printf("%c ", border);
}
printf("\n");
}
void main()
{
int length = 6;
clrscr();
drawTriangle('*', ' ', length);
getch();
}
52. Program to accept number and print it's factorial.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, fact=1, n;
clrscr();
printf("Enter number : ");
scanf("%d", &n);
for(i=1; i<=n; i++)
{
fact = fact*i;
}
printf("Factorial is: %d", fact);
getch();
}
53. Program to accept number and print if it is prime number or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, n;
clrscr();
printf("Enter number : ");
scanf("%d", &n);
for(i=2; i<=n/2; i++)
{
if(n%i==0)
{
printf("Number is not Prime");
getch();
break;
}
}
printf("Number is Prime");
getch();
}
54. Program to print 'n' prime numbers.
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
int i, j, flag=1, n;
clrscr();
printf("Enter number : ");
scanf("%d", &n);
for(i=2; i<=n; i++)
{
flag=1;
for(j=2; j<=i/2; j++)
{
if(i%j==0)
{
flag=0;
break;
}
}
if(flag==1)
printf("%d\n", i);
}
getch();
}
55. Program to accept a number and print Fibonacci sequence.
#include<stdio.h>
#include<conio.h>
void main()
{
int pre=1, cur=1, temp, i, n;
//pre means previous number
//cur means current number
clrscr();
printf("Enter number : ");
scanf("%d", &n);
printf("%d\t%d", pre, cur);
for(i=3; i<=n; i++)
{
temp = cur;
cur = pre + cur;
pre = temp;
printf("\t%d", cur);
}
getch();
}
0 comments:
Post a Comment