วันเสาร์ที่ 29 ตุลาคม พ.ศ. 2559

C Diamon 1

#include <math.h>
#include <iostream>
#include <stdio.h>
using namespace std;

int main(int argc, char** args)
{
int n;
printf("Enter n : ");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 2 * n - 1; j++)
{
if (j == (n-1) - i || j == (n - 1) + i)
printf("X");
else
printf("-");
}
printf("\n");
}
for (int i = n - 1-1; i >= 0; i--)
{
for (int j = 0; j < 2 * n - 1; j++)
{
if (j == (n - 1) - i || j == (n - 1) + i)
printf("X");
else
printf("-");
}
printf("\n");
}
return 0;
}

วันพุธที่ 26 ตุลาคม พ.ศ. 2559

C++ Diamon 2

#include <iostream>
#include <string>

using namespace std;
int n;
int main(int argc, char** argv)
{
cout << "Enter number : ";
cin >> n;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n - i; j++)
{
cout << "-";
}

cout << "X";
for (int k = i * 2 - 1; k >1; k--)cout << "-";
cout << "X";
for (int m = 0; m <n - i; m++)cout << "-";
cout << endl;
}
for (int i = n; i >0; i--)
{
for (int j = 1; j <= n - i; j++)
{
cout << "-";
}

cout << "X";
for (int k = i * 2-1 ; k >1; k--)cout << "-";
cout << "X";
for (int m = 0; m <n-i; m++)cout << "-";
cout << endl;
}

return 0;
}

C cross X

#include <iostream>
#include <string>

using namespace std;
int n;
int main(int argc, char** argv)
{
cout << "Enter number : ";
cin >> n;
for (int i = n; i >0; i--)
{
for (int j = 1; j <= n - i; j++)
{
cout << "-";
}

cout << "X";
for (int k = i * 2-1 ; k >1; k--)cout << "-";
cout << "X";
for (int m = 0; m <n-i; m++)cout << "-";
cout << endl;
}
for (int i = 1; i<=n; i++)
{
for (int j = 1; j <= n - i; j++)
{
cout << "-";
}

cout << "X";
for (int k = i * 2 - 1; k >1; k--)cout << "-";
cout << "X";
for (int m = 0; m <n - i; m++)cout << "-";
cout << endl;
}
return 0;
}

C Pyramid 6.2

#include <iostream>
#include <string>

using namespace std;
int n;
int main(int argc, char** argv)
{
cout << "Enter number : ";
cin >> n;
for (int i = n; i > 0; i--)
{
for (int j = 1; j <= n - i; j++)cout << "-";
for (int k = i*2-1; k >0; k--)cout << "X";
cout << endl;
}
return 0;
}

C Pyramid 6.1

#include <iostream>
#include <string>

using namespace std;
int n;
int main(int argc, char** argv)
{
cout << "Enter number : ";
cin >> n;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n - i; j++)cout << "-";
for (int k = 1; k <= i * 2 - 1; k++)cout << "X";
cout << endl;
}
return 0;
}

C Code13

#include <stdio.h>
#include <string>
char c[30];
char n[30];
int main(int argc, char** argv)
{
printf("Enter code: ");
scanf("%s", c);
for(int i=0;i<strlen(c);i++)
{
if (c[i] >= 48 && c[i] <= 57)
n[i] = ((c[i] - 48 + 13) % 10) + 48;
if(c[i]>=65 && c[i]<=90)
n[i] = ((c[i] - 65 + 13) % 26) + 65;
if(c[i] >= 97 && c[i] <= 122)
n[i] = ((c[i] - 97 + 13) % 26) + 97;
printf("%c", n[i]);
}
printf("\n");
return 0;
}

วันอังคารที่ 25 ตุลาคม พ.ศ. 2559

C PI

#include <stdio.h>
float PI = 3.141592;
float pi1;
float pi2, sum = 0;
int count = 0;
int i;
int main(int argc, char** argv)
{
for (i = 1; i<5000000; i += 4)
{
if (PI - sum != 0)
{
pi1 = (float)4 / i;
sum = sum + pi1;
count++;
}
else
{
break;
}
if (PI - sum != 0)
{
pi2 = (float)4 / (i + 2);
sum = sum - pi2;
count++;
}
else
{
break;
}
}
printf("count = %d\n", count);
return 0;
}




วันจันทร์ที่ 24 ตุลาคม พ.ศ. 2559

C Bank Box

#include <stdio.h>
#include <string>
#include <math.h>
class box
{
public:
void nbox(int n)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (i == 1 || i == n || j == 1 || j == n)
printf("*");
else
printf(" ");
}
printf("\n");
}
}
};
int main(int argc,char** args)
{
box b;
b.nbox(20);
return 0;
}


C Diamond

#include <stdio.h>
#include <string>
class diamond
{
public:
void diam(int n,int m)
{
for (int i = 0; i < m; i++)
{
for (int i = 1; i <= n; i++)
{
for (int k = 0; k <= n-i; k++)printf(" ");
for (int x = 0; x <i*2-1; x++)printf("=");
printf("\n");
}
}
for (int i = n - 1; i > 0; i--)
{
for (int k = 0; k <= n - i; k++)printf(" ");
for (int x = 0; x < i * 2 - 1; x++)printf("=");
printf("\n");
}
}
};
int main(int argc,char** args)
{
diamond d;
d.diam(10, 1);
return 0;
}


C Pyramid

#include <stdio.h>
#include <string>
class piramid
{
public:
void pira(int n,int m)
{
for (int i = 0; i < m; i++)
{
for (int i = 1; i <= n; i++)
{
for (int k = 0; k <= n-i; k++)printf(" ");
for (int x = 0; x <i*2-1; x++)printf("=");
printf("\n");
}
}
}
};
int main(int argc,char** args)
{
piramid p;
p.pira(10,1);
return 0;
}


วันอาทิตย์ที่ 23 ตุลาคม พ.ศ. 2559

จำนวนเฉพาะตัวที่ 10001 มีค่าเป็นเท่าไร เมื่อจำนวนเฉพาะตัวแรก คือ 2


#include <stdio.h>
#include <math.h>
int main()
{
    int n = 1000000;
    int count = 0;
    int c=0;
    for(int i=1;i<n+1;i++)
    {
        count = 0;
        for(int j = 1;j<=i;j++)
        {
            if(i%j ==0)
            {
                count++;
            }
        }
        if(count==2)
        {
            c++;
        }
        if(c == 10001)
        {
            printf("%d\n",i);
            break;
        }
    }
    return 0;
}