الگوریم های مختلف با ++C و C

به نام خدا سلام

ببخشید یکم دیر پست گزاشتم چون یکم درس باید میخوندم ?

ما توی این پست میخوایم چند تا کد های سی و سی پلاس پلاس رو با هم برسی کنیم

ساخت ماشین حساب(++C)

#include <iostream>

using namespace std;


int main(int argc, char** argv) {

float firstNumber, secondNumber, result;

char op;

cout<<"pleas enter the expersion:"<<endl;

cout<<"forexample: 6 * 56"<<endl;

cin>>firstNumber>>op>>secondNumber;

switch(op){

case '+':

result = firstNumber + secondNumber;

break;

case '-':

result = firstNumber - secondNumber;

break;

case '*':

result = firstNumber * secondNumber;

break;

case '/':

result = firstNumber / secondNumber;

break;

default:

cout<<"Error"<<endl;

break;

}

cout<<firstNumber<<" "<<op<<" "<<secondNumber<<" = "<<result;

return 0;

}



میانگین(++C)

#include <iostream>

using namespace std;


int main()

{

int n, i;

float num[100], sum=0.0, average;


cout << "enter the number you want to get Average: ";

cin >> n;


while (n > 100 || n <= 0)

{

cout << "Error! number should in range of (1 to 100)." << endl;

cout << "Enter the number again: ";

cin >> n;

}


for(i = 0; i < n; ++i)

{

cout << i + 1 << ". Enter number: ";

cin >> num[i];

sum += num[i];

}


average = sum / n;

cout << "Average = " << average;


return 0;

}




بمم و کمم(++C)

#include <iostream>

using namespace std;

int main(int argc, char** argv) {

int a, b, n1, n2, temp, lcm;

cout<<"enter number 1 : ";

cin>>a;

cout<<"enter number 2 : ";

cin>>b;

n1 = a;

n2 = b;

while(true){

if(b == 0) break;

temp = a%b;

a = b;

b = temp;

}

lcm = (n1 * n2) / a;

cout<<"the G.C.D of numbers is :"<<a<<endl;

cout<<"the L.C.M of numbers is:"<<lcm;

return 0;

}



تبدیل اعداد به باینری(++C)

#include <iostream>

#include <sstream>

#include <conio.h>

#include <cmath>

#include <string>

#include <stdio.h>

#include <ctime>

#include <windows.h>

using namespace std;


void binary(int number)

{

int num;

if(number == 0)

printf ("0");

else{

binary (number/2);

printf ("%d", number%2);

}

}

int main() {

int number;

printf("enter the number you want to convert to (01010):");

scanf("%d",&number);

printf ("binary: ");

binary(number);

getch();

return 0;

}



فاکتوریل(C)

#include <stdio.h>

#include <conio.h>

unsigned long fact(int) ;

int main()

{

int m ;

printf("\n enter a (+) number:");

scanf("%d", &m);

printf("\n number=%d, factorail = %ld", m, fact(m));

getch();

return 0;

}

unsigned long fact(int x)

{

if(x != 0)

return(x * fact(x - 1)) ;

return 1 ;

}



تشخیص زوج یا فرد بودن اعداد(C)

#include <stdio.h>

int main() {

int num;

printf("enter a number: ");

scanf("%d", &num);

if(num % 2 == 0)

printf("%d is even.", num);

else

printf("%d is odd.", num);

return 0;

}



جذر(رادیکال) (++C)

#include<iostream>

#include<conio.h>

#include<math.h>

using namespace std;

main()

{

float n;

cout<<"please enter a number:";

cin>>n;

cout<<"\n\n"<<"square of "<<n<<" = "<<sqrt(n);

cout<<"\n\n\n"<<"press any key to exit...";

getch();

return 0;

}



محاسبه ی سرعت اجرای کد(++C)

#include <iostream>

#include <ctime>

using namespace std;

int main()

{

clock_t time;

time = clock();

/// Your code

time = clock() - time;

cout << "it took me " << time << " clicks and " << ((float)time)/CLOCKS_PER_SEC << " seconds" << endl ;

return 0;

}



چقدر خدایی نوشتم??

اگر از این آموزش خوشتون اومد حتما حمایت کن عامو

تا جلسه ی بعدی خدانگهدار