Vzorci v programiranju na C - Vrste vzorcev s primeri

Kazalo:

Anonim

Uvod v vzorce v C programiranju

Vzorci v C programiranju, C je postopkovni, splošni programski jezik. Prvič ga je ustvaril med letoma 1969 in 1973 Dennis Ritchie. Glavne značilnosti jezika C so nizki dostop do pomnilnika, preprost nabor ključnih besed in enostavna izvedba. Veliko jezikov, kot so PHP, Java, Javascript itd., Do neke mere sledi značilnostim ali sintaksi C.

Osnovna struktura jezika C je podana na naslednji način

Header #include
Glavni (): int glavni ()
(
Spremenljiva izjava: int x = 12;
Telo: printf ("% d", x);
Vrnitev: vrnitev 0;
)

Primeri vzorcev v C programiranju

V jeziku C so različni vzorci, kot so zvezdni vzorec, številčni vzorci in vzorci znakov. V tem razdelku bomo s primeri pomoči razpravljali, kako ustvariti različne vzorce v jeziku C.

1. Številčni vzorci

V tem razdelku si bomo ogledali, kako tiskati različne vzorce števil v jeziku C

Primer 1: Programirajte na C za tiskanje vzorca piramide števil

V naslednjem programu C lahko uporabnik navede število vrstic za tiskanje vzorca piramide števil, kot želi, rezultat bo prikazan na zaslonu:

#include
#include
int main()
(
int n, x, y, k;
printf("Enter the number of rows to show number pattern: ");
scanf("%d", &n);
for(x =1; x <= n; x++)
(
for(y =1; y <= n; y++)
(
if(y <= x)
printf("%d", y);
else
printf(" ");
)
for(y = n; y >= 1;y--)
(
if(y <= x)
printf("%d", y);
else
printf(" ");
)
printf("\n");
)
return 0;
)

Izhod:

Primer 2: Programirajte na C za tiskanje vzorca številk z diamantom

V naslednjem programu C lahko uporabnik navede število vrstic za tiskanje diamantnega vzorca, kot želi, rezultat bo prikazan na zaslonu

#include
#include
int main()
(
int n, x, y, k;
printf("Enter the number of rows to show number paatern: ");
scanf("%d", &n);
for(x = 1; x <= n; x++)
(
for(y = x; y (
printf(" ");
)
for(k = 1; k < (x*2); k++)
(
printf("%d", k);
)
printf("\n");
)
for(x = 4; x >= 1; x--)
(
for(y = n; y > x; y--)
(
printf(" ");
)
for(k = 1; k < (x*2); k++)
(
printf("%d", k);
)
printf("\n");
)
return 0;
)
#include
#include
int main()
(
int n, x, y, k;
printf("Enter the number of rows to show number paatern: ");
scanf("%d", &n);
for(x = 1; x <= n; x++)
(
for(y = x; y (
printf(" ");
)
for(k = 1; k < (x*2); k++)
(
printf("%d", k);
)
printf("\n");
)
for(x = 4; x >= 1; x--)
(
for(y = n; y > x; y--)
(
printf(" ");
)
for(k = 1; k < (x*2); k++)
(
printf("%d", k);
)
printf("\n");
)
return 0;
)

Izhod:

Primer 3: Programirajte na C za tiskanje vzorca piramide števil

V naslednjem programu C lahko uporabnik navede število vrstic za tiskanje vzorca piramide števil, kot želi, rezultat bo prikazan na zaslonu:

#include
#include
int main()
(
int x, s, n, y = 0, cntr = 0, cntr1 = 0;
printf("Enter the number of rows to show number pattern: ");
scanf("%d", &n);
for(x = 1; x <= n; ++x)
(
for(s = 1; s <= nx; ++s)
(
printf(" ");
++cntr;
)
while(y != 2 * x - 1)
(
if (cntr <= n - 1)
(
printf("%d ", x + y);
++cntr;
)
else
(
++cntr1;
printf("%d ", (x + y - 2 * cntr1));
)
++y;
)
cntr1 = cntr = y = 0;
printf("\n");
)
return 0;
)

Izhod:

Primer 4: Programirajte na C za tiskanje vzorca navpične piramide števil

V naslednjem programu C lahko uporabnik navede število vrstic za tiskanje navpičnega števila piramidnih vzorcev, rezultat bo prikazan na zaslonu

#include
#include
int main()
(
int n, x, y;
printf("Enter the number of rows to show number pattern: ");
scanf("%d", &n);
for(int x = 1; x < n; x++)
(
for(int y = 1; y <= x; y++)
printf("%d", y);
printf("\n");
)
for(int x = n; x >= 0; x--)
(
for(int y = 1; y <= x; y++)
printf("%d", y);
printf("\n");
)
return 0;
)

Izhod:

Primer 5: Programirajte na C za tiskanje vzorca piramide števil

V naslednjem programu C lahko uporabnik navede število vrstic za tiskanje vzorca piramide števil, kot želi, rezultat bo prikazan na zaslonu

#include
#include
int main()
(
int n, x, y;
printf("Enter the number of rows to show number patterns: ");
scanf("%d", &n);
for (int x = n; x >= 0; x--)
(
for (int y = 1; y <= x; y++)
printf("%d", y);
printf("\n");
)
for(int x = 1; x <= n; x++)
(
for(int y = 1; y <= x; y++)
printf("%d", y);
printf("\n");
)
return 0;
)

Izhod:

2. Zvezdni vzorci

V tem razdelku si bomo ogledali, kako tiskati različne vzorce zvezd v jeziku C

Primer 1: Programirajte na C za tiskanje vzorca Star Diamond

V naslednjem programu C lahko uporabnik navede število vrstic za tiskanje vzorca Star Diamond, kot želi, rezultat bo prikazan na zaslonu

#include
#include
int main()
(
int n, s, x, y;
printf("Enter number of rows to show star pattern: ");
scanf("%d", &n);
for(x = 0; x <= n; x++)
(
for(s = n; s > x; s--)
printf(" ");
for(y = 0; y < x; y++)
printf("* ");
printf("\n");
)
for(x = 1; x < n; x++)
(
for(s = 0; s < x; s++)
printf(" ");
for(y = n; y > x; y--)
printf("* ");
printf("\n");
)
return 0;
)

Izhod:

Primer 2: Programirajte na C za tiskanje vzorca navpične krivulje

V naslednjem programu C lahko uporabnik navede število vrstic za tiskanje vzorca navpične krivulje, kot želi, rezultat bo prikazan na zaslonu

#include
#include
int main()
(
int n, x, y;
printf("Enter number of rows to show star pattern: ");
scanf("%d", &n);
for(x = 1; x <= n; x++)
(
for(y = 1; y <= x; y++)
(
printf("*");
)
printf("\n");
)
for(x = n; x >= 1; x--)
(
for(y = 1; y <= x; y++)
(
printf( "*");
)
// ending line after each row
printf("\n");
)
return 0;
)

Izhod:

Primer 3: Programirajte na C, da natisnete vzorec diamantov s votlo številko

V naslednjem programu C lahko uporabnik navede število vrstic za tiskanje votlega diamantnega vzorca, kot želi, rezultat bo prikazan na zaslonu

#include
#include
int main()
(
printf("Enter the number of rows to show the star pattern: ");
int n, x, y, s = 1, k;
scanf("%d", &n);
for(x = 0; x <= n; x++)
(
for(y = n; y > x; y--)
(
printf(" ");
)
printf("*");
if (x > 0)
(
for(k = 1; k <= s; k++)
(
printf(" ");
)
s += 2;
printf("*");
)
printf("\n");
)
s -= 4;
for(x = 0; x <= n -1; x++)
(
for(y = 0; y <= x; y++)
(
printf(" ");
)
printf("*");
for(k = 1; k <= s; k++)
(
printf(" ");
)
s -= 2;
if(x != n -1)
(
printf ("*");
)
//ending line after each row
printf("\n");
)
return 0;
)

Izhod:

Primer 4: Programirajte na C, da natisnete vzorec votlega trikotnika

V naslednjem programu C lahko uporabnik navede število vrstic, da natisne vzorec votlega trikotnika, kot želi, rezultat bo prikazan na zaslonu

#include
#include
int main()
(
int n, x, y, s;
printf("Enter number of rows to show the star pattern: ");
scanf("%d", &n);
for(x = 1; x <= n; x++)
(
//for loop to put space in pyramid
for (s = x; s < n; s++)
printf(" "); //for loop to print star
for(y = 1; y <= (2 * n - 1); y++)
(
if(x == n || y == 1 || y == 2 * x - 1)
printf("*");
else
printf(" ");
)
//ending line after each row
printf("\n");
)
return 0;
)

Izhod:

Primer 5: Programirajte na C, da natisnete vzorec Zvezdni trikotnik

V naslednjem programu C lahko uporabnik navede število vrstic za tiskanje vzorca Zvezdni trikotnik, kot želi, rezultat bo prikazan na zaslonu

#include
#include
int main()
(
int n, s, x, y;
printf("Enter number of rows to show star pattern: ");
scanf("%d", &n);
for(x = 1; x <= n; x++)
(
//for loop to put space
for(s = x; s < n; s++)
printf(" ");
//for loop for displaying star
for(y = 1; y <= x; y++)
printf("* ");
// ending line after each row
printf("\n");
)
return 0;
)

Izhod:

3. Vzorci vzorcev

V tem razdelku si bomo ogledali, kako tiskati različne vzorce znakov v jeziku C

Primer 1: Programirajte na C za tiskanje trikotnega vzorca zaporednih znakov

V naslednjem programu C lahko uporabnik navede število vrstic za tiskanje vzorca Trikotnika Trikotnika zaporednih znakov, kot želi, rezultat bo prikazan na zaslonu

#include
#include
int main()
(
int n, x, y;
printf("Enter number of rows to show character pattern: ");
scanf("%d", &n);
for(x = 1; x <= n; x++)
(
for(y = 1; y <= x; y++)
(
printf("%c", 'A' + y -1);
)
printf("\n");
)
return 0;
)

Izhod:

Primer 2: Programirajte na C, da natisnete Vzorec trikotnika

V naslednjem programu C lahko uporabnik navede število vrstic, da natisne vzorec Trikotnik Trikotnika, kot želi, rezultat bo prikazan na zaslonu

#include
#include
int main()
(
int n, x, y;
printf("Enter number of rows to show character pattern: ");
scanf("%d", &n);
for(x = 1; x <= n; x++)
(
for(y = 1; y <= x; y++)
(
printf("%c", 'A'-1 + x);
)
printf("\n");
)
return 0;
)

Izhod:

Primer 3: Programirajte na C, da natisnete vzorec obrnjenega trikotnika znakov

V naslednjem programu C lahko uporabnik navede število vrstic za tiskanje vzorca obrnjenega trikotnika znakov, kot želi, rezultat bo prikazan na zaslonu

#include
#include
int main()
(
int n, x, y;
printf("Enter number of rows to show character pattern: ");
scanf("%d", &n);
for(x= 1; x <= n; x++)
(
for(y = n; y >= x; y--)
(
printf("%c", 'A'-1 + x);
)
printf("\n");
)
return 0;
)

Izhod:

Priporočeni članek

To je vodnik za vzorce v programiranju C. Tukaj obravnavamo različne številke, zvezde in vzorce znakov s primeri. Če želite izvedeti več, lahko preberete tudi druge naše predlagane članke -

  1. Agilno programiranje
  2. Algoritem v programiranju
  3. Objektno usmerjeno programiranje na Javi
  4. Uvod v zvezde vzorcev na Javi
  5. Vzorci v C #
  6. C Programiranje množenja matrike