c++ to c conversion

closed account (G8k4izwU)
dear members would some one help me to convert the following code from c++ to c ;


===============================================
#include <iostream>
using namespace std;



float momentum(float m , float v)
{
float mom = m * v;
return mom;

}


float work (float f , float d)
{
float wor = f * d;
return wor;

}


float energy (float m , float v)
{

float ene;
ene = 0.5 *(m * (v*v));

return ene;


}


int main ()
{
cout<<"\t\t========select the formulla======== \n";
cout<<"1- Momentum\n";
cout<<"2- Work\n";
cout<<"3- Energy\n";
cout<<"4- Go back\n";
cout<<"\n\n\nYour option [ ]\b\b";



int select ;
float v1 , v2;
cin>>select;

switch (select ){

case 1:
system("cls");
cout<< "Enter mass value : \n";
cin>>v1;
cout<< "Enter velocity : \n";
cin>>v2;
cout<<"Momentum is = "<<momentum(v1,v2)<<" kg-m/sec\n\n";
break;

case 2:
system("cls");
cout<< "Enter force value : \n";
cin>>v1;
cout<< "Enter distance value in meters : \n";
cin>>v2;
cout<<"Work is = "<<work(v1,v2)<<" joule(j)\n\n";
break;


case 3:
system("cls");
cout<< "Enter mass value : \n";
cin>>v1;
cout<< "Enter velocity : \n";
cin>>v2;
cout<<"Energy is = "<<energy(v1,v2)<<" joule(j)\n\n";
break;


case 4 :
system ("cls");

cout<<"========Testahbel enta no back Tshaaah astgfur allah========\n\n";
cout<<"press 'y' to exit\n\n";
char yes ='y';
if(yes == 'y')
break;


}

system("pause");
return 0;
}
===================================================

thanks in advance
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "stdio.h"
#include "stdlib.h"

float momentum(float m , float v){
	return m*v;
}

float work (float f , float d){
	return f * d;
}

float energy (float m , float v){
	return 0.5 *(m * (v*v));
}

int main (void){
	puts("\t\t========select the formulla========");
	puts("1- Momentum");
	puts("2- Work");
	puts("3- Energy");
	puts("4- Go back");
	printf("\n\n\nYour option [ ]\b\b");
	int select;
	float v1, v2;
	char s[100];
	fgets(s,100,stdin);
	sscanf(s,"%d",&select);
	switch (select){
	case 1:
		system("cls");
		puts("Enter mass value : ");
		fgets(s,100,stdin);
		sscanf(s,"%f",&v1);
		puts("Enter velocity : ");
		fgets(s,100,stdin);
		sscanf(s,"%f",&v2);
		printf("Momentum is = %f kg-m/sec\n\n",momentum(v1,v2));
		break;
	case 2:
		system("cls");
		puts("Enter force value : ");
		fgets(s,100,stdin);
		sscanf(s,"%f",&v1);
		puts("Enter distance value in meters : ");
		fgets(s,100,stdin);
		sscanf(s,"%f",&v2);
		printf("Work is = %f joule(j)\n\n",work(v1,v2));
		break;
	case 3:
		system("cls");
		puts("Enter mass value : ");
		fgets(s,100,stdin);
		sscanf(s,"%f",&v1);
		puts("Enter velocity : ");
		fgets(s,100,stdin);
		sscanf(s,"%f",&v2);
		printf("Energy is = %f joule(j)\n\n",energy(v1,v2));
		break;
	case 4 :
		system ("cls");
		puts("========Testahbel enta no back Tshaaah astgfur allah========\n");
		puts("press 'y' to exit\n");
		fgets(s,100,stdin);
		if(s[0] == 'y')
		break;
	}
	system("pause");
	return 0;
}
Could I shortly ask that what's this : %d I've never learnt C before.
That is the int decimal format specifier for the printf and scanf family of functions.
Last edited on
closed account (G8k4izwU)
i really really appreciated . thanks alot fellow
Topic archived. No new replies allowed.