Uvod u obrasce u C programiranju
Obrasci programiranja C, C je proceduralni opći programski jezik. Prvi put ga je stvorio između 1969. i 1973. Dennis Ritchie. Pristup memoriji na niskoj razini, jednostavan skup ključnih riječi i jednostavna implementacija glavne su značajke jezika C. Postoji mnogo jezika kao što su PHP, Java, Javascript, itd. Koji u određenoj mjeri prate značajke ili sintaksu C.
Osnovna struktura jezika C dan je na sljedeći način
Zaglavlje #include
Glavni (): int glavni ()
(
Izmjenjiva deklaracija: int x = 12;
Tijelo: printf ("% d", x);
Povratak: povratak 0;
)
Primjeri uzoraka u C programiranju
Na jeziku C postoje različiti obrasci poput uzorka zvijezda, brojača i obrazaca znakova. U ovom ćemo dijelu pomoću primjera pomoći razgovarati o tome kako stvoriti različite uzorke na jeziku C.
1. Broj obrasca
U ovom ćemo odjeljku vidjeti kako ispisati različite uzorke brojeva na jeziku C
Primjer 1: Programirajte na C za ispis uzoraka piramide broja
U sljedećem C programu korisnik može unijeti broj redaka za ispis uzoraka piramide broja koliko želi, rezultat će biti 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;
)
Izlaz:
Primjer 2: Programirajte na C za ispis broja dijamanata s brojevima
U sljedećem C programu korisnik može unijeti broj redaka za ispis dijamantskog uzorka kako želi, rezultat će biti 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;
)
Izlaz:
Primjer 3: Programirajte na C za ispis uzoraka piramide broja
U sljedećem C programu korisnik može unijeti broj redaka za ispis uzoraka piramide broja koliko želi, rezultat će biti 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;
)
Izlaz:
Primjer 4: Programirajte na C za ispis uzorka vertikalne piramide s brojevima
U sljedećem C programu korisnik može unijeti broj redaka za ispis vertikalnog uzorka piramide broja koliko želi, rezultat će biti 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;
)
Izlaz:
Primjer 5: Programirajte na C za ispis uzorka piramide broja
U sljedećem C programu korisnik može unijeti broj redaka za ispis uzoraka piramide broja koliko želi, rezultat će biti 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;
)
Izlaz:
2. Zvijezdani obrasci
U ovom ćemo dijelu vidjeti kako ispisati različite uzorke zvijezde na jeziku C
Primjer 1: Programirajte na C za ispis uzorka Star Diamond
U sljedećem C programu korisnik može unijeti broj redaka za ispis uzorka Star Diamond kako želi, rezultat će biti 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;
)
Izlaz:
Primjer 2: Programirajte na C za ispis uzorka vertikalne krivulje
U sljedećem C programu korisnik može unijeti broj redaka za ispis uzorka vertikalne krivulje kako želi, rezultat će biti 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;
)
Izlaz:
Primjer 3: Programirajte na C za ispis uzorka dijamanta šupljeg broja
U sljedećem C programu korisnik može unijeti broj redaka za ispis šupljeg dijamantskog uzorka kako želi, rezultat će biti 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;
)
Izlaz:
Primjer 4: Programirajte na C za ispis uzorka šupljeg trokuta
U sljedećem C programu korisnik može unijeti broj redaka za ispis uzorka šupljeg trokuta koliko želi, rezultat će biti 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;
)
Izlaz:
Primjer 5: Programirajte na C za ispis uzorka Zvijezdani trokut
U sljedećem C programu korisnik može unijeti broj redaka za ispis uzorka Zvijezdani trokut kako želi, rezultat će biti 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;
)
Izlaz:
3. Uzorci likova
U ovom ćemo odjeljku vidjeti kako ispisati različite obrasce znakova na jeziku C
Primjer 1: Programirajte na C za ispis trokutnog uzorka uzastopnih znakova
U sljedećem C programu korisnik može unijeti broj redaka za ispis uzoraka trokuta uzastopnih znakova kako želi, rezultat će biti 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;
)
Izlaz:
Primjer 2: Programirajte C da biste ispisali uzorak trokuta likova
U sljedećem C programu korisnik može unijeti broj redaka za ispis uzoraka s trokutom Znakova u obliku trokuta kako želi, rezultat će biti 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;
)
Izlaz:
Primjer 3: Programirajte na C da biste ispisali obrazac obrnutog trokuta likova
U sljedećem C programu korisnik može unijeti broj redaka za ispis obrazaca s obrnutim trokutom znakova kako želi, rezultat će biti 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;
)
Izlaz:
Preporučeni članak
Ovo je vodič za obrasce u C programiranju. Ovdje ćemo raspraviti različite brojeve, zvijezde i obrasce lika s primjerima. Možete i proći naše druge predložene članke da biste saznali više -
- Agilno programiranje
- Algoritam u programiranju
- Objektno orijentirano programiranje na Javi
- Uvod u zvijezde uzorka na Javi
- Obrasci u C #
- C Programiranje množenja matrice