programming like c, c++, java,python learn in free

Thursday, June 4, 2020

C programming Question and Answer Day-11

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();
}
Share:

0 comments:

Post a Comment

Unordered List

recentposts1
Powered by Blogger.

Text Widget

Featured Post

C programming Question and Answer Day-15

71. Program to check Niven number (Harshad number). #include<stdio.h> #include<conio.h> void main() {     int n, d, a, su...

Search This Blog

recent comments

recentcomments

Labels

[slideshow][technology]

vertical posts

[verticalposts][technology]

business

[business][grids]

ad space

ads 600

vehicles

[cars][stack]
[verticalposts][food]

our facebook page

about us

logo

Jupiter is a magazine responsive Blogger template. It has everything you need to make your blog stand out. This template is fully customizable and very flexible and we believe you will love it as much as we do.

Slide show

[people][slideshow]

health

[health][btop]

recent posts

recentposts1

Subscribe Us

random posts

randomposts2

Blog Archive

Recent Posts

top ads

Unordered List

recent

Pages

Theme Support

[socialcounter] [facebook][#][215K] [twitter][#][115K] [youtube][#][215,635] [rss][#][23M] [linkedin][#][21.5K] [instagram][#][600,300]