Skip to content

Instantly share code, notes, and snippets.

@GokselKUCUKSAHIN
Created March 26, 2018 05:16
Show Gist options
  • Save GokselKUCUKSAHIN/7f930d2687f4f3d5ac54ac2552f1d012 to your computer and use it in GitHub Desktop.
Save GokselKUCUKSAHIN/7f930d2687f4f3d5ac54ac2552f1d012 to your computer and use it in GitHub Desktop.
Some C Programs Written by Me
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
void Binary(void);
void Octal(void);
void Hexadecimal(void);
void BinaryDecimal(void);
void OctalDecimal(void);
void HexaToDecimal(void);
int main(void)
{
printf("Welcome to Base Convert Tool\n");
int swtch;
do
{
printf("\nSelect :\n1- Decimal to Binary\n2- Decimal to Octal\n3- Decimal to Hexadecimal\n4- Binary to Decimal\n5- Octal to Decimal\n6- Hexadecimal to Decimal\n7- EXIT\n=>");
scanf("%d",&swtch);
switch(swtch){
case 1:{
Binary();
break;
}
case 2:{
Octal();
break;
}
case 3:{
Hexadecimal();
break;
}
case 4:{
BinaryDecimal();
break;
}
case 5:{
OctalDecimal();
break;
}
case 6:{
HexaToDecimal();
break;
}
case 7:{
printf("Good Bye.\n");
system("exit");
break;
}
default:{
printf("Error Code 0x1\n");
}
}
}
while(swtch !=7);
system("pause");
return 0;
}
void Binary(void)
{
int x,a;
int Bin[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //16 karakterlik bir dizi tanımladım
int count=0;
printf("Type Decimal Value to Convert Binary->"); scanf("%d",&x);
a=x; //ilk değeri gecici "a" değerine atadım
do //do-while kullanarak bölüm 0 olana kadar farkı dizinin gerekli hanelerine atadım.
{
Bin[count]=a%2;
a=x/2;
x=a;
count++;
}
while(a!=0);
//atarker tersten atandığı için yazdırırken düz çıkması için tersten yazdırdım.
printf("%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n",Bin[15],Bin[14],Bin[13],Bin[12],Bin[11],Bin[10],Bin[9],Bin[8],Bin[7],Bin[6],Bin[5],Bin[4],Bin[3],Bin[2],Bin[1],Bin[0]);
printf("Press Any key to Continue!");
getch();
// printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b "); //Line Cleaner
printf("\r "); //Line Cleaner
}
void Octal(void)
{
int x,a;
int Bin[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int count=0;
printf("Type Decimal Value to Convert Octal->"); scanf("%d",&x);
a=x;
do
{
Bin[count]=a%8;
a=x/8;
x=a;
count++;
}
while(a!=0);
printf("%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n",Bin[15],Bin[14],Bin[13],Bin[12],Bin[11],Bin[10],Bin[9],Bin[8],Bin[7],Bin[6],Bin[5],Bin[4],Bin[3],Bin[2],Bin[1],Bin[0]);
printf("Press Any key to Continue!");
getch();
// printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b "); //Line Cleaner
printf("\r "); //Line Cleaner
}
void Hexadecimal(void)
{
int x,a,h;
int Bin[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int count=0;
printf("Type Decimal Value to Convert Hexadecimal->"); scanf("%d",&x);
a=x;
do
{
Bin[count]=a%16;
a=x/16;
x=a;
count++;
}
while(a!=0);
int X;
char b;
char hexa[17]="0000000000000000"; //16 yapınca sonuna fazladan bozuk karakter basıyor!!
for(h=0;h<=15;h++)
{
X=Bin[h];
if(X >=0 && X<=9) //arrayden stringe direkt sayı aktarımı yapamadım bende mecbur 0 için '0' yaz vs gibi kodlamak zorunda kaldım.
{ //normalde "hexa[h]=Bin[h];" yazdım bu araklık(0 && 9) için ama çalışmadı.
if(X==0){
b='0';
hexa[h]=b;
}
else if(X==1){
b='1';
hexa[h]=b;
}
else if(X==2){
b='2';
hexa[h]=b;
}
else if(X==3){
b='3';
hexa[h]=b;
}
else if(X==4){
b='4';
hexa[h]=b;
}
else if(X==5){
b='5';
hexa[h]=b;
}
else if(X==6){
b='6';
hexa[h]=b;
}
else if(X==7){
b='7';
hexa[h]=b;
}
else if(X==8){
b='8';
hexa[h]=b;
}
else if(X==9){
b='9';
hexa[h]=b;
}
}
//9dan büyük karakterler için gerekli harflerin atanması.
else if(X == 10){
hexa[h]='A';
}
else if(X == 11){
hexa[h]='B';
}
else if(X == 12){
hexa[h]='C';
}
else if(X == 13){
hexa[h]='D';
}
else if(X == 14){
hexa[h]='E';
}
else if(X == 15){
hexa[h]='F';
}
}
printf("%c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c\n",hexa[15],hexa[14],hexa[13],hexa[12],hexa[11],hexa[10],hexa[9],hexa[8],hexa[7],hexa[6],hexa[5],hexa[4],hexa[3],hexa[2],hexa[1],hexa[0]);
printf("Press Any key to Continue!");
getch();
// printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b "); //Line Cleaner
printf("\r "); //Line Cleaner
}
void BinaryDecimal(void)
{
//int pow(x,y); = x^y.
int Binary[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //16 digit.
char strBinary[17]; //for '\0'.
int i,j,x,y=1,z,Len;
char dummy[1];
gets(dummy);
printf("Type Binary Code-> ");
gets(strBinary);
Len = strlen(strBinary);
// printf("%d\n",Len);
// printf("%s\n",strBinary);
//String to Array Converter and Char Controll.
x=16-Len;
for(j=0;j<Len;j++)
{
// printf("%c ",strBinary[j+1]);
if(strBinary[j] != '0' && strBinary[j] != '1')
{
printf("Error On %-2dth digit of Array!\n",j+1);
y=0;
}
}
if(y==1)
{
for(j=0;j<Len;j++)
{
if(strBinary[j]=='1')
{
Binary[j+x]=1;
}
else if(strBinary[j]=='0')
{
Binary[j+x]=0;
}
else
{
Binary[j+x]=9;
}
}
}
for(i=0;i<16;i++)
{
printf("%d ",Binary[i]);
}
printf("\n");
//CALCULATION PHASE
int decimal=0;
int Base=0;
for(z=0;z<Len;z++)
{
Base=Binary[15-z]*pow(2,z);
// printf("Base=%d, Z=%d\n",Base,z); //control lines
decimal+=Base;
}
printf("DECIMAL = %d\n",decimal);
printf("Press Any key to Continue!");
getch();
// printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b "); //Line Cleaner
printf("\r "); //Line Cleaner
}
void OctalDecimal(void)
{
//int pow(x,y); = x^y.
int Octal[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //16 digit.
char strOctal[17]; //for '\0'.
int i,j,x,y=1,z,Len;
char dummy[1];
gets(dummy);
printf("Type Octal Code-> ");
gets(strOctal);
Len = strlen(strOctal);
// printf("%d\n",Len);
// printf("%s\n",strBinary);
//String to Array Converter and Char Controll.
x=16-Len;
for(j=0;j<Len;j++)
{
// printf("%c ",strBinary[j+1]);
if(strOctal[j] != '0' && strOctal[j] != '1' && strOctal[j] != '2' && strOctal[j] != '3' && strOctal[j] != '4' && strOctal[j] != '5' && strOctal[j] != '6' && strOctal[j] != '7')
{
printf("Error On %-2dth digit of Array!\n",j+1);
y=0;
}
}
if(y==1)
{
for(j=0;j<Len;j++)
{
if(strOctal[j]=='0')
{
Octal[j+x]=0;
}
else if(strOctal[j]=='1')
{
Octal[j+x]=1;
}
else if(strOctal[j]=='2')
{
Octal[j+x]=2;
}
else if(strOctal[j]=='3')
{
Octal[j+x]=3;
}
else if(strOctal[j]=='4')
{
Octal[j+x]=4;
}
else if(strOctal[j]=='5')
{
Octal[j+x]=5;
}
else if(strOctal[j]=='6')
{
Octal[j+x]=6;
}
else if(strOctal[j]=='7')
{
Octal[j+x]=7;
}
else
{
// Octal[j+x]=9;
}
}
}
for(i=0;i<16;i++)
{
printf("%d ",Octal[i]);
}
printf("\n");
int decimal=0;
//CALCULATION PHASE
int Base=0;
for(z=0;z<Len;z++)
{
Base=Octal[15-z]*pow(8,z);
// printf("Base=%d, Z=%d\n",Base,z); //control lines
decimal+=Base;
}
printf("DECIMAL = %d\n",decimal);
printf("Press Any key to Continue!");
getch();
// printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b "); //Line Cleaner
printf("\r "); //Line Cleaner
}
void HexaToDecimal(void)
{
//int pow(x,y); = x^y.
int Hexa[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //16 digit.
char strHexa[17]; //for '\0'.
int i,j,x,y=1,z,Len;
char dummy[1];
gets(dummy);
printf("Type Hexadecimal Code-> ");
gets(strHexa);
Len = strlen(strHexa);
// printf("%d\n",Len);
// printf("%s\n",strBinary);
//String to Array Converter and Char Controll.
x=16-Len;
for(j=0;j<Len;j++)
{
// printf("%c ",strBinary[j+1]);
if(strHexa[j] != '0' && strHexa[j] != '1' && strHexa[j] != '2' && strHexa[j] != '3' && strHexa[j] != '4' && strHexa[j] != '5' && strHexa[j] != '6' && strHexa[j] != '7' && strHexa[j] != '8' && strHexa[j] != '9' && strHexa[j] != 'A' && strHexa[j] != 'B' && strHexa[j] != 'C' && strHexa[j] != 'D' && strHexa[j] != 'E' && strHexa[j] != 'F' && strHexa[j] != 'a' && strHexa[j] != 'b' && strHexa[j] != 'c' && strHexa[j] != 'd' && strHexa[j] != 'e' && strHexa[j] != 'f')
{
printf("Error On %-2dth digit of Array!\n",j+1);
y=0;
}
}
if(y==1)
{
for(j=0;j<Len;j++)
{
if(strHexa[j]=='0')
{
Hexa[j+x]=0;
}
else if(strHexa[j]=='1')
{
Hexa[j+x]=1;
}
else if(strHexa[j]=='2')
{
Hexa[j+x]=2;
}
else if(strHexa[j]=='3')
{
Hexa[j+x]=3;
}
else if(strHexa[j]=='4')
{
Hexa[j+x]=4;
}
else if(strHexa[j]=='5')
{
Hexa[j+x]=5;
}
else if(strHexa[j]=='6')
{
Hexa[j+x]=6;
}
else if(strHexa[j]=='7')
{
Hexa[j+x]=7;
}
else if(strHexa[j]=='8')
{
Hexa[j+x]=8;
}
else if(strHexa[j]=='9')
{
Hexa[j+x]=9;
}
else if(strHexa[j]=='A')
{
Hexa[j+x]=10;
}
else if(strHexa[j]=='a')
{
Hexa[j+x]=10;
}
else if(strHexa[j]=='B')
{
Hexa[j+x]=11;
}
else if(strHexa[j]=='b')
{
Hexa[j+x]=11;
}
else if(strHexa[j]=='C')
{
Hexa[j+x]=12;
}
else if(strHexa[j]=='c')
{
Hexa[j+x]=12;
}
else if(strHexa[j]=='D')
{
Hexa[j+x]=13;
}
else if(strHexa[j]=='d')
{
Hexa[j+x]=13;
}
else if(strHexa[j]=='E')
{
Hexa[j+x]=14;
}
else if(strHexa[j]=='e')
{
Hexa[j+x]=14;
}
else if(strHexa[j]=='F')
{
Hexa[j+x]=15;
}
else if(strHexa[j]=='f')
{
Hexa[j+x]=15;
}
else
{
// x[j+x]=16;
}
}
}
for(i=0;i<16;i++)
{
printf("%d ",Hexa[i]);
}
printf("\n");
int decimal=0;
//CALCULATION PHASE
int Base=0;
for(z=0;z<Len;z++)
{
Base=Hexa[15-z]*pow(16,z);
// printf("Base=%d, Z=%d\n",Base,z); //control lines
decimal+=Base;
}
printf("DECIMAL = %d\n",decimal);
printf("Press Any key to Continue!"); //27 karakter
getch();
// printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b "); //Line Cleaner
printf("\r "); //Line Cleaner
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int asalMi(int);
int RakamYap(char);
int main(void)
{
FILE *Dosya=fopen("LASA.txt","w");
int sayiA,sayiB;
char Rakamlar[7];
int i,j;
for(i=11;i<=9999999;i+=2)
{
sayiA=0;sayiB=0;
sprintf(Rakamlar,"%d",i);
int LenRakam=strlen(Rakamlar);
int Dizi[LenRakam];
int ReverseDizi[LenRakam];
for(j=0;j<LenRakam;j++)
{
Dizi[j]=RakamYap(Rakamlar[j]);
ReverseDizi[LenRakam-1-j]=RakamYap(Rakamlar[j]);
}
for(j=0;j<LenRakam;j++)
{
sayiA+= Dizi[j]*pow(10,(LenRakam-j-1));
sayiB+= ReverseDizi[j]*pow(10,(LenRakam-j-1));
}
if(sayiA>=10)
{
if(asalMi(sayiA)==1 && asalMi(sayiB)==1)
{
fprintf(Dosya,"%-8d Sayisi LASA sayisidir.\n",sayiA);
}
}
}
fclose(Dosya);
return 0;
}
int RakamYap(char CharRakam)
{
if(CharRakam=='0')
return 0;
else if(CharRakam=='1')
return 1;
else if(CharRakam=='2')
return 2;
else if(CharRakam=='3')
return 3;
else if(CharRakam=='4')
return 4;
else if(CharRakam=='5')
return 5;
else if(CharRakam=='6')
return 6;
else if(CharRakam=='7')
return 7;
else if(CharRakam=='8')
return 8;
else if(CharRakam=='9')
return 9;
else
return 0;
}
int asalMi(int Sayi)
{
int i, Asal=1;
for(i=2;i<=sqrt(Sayi);i++)
if(Sayi%i==0)
{
Asal=0;
break;
}
if(Sayi>=2)
return(Asal);
else
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int faktoriel(int);
int main() {
float e=1.0,f;
int j;
for(j=1;j<=10;j++){
f=faktoriel(j);
e+=(1/f);
printf("%.16f\n",e);
}
system("PAUSE");
return 0;
}
int faktoriel(int a){
int f=1,i;
for (i=2;i<=a;i++)
f*=i;
return(f);
}
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int sayi;
int ilk = 0;
int ikinci = 1;
int sonraki;
int c;
printf("Fiboncci sayi dizisinin kac terimini görmek istiyorsunuz? -> ");
scanf("%d",&sayi);
printf("Fibonacci\'nin ilk %d temirimi: \n",sayi);
for (c=0;c<sayi+1;c++)
{
if(c<=1)
{
sonraki=c;
}
else
{
sonraki = ilk + ikinci;
ilk = ikinci;
ikinci = sonraki;
}
if (sonraki != 0)
{
printf("%d\n",sonraki);
}
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// is palindrom??
int main(void)
{
char IsPalin[100];
char Reverse[100];
int Len,i,Test;
printf("IS PALINDROM?(it\'s NOT case sensitive)\n--> ");
gets(IsPalin);
// printf("%s\n",IsPalin);
Len = strlen(IsPalin);
// printf("%d\n",Len);
for(i=0;i<Len;i++)
{
// printf("%d\n",(Len-1)-i);
IsPalin[i]=tolower(IsPalin[i]); //active for non case sensetive
Reverse[(Len-1)-i]=IsPalin[i];
Reverse[Len]='\0'; //son karakteri \0 (null-space) yapmak lazım.
}
// puts(Reverse);
Test=strcmp(IsPalin,Reverse);
if(Test == 0)
printf("\'\'%s\'\' Is a Palindrom Word :D\n",IsPalin);
else
printf("\'\'%s\'\' Is NOT a Palindrom Word :(\n",IsPalin);
system("PAUSE");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main(int argc, char *argv[]) {
bool hello = true;
int i;
double X = 0;
for(i=1;i<99999999999;i+=2)
{
if(hello)
{
X+=(1.0/(i));
hello = false;
}
else
{
X-=(1.0/(i));
hello = true;
}
}
double Pi = X*4.0;
printf("%5.20f\n",Pi);
system("pause");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{
int limit,i,j,asal;
printf("Kaca kadar olan sayilar incelenecek -> "); scanf("%d",&limit);
int Prime=0, NonPrime=0;
for(i=1;i<=limit;i++)
{
asal=1;
for(j=2;j<=sqrt(i);j++)
{
if (i%j==0)
{
asal=0;
break;
}
}
if (asal==0)
{
NonPrime++;
}
else
{
if (i >= 2)
printf("%d\n",i);
Prime++;
}
}
printf("Toplam = %d\n",Prime);
system("PAUSE");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment