PDA

View Full Version : C Programmers Help Me!!!!


senewimala1988
02-13-2009, 02:41 PM
Here are two simple exercise on C programming language. Please give me the coding for these program(at least for the second exercise)(The second one is the most important exercise)

Exercise 1:
Write a program to input a temperature reading in either Celsius(c) or Fahrenheit(f) scale and convert it to the other scale. The temperature reading consist of a decimal integer followed by letter “F” or “f” if the temperature is in Fahrenheit scale or letter “C” or “c” if it is in Celsius scale. You may use a similar format for the output of the program.


Note: c=5*(f-32)/9


$ ./a.out
Temperature reading: 100C
212F


$ ./a.out
Temperature reading: 100F
37C


Exercise 2:
Give a date as a triplet of numbers(y,m,d), with y indicating the year, m the month(m=1 for January, m=2 for February, etc.), and d the day of the month, the corresponding day of the week f(f=0 for Sunday, f=1 for Monday, etc.), can be found as follows:


(a)if m<3 let m=m+12 and let y=y-1
(b)let a=2m+6(m+1)/10
(c)let b=y+y/4-y/100+y/400
(d)let f1=d+a+b+1
(e)let f=f1 mod 7
(f)stop.


Write a program that will read a date and print the corresponding day of the week. All divisions indicated above are integer divisions.


:(:(:(

blackarea51
02-13-2009, 02:45 PM
mama danne ova gena

senewimala1988
02-13-2009, 02:53 PM
Kavuruth naddo?????????????

¤--bACarDi--¤
02-13-2009, 02:54 PM
wait...

xcorect
02-13-2009, 02:57 PM
prashne pahadili madi machan
input ekai
umbata wenna ona out put ekak pahadiliwa kiyanna

chamidilk
02-13-2009, 02:59 PM
menna temperature conversion prog eka

#include<stdio.h>
main(){
int c;
int f;
char type;
printf("Temperature reading: ");
scanf("%d%c",&c,&type);
if((type=='c'||type=='C')){
printf("%dF",F(c));
}
else if((type=='f'||type=='F')){
printf("%dC",C(c));
}
else printf("Invalid Entry\nTry Again");
}

int C(int i){
return (5*(i-32)/9);
}
int F(int i){
return (32+9*i/5);
}


menna anith prog eka day eka kiyana eka


#include<stdio.h>
main(){
int y,m,d,a,b,f1,f;
printf("Enter (YYYY-MM-DD) : ");
scanf("%d-%d-%d",&y,&m,&d);
if(m<3){
m+=12;--y;
}
a = 2*m+6*(m+1)/10;
b = y+y/4-y/100+y/400;
f1= d+a+b+1;
f = f1%7;
switch(f){
case 0:printf("Sunday\n");break;
case 1:printf("Monday\n");break;
case 2:printf("Tuesday\n");break;
case 3:printf("Wednesday\n");break;
case 4:printf("Thursday\n");break;
case 5:printf("Friday\n");break;
case 6:printf("Saturday\n");break;
}
}

chamidilk
02-13-2009, 03:00 PM
harida ????
wada neda ?????

¤--bACarDi--¤
02-13-2009, 03:03 PM
My "C" Knowledge sucks...!!

senewimala1988
02-13-2009, 03:05 PM
harida ????
wada neda ?????
I still didnt try machan.. Next week I have a laboratary exercise..These are the questions...
Thanx machan...:D

senewimala1988
02-13-2009, 03:07 PM
prashne pahadili madi machan
input ekai
umbata wenna ona out put ekak pahadiliwa kiyanna
Mata therenneth ne ban!!!
Oka thama prashana tika!!!:lol:

Jack_Sparrow
02-13-2009, 03:08 PM
apoo kammeliy banzzz :S
umbata karanna ona eka onenam kiyannam
umba program eka liyaganin

senewimala1988
02-13-2009, 03:09 PM
harida ????
wada neda ?????
Umba maara man kenek ne!!!!
umba mona group ekeda???
mama A4 eke!!!

chamidilk
02-13-2009, 03:09 PM
adee mamath umbe batch eke thama(UOM`08)

group eka mokakda kiyanna balanna ......

try 2 understand those progs
those r simple men

chamidilk
02-13-2009, 03:10 PM
mama A2 macho

umba wenuwenmai oya progs deka haduwe ...

modifications karaganin one nam

chamidilk
02-13-2009, 03:13 PM
macho ara day eka kiyana prog eke input eka mama dunne me widiyata honde
look at dz instance
ex-: input-----> 2009-2-13
output-----> Friday

¤--bACarDi--¤
02-13-2009, 03:13 PM
menna temperature conversion prog eka

#include<stdio.h>
main(){
int c;
int f;
char type;
printf("Temperature reading: ");
scanf("%d%c",&c,&type);
if((type=='c'||type=='C')){
printf("%dF",F(c));
}
else if((type=='f'||type=='F')){
printf("%dC",C(c));
}
else printf("Invalid Entry\nTry Again");
}

int C(int i){
return (5*(i-32)/9);
}
int F(int i){
return (32+9*i/5);
}


menna anith prog eka day eka kiyana eka


#include<stdio.h>
main(){
int y,m,d,a,b,f1,f;
printf("Enter (YYYY-MM-DD) : ");
scanf("%d-%d-%d",&y,&m,&d);
if(m<3){
m+=12;--y;
}
a = 2*m+6*(m+1)/10;
b = y+y/4-y/100+y/400;
f1= d+a+b+1;
f = f1%7;
switch(f){
case 0:printf("Sunday\n");break;
case 1:printf("Monday\n");break;
case 2:printf("Tuesday\n");break;
case 3:printf("Wednesday\n");break;
case 4:printf("Thursday\n");break;
case 5:printf("Friday\n");break;
case 6:printf("Saturday\n");break;
}
}


Hey nice machan...Are u studing @ SLIIT or sumwhere...?

madurax86
02-13-2009, 03:14 PM
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
int y,m,d,f1,f,a,b;

cin>>y;
cin>>m;
cin>>d;
if (m<3) {
m=m+12;
y=y-1;
}
a=2*m+6*(m+1)/10;
b=y+y/4-y/100+y/400;
f1=d+a+b+1;
f=f1 % 7;
switch (f){
case 0:cout<<"Sunday";
case 1:cout<<"Monday";
case 2:cout<<"Tuesday";
case 3:cout<<"Wednesday";
case 4:cout<<"Thursday";
case 5:cout<<"Friday";
case 6:cout<<"Saturday";
}
system("PAUSE");
return EXIT_SUCCESS;
}



Meka C++ eth convert karagana puluwan ne 2ma eka wage ne tikak printf ui scanf ui witarai man hithanne wenasa manda man C danne na C++ pamanai

senewimala1988
02-13-2009, 03:16 PM
macho ara day eka kiyana prog eke input eka mama dunne me widiyata honde
look at dz instance
ex-: input-----> 2009-2-13
output-----> Friday
Ela 1st eka wada...Modifications kala...
will try the 2nd one..
Thanx!!!!

senewimala1988
02-13-2009, 03:18 PM
mama A2 macho

umba wenuwenmai oya progs deka haduwe ...

modifications karaganin one nam
umba nikan program 2 danne ne wage ne!!!:confused:Meka next week submit karanna one ne!!

chamidilk
02-13-2009, 03:18 PM
no macho I study in University of Moratuwa ....
Why did you ask me whether I studied in SLIIT ..... ????
macho CPP wala oya switch case eka ain karala kelinma String array ekak ganna puluwan neda ?????

madurax86
02-13-2009, 03:19 PM
macho ara day eka kiyana prog eke input eka mama dunne me widiyata honde
look at dz instance
ex-: input-----> 2009-2-13
output-----> Friday

me c++ wala cin ekata input format karana puluwan da?

senewimala1988
02-13-2009, 03:21 PM
macho ara day eka kiyana prog eke input eka mama dunne me widiyata honde
look at dz instance
ex-: input-----> 2009-2-13
output-----> Friday
Ela ela 2nd ekath wada...........
Thank you macho.....................

madurax86
02-13-2009, 03:22 PM
no macho I study in University of Moratuwa ....
Why did you ask me whether I studied in SLIIT ..... ????
macho CPP wala oya switch case eka ain karala kelinma String array ekak ganna puluwan neda ?????

ow eka mathak une na :P tama learning ne hehe

¤--bACarDi--¤
02-13-2009, 03:26 PM
no macho I study in University of Moratuwa ....
Why did you ask me whether I studied in SLIIT ..... ????
macho CPP wala oya switch case eka ain karala kelinma String array ekak ganna puluwan neda ?????

Ne banz SLIIT eketh me dawas wala owa thamai karanney...;)
Uba Moratuwa Software Engeneering wenna one...!!!
1st year or 2nd Year...?

chamidilk
02-13-2009, 03:27 PM
mama danna widiyata CIN wala formatting baha
mama thama L1
eeth hithan inne tronix karanna

madurax86
02-13-2009, 03:29 PM
mama danna widiyata CIN wala formatting baha
mama thama L1
eeth hithan inne tronix karanna

k thanks formatting karagana ehenam string.h include karanawei wage :)

VIDUrUVAN123
02-13-2009, 03:32 PM
machang try to lean your self these are the most eassy programs in C

machang first try to write pseudo codes and take a logic behind it

then code machang

Then put the codes we will help you

chamidilk
02-13-2009, 03:34 PM
ow umbata puluwan nam ee prog eka methana danna balanna
c whether my String array declaration in cpp is correct

........
........
#include<string.h>
int main(){
.............
.............
........
String s[]= {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};

senewimala1988
02-13-2009, 03:39 PM
machang try to lean your self these are the most eassy programs in C

machang first try to write pseudo codes and take a logic behind it

then code machang

Then put the codes we will help you
umbanam kiyai..Mama next week submit karanna oni.........

chamidilk
02-13-2009, 03:57 PM
naha macho api pseudo code walin programming start eka 1st semester waladi gatta ne ????
umba oya Lab Notes wala thiyena ewwa mula idan gahala purudu wenna kohomada program flow eka kiyala
anith eka syntax tikath balaganna

senewimala1988
02-13-2009, 04:13 PM
naha macho api pseudo code walin programming start eka 1st semester waladi gatta ne ????
umba oya Lab Notes wala thiyena ewwa mula idan gahala purudu wenna kohomada program flow eka kiyala
anith eka syntax tikath balaganna
Mchhho mama godak durata pracice kala...Apita kohomath lecture ekakuth miss vuna wednesday holiday nisa..Eka nam wadakuth ne...
Mata ara temperature eka haduwa..But eka two times input karanna oni...1st either celsius or fahrenheit..2nd the temperature..
Mata 2nd eka nam therune ne..
1st ekath thaniyen practice karala thama hada gaththe..Eth oya dunnu vidiye advance yanna mage knowledge eka madi!!!

/*Program-4.1*/
#include<stdio.h>
int main()
{
char temp;
float C,F;
printf("What is your input type(C/F)?");
scanf("%c",&temp);
if(temp=='C')
{
printf("Type your temperature in Celsius: ");
scanf("%f",&C);
F=(9*C/5)+32;
printf("Temprature reading: %.2f",F);
printf(" F\n");
}
else if(temp=='F')
{
printf("Type your temperature in Farenheit: ");
scanf("%f",&F);
C=5*(F-32)/9;
printf("Temperature Reading: %.2f",C);
printf(" C\n");
}
else
printf("STOP! Wrong input\n");
return 0;
}

chamidilk
02-13-2009, 04:22 PM
okath ithin aulak nahane .... ???

mama deepu prog eke umbata ewelawema denna puluwan Celsius da Farenheit da kiyala ???

ai mage prog eka therenne naththe ???
mokakda aula

DJ ASHAN
02-13-2009, 04:23 PM
mokadda meeeee

senewimala1988
02-13-2009, 04:32 PM
okath ithin aulak nahane .... ???

mama deepu prog eke umbata ewelawema denna puluwan Celsius da Farenheit da kiyala ???

ai mage prog eka therenne naththe ???
mokakda aula
Avulak ne ban...Mama thaama if conditions valin ehaata thaaama paadam kale ne...
E hinda tikak advance yanna amaarui..
Eth avulak ne..
Mama JAVA yaantham karala thiyenavaa..
Eka hinda tikak therum ganna puluwan...

chamidilk
02-13-2009, 04:33 PM
#include<stdio.h>
main(){
int c;
char type;
printf("Temperature reading: ");
scanf("%d%c",&c,&type);
if((type=='c'||type=='C')){
printf("%dF",F(c));
}
else if((type=='f'||type=='F')){
printf("%dC",C(c));
}
else printf("Invalid Entry\nTry Again");
}

int C(int i){
return (5*(i-32)/9);
}
int F(int i){
return (32+9*i/5);
}


mokakda aula
compile wenne nadda ????

senewimala1988
02-13-2009, 04:39 PM
#include<stdio.h>
main(){
int c;
char type;
printf("Temperature reading: ");
scanf("%d%c",&c,&type);
if((type=='c'||type=='C')){
printf("%dF",F(c));
}
else if((type=='f'||type=='F')){
printf("%dC",C(c));
}
else printf("Invalid Entry\nTry Again");
}

int C(int i){
return (5*(i-32)/9);
}
int F(int i){
return (32+9*i/5);
}

mokakda aula
compile wenne nadda ????
Ayyo banz avulak ne!!!
Eka elakiri wage wada..

chamidilk
02-13-2009, 04:40 PM
Ayyo banz avulak ne!!!
Eka elakiri wage wada..

eka misak mama baluwa moko kiyala

senewimala1988
02-13-2009, 04:44 PM
eka misak mama baluwa moko kiyala
he he
I will meet u next week...BYE!!!

chamidilk
02-14-2009, 02:50 PM
I found tht de program u all asked me wz based on basic control structures .
bt de program u were all given first wz highly cohesive n readable ....
bt i thought all hd no clear idea of functions
here is de new prog

#include<stdio.h>
main(){
int i;
char type;
printf("Temperature reading: ");
scanf("%d%c",&i,&type);
if((type=='c'||type=='C')){
printf("%dF",(32+9*i/5));
}
else if((type=='f'||type=='F')){
printf("%dC",(5*(i-32)/9));
}
else printf("Invalid Entry\nTry Again");
}

x-pert
02-14-2009, 03:21 PM
Explanation:

#include<stdio.h>
main(){
int i;
char type;
printf("Temperature reading: ");
scanf("%d%c",&i,&type);
// %d kiyana eken capture karanne INTEGER values.
// %c kiyana eken capture karanne CHAR values.
// ethakota oya uda line eken wenne, keyboard input eke thiyana integer values tika
// assign wenawa 'i' kiyana variable ekata. and...
// Char values tika assign wenawa 'type' kiyana variable ekata.

if((type=='c'||type=='C')){
printf("%dF",(32+9*i/5));
// Enter karapu eke 'type' eka c or C nam, do the above calculation to 'i'
}


else if((type=='f'||type=='F')){
printf("%dC",(5*(i-32)/9));
}
// Enter karapu eke 'type' eka f or F nam, do the above calculation to 'i'


else printf("Invalid Entry\nTry Again");
}

chamidilk
02-14-2009, 03:22 PM
1st solution(dz 1 z mine)-dz prog z de fastest 1 in investigating a prime

#include<stdio.h>
main(){
int i,s,ip;
scanf("%d",&s);
while(s!=-1){
if(s==2)printf("prime\n");
else if(s%2==0);
else {
if(s!=1){
ip=1;
for(i=2;i*i<=s;i++){
if(s%i==0){
ip=-1;
break;
}
}
if(ip==1)printf("prime\n");
}
}
scanf("%d",&s);
}
}

2nd solution which u all like very much

#include<stdio.h>
main(){
int s,i,j,ip;
scanf("%d",&s);
while(s!=-1){
if(s==2||s==3)printf("prime\n");
else{
if(s!=1){
ip=1;
for(j=3;j<s;j+=1){
if(s%j==0){
ip=-1;break;
}
}
if(ip==1)printf("prime\n");
}
}
scanf("%d",&s);
}
}

madurax86
02-14-2009, 03:49 PM
C++ version, C paranai ne meka kochara simpla plan ekak da? :P


#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
int i,n;
bool isprime;
cin>>n;
isprime=true;
i=abs(n); //neglect negative numbers
n=i;
while (i>2) {
i--;
if (n%i==0) {isprime=false; break;} ;
}
if (isprime==1) cout<<"Prime"<<endl; else cout<<"Not prime"<<endl;

system("PAUSE");
return EXIT_SUCCESS;
}

madurax86
02-14-2009, 03:49 PM
double post, due to server load srry :P

chamidilk
02-14-2009, 04:02 PM
Exercise 2-

#include<stdio.h>
main(){
int i,j,k;
scanf("%d",&k);
while(k!=-1){
for(i=1;i<k;i++){
if(k%i==0)j+=i;
}
if(j==k)printf("perfect\n");
j=0;
scanf("%d",&k);
}
}

Exercise 3-

#include<stdio.h>
main(){
int i,j;
for(j=0;j<5;j++){
for(i=6;i>j;i--)printf("$");
printf("\n");
}
printf("$\n");
for(j=0;j<5;j++){
for(i=0;i<j+2;i++)printf("$");
printf("\n");
}
}

chamidilk
02-14-2009, 04:11 PM
C++ version, C paranai ne meka kochara simpla plan ekak da? :P


#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
int i,n;
bool isprime;
cin>>n;
isprime=true;
i=abs(n); //neglect negative numbers
n=i;
while (i>2) {
i--;
if (n%i==0) {isprime=false; break;} ;
}
if (isprime==1) cout<<"Prime"<<endl; else cout<<"Not prime"<<endl;

system("PAUSE");
return EXIT_SUCCESS;
}



i appreciate your postings here
please bro
cn u please correct ur code and put t back here
my uni mates will be confused in ur prog

madurax86
02-14-2009, 04:14 PM
i appreciate your postings here
please bro
cn u please correct ur code and put t back here
my uni mates will be confused in ur prog

correct what? it works fine with mingw32 compiler its C++ not C :)

chamidilk
02-14-2009, 04:20 PM
correct what? it works fine with mingw32 compiler its C++ not C :)

r u sure check t back n tell me

madurax86
02-14-2009, 04:32 PM
r u sure check t back n tell me
sure dath kiyala ahanawa

http://i433.photobucket.com/albums/qq55/madurax86/df.jpg

chamidilk
02-14-2009, 04:39 PM
check whether 2 is a prime or not using your prog .......
I am sure your prog hz a mistake

http://i301.photobucket.com/albums/nn43/Djdil_2008/prime.jpg

madurax86
02-14-2009, 04:52 PM
check whether 2 is a prime or not using your prog .......
I am sure your prog hz a mistake


I didnt change anything it works well man :P i edited the code some while ago(before you said it has a bug) to make it shorter after posting in EK u may have copied the program then copy it now and see

I copied the code from EK then compiled a new project same result(this is the exact code that u quoted)
http://i433.photobucket.com/albums/qq55/madurax86/dzf-2.jpg

Just by looking at the code you can say that 2 is never being passed thru the algorithm only numbers greater than 2, i=2 is bypassed and returns prime, its the same for 1 too

chamidilk
02-14-2009, 05:11 PM
hmmmmm
fine ur prog z correct ....

sorry .......i dint get tht de value n .... actually by mistake t wz commented
thts y t did an error

bt still thr z n error

t gives u 1 is a prime ...
z t correct my dear ???????
1 is nt a prime although ts facors r 1 n tself only

n next error z u r investigating whether even numbers r primes xcluding 2 .......

bt u r doing a very gud contribution 2 my C knowledge n my mates ....
keep posting

chamidilk
02-14-2009, 07:16 PM
C++ version, C paranai ne meka kochara simpla plan ekak da? :P


#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
int i,n;
int isprime;
cin>>n;
isprime=true;
i=abs(n); //neglect negative numbers
n=i;
while (i>2) {
if(n%2==0){isprime=-1;break;}
i--;
if (n%i==0) {isprime=false; break;} ;
}
if (isprime==1&&n!=1) cout<<"Prime"<<endl; else cout<<"Not prime"<<endl;

system("PAUSE");
return EXIT_SUCCESS;
}




meke 1 prime ekak widiyata ganne naha
anika even numbers >2 wil nt b processed

madurax86
02-14-2009, 07:18 PM
hmmmmm
fine ur prog z correct ....

sorry .......i dint get tht de value n .... actually by mistake t wz commented
thts y t did an error

bt still thr z n error

t gives u 1 is a prime ...
z t correct my dear ???????
1 is nt a prime although ts facors r 1 n tself only

n next error z u r investigating whether even numbers r primes xcluding 2 .......

bt u r doing a very gud contribution 2 my C knowledge n my mates ....
keep posting

i did think that 1 is a prime when i was coding :P it should be it is dividable by 1 and itself which we see as a common thing in all other prime numbers, its just a definition not actual math. I didnt now the definition at the time of coding and i thought of the common factor in all primes and included 1 as a prime too
The program is only made for integers not real numbers so excluding 2 and even 3 would not result in a wrong output because isprime is assigned true at the beginning and if some number was lesser than 2 then it would do nothing, the while would not execute and the answer will be delivered through a shortcut you can put i>3 also it wont result in wrong outputs it just uses the shortcut for those numbers that we have provided.
If you are interested on programming, check out the calc i made its in my blog i made it with a friend of mine. :D think of the shortcuts before you start coding that might make ur prog a lot faster and that is what we all need.
\/
\/
\/

chamidilk
02-14-2009, 07:24 PM
hmmm
gud i also like optimizing progz .....
hv u hd a look on ma 1st prime investigating prog ...
t z a marvellous optimization of prime investigation ....
actually i cn do t verywell bt our newbies wil b confused looking thm @ once ...

cn u pls let me know de link of ur calc .....

mata ewanawada eke source code eka ....
oya java dannawada ???
mamath java walin calc ekak haduwa .....

[email protected]

chamidilk
02-14-2009, 07:30 PM
marai ah eeee calc eka
aiye mata eke source eka e-mail karannako
eka VB walinda haduwe ??????

madurax86
02-14-2009, 07:32 PM
meke 1 prime ekak widiyata ganne naha
anika even numbers >2 wil nt b processed

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
int i,n;
int isprime;
cin>>n;
isprime=true;
i=abs(n); //neglect negative numbers
n=i;
while (i>2) {
if(n%2==0){isprime=-1;break;}
i--;
if (n%i==0) {isprime=false; break;} ;
}
if (isprime==1&&n!=1) cout<<"Prime"<<endl; else cout<<"Not prime"<<endl;

system("PAUSE");
return EXIT_SUCCESS;
}


sure i did check even numbers but dude y put the check inside the while loop ??? this will be slow as my original code :P
put it like this to get it faster


#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
int i,n;
int isprime;
cin>>n;
isprime=1;
i=abs(n); //neglect negative numbers
n=i;
if (n%2==0 && n!=2) isprime=-1;
while (i>2 && isprime==1) {
i--;
if (n%i==0) {isprime=0; break;} ;
}
if (isprime==1&&n!=1) cout<<"Prime"<<endl; else cout<<"Not prime"<<endl;

system("PAUSE");
return EXIT_SUCCESS;
}

madurax86
02-14-2009, 07:34 PM
marai ah eeee calc eka
aiye mata eke source eka e-mail karannako
eka VB walinda haduwe ??????

yea fully VB, graph supports graphical differentiation and integration ;p mage pixu wada mona aiya da ane man 2008 AL mora ringa gana zscore madi UCSC yanawa hambunoth :P
source eka denam commercial wada ba

chamidilk
02-14-2009, 07:34 PM
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
int i,n;
int isprime;
cin>>n;
isprime=true;
i=abs(n); //neglect negative numbers
n=i;
while (i>2) {
if(n%2==0){isprime=-1;break;}
i--;
if (n%i==0) {isprime=false; break;} ;
}
if (isprime==1&&n!=1) cout<<"Prime"<<endl; else cout<<"Not prime"<<endl;

system("PAUSE");
return EXIT_SUCCESS;
}


sure i did check even numbers but dude y put the check inside the while loop ??? this will be slow as my original code :P
put it like this to get it faster


#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
int i,n;
int isprime;
cin>>n;
isprime=1;
i=abs(n); //neglect negative numbers
n=i;
if (n%2==0 && n!=2) isprime=-1;
while (i>2 && isprime==1) {
i--;
if (n%i==0) {isprime=0; break;} ;
}
if (isprime==1&&n!=1) cout<<"Prime"<<endl; else cout<<"Not prime"<<endl;

system("PAUSE");
return EXIT_SUCCESS;
}



yes i agree
mata arake source eka e-mail karannako

madurax86
02-14-2009, 07:36 PM
yes i agree
mata arake source eka e-mail karannako
source eka 20MB witara wenawa man heta dihata mage yaluwagen ahala upload karanam kamak na ne :)

truth4L
02-14-2009, 07:51 PM
onna java num podi help ekak denna tibba...:P:P
C gena melo deyak danne nehe:no::no:

chamidilk
02-15-2009, 05:22 AM
kkkk
t dznt matter f u dnt lik
dz dayz i m developing 1 which hz a diff n an int engine .....bt t z based on java
bt t z nt tht much eZ

look @ dz

#include<stdio.h>
int main(){
int i,j,s,ip;
scanf("%d",&s);
ip=1;
j=s;
i=2;
while(s>2&&i*i<=s){
if(s%i==0||j%2==0){ip=-1;break;}
i++;
}
if(ip==1&&j!=1)printf("prime\n");else printf("Not a prime");
return 0;
}

hik hik de fastest

madurax86
02-15-2009, 09:22 AM
kkkk
t dznt matter f u dnt lik
dz dayz i m developing 1 which hz a diff n an int engine .....bt t z based on java
bt t z nt tht much eZ

look @ dz

#include<stdio.h>
int main(){
int i,j,s,ip;
scanf("%d",&s);
ip=1;
j=s;
i=2;
while(s>2&&i*i<=s){
if(j%2==0||s%i==0){ip=-1;break;}
i++;
}
if(ip==1&&j!=1)printf("prime\n");else printf("Not a prime");
return 0;
}

hik hik de fastest

Shorter code doesnt mean its fast! you're still using the even check inside the while loop j doesnt change with i so why did you put it inside the while loop? theres no need of checking the same number whether its even or not again and again?, you make it faster by other things but make it alot slower by this

x-pert
02-15-2009, 10:54 AM
kkkk
t dznt matter f u dnt lik
dz dayz i m developing 1 which hz a diff n an int engine .....bt t z based on java
bt t z nt tht much eZ

look @ dz

#include<stdio.h>
int main(){
int i,j,s,ip;
scanf("%d",&s);
ip=1;
j=s;
i=2;
while(s>2&&i*i<=s){
if(j%2==0||s%i==0){ip=-1;break;}
i++;
}
if(ip==1&&j!=1)printf("prime\n");else printf("Not a prime");
return 0;
}
hik hik de fastest

You're using a while loop. Hence slower in performance than the earlier one :D

chamidilk
02-15-2009, 12:44 PM
Shorter code doesnt mean its fast! you're still using the even check inside the while loop j doesnt change with i so why did you put it inside the while loop? theres no need of checking the same number whether its even or not again and again?, you make it faster by other things but make it alot slower by this

i know t dear :P
cn`t u understand y i say dz z de fastest ....
c my prog calculates using only perfect squares upto de given number
ur prog cn`t reach my prog`s speed ....
i cn put de condition j%2 in an if block bt i dnt lik t ....
there z a reason ...
actually in ur prog also isprime is always checked until t bcomes -1 .....:P

madurax86
02-15-2009, 12:54 PM
You're using a while loop. Hence slower in performance than the earlier one :D

i;ve told him twice to use the even check for j before the while but no point :P eya asa na lu but he says his prog is fast you know anyway that we can get tick count in c++?

chamidilk
02-15-2009, 01:00 PM
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
int i,n;
int isprime;
cin>>n;
isprime=1;
i=abs(n); //neglect negative numbers
n=i;
if (n%2==0 && n!=2) isprime=-1;
while (i>2 && isprime==1) {
i--;
if (n%i==0) {isprime=0; break;} ;
}
if (isprime==1&&n!=1) cout<<"Prime"<<endl; else cout<<"Not prime"<<endl;

system("PAUSE");
return EXIT_SUCCESS;
}


wht do u say ????
i know de point there

i dnt say thr z no point

:P :P :P :P :P :P :P

madurax86
02-15-2009, 01:02 PM
i know t dear :P
cn`t u understand y i say dz z de fastest ....
c my prog calculates using only perfect squares upto de given number
ur prog cn`t reach my prog`s speed ....
i cn put de condition j%2 in an if block bt i dnt lik t ....
there z a reason ...
actually in ur prog also isprime is always checked until t bcomes -1 .....:P

i can understand that thats why i said u can make it faster by adding things but you let the even check be in the while loop is its slower than you think trust me i have been programming for 6 years :P

madurax86
02-15-2009, 01:04 PM
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
int i,n;
int isprime;
cin>>n;
isprime=1;
i=abs(n); //neglect negative numbers
n=i;
if (n%2==0 && n!=2) isprime=-1;
while (i>2 && isprime==1) {
i--;
if (n%i==0) {isprime=0; break;} ;
}
if (isprime==1&&n!=1) cout<<"Prime"<<endl; else cout<<"Not prime"<<endl;

system("PAUSE");
return EXIT_SUCCESS;
}


wht do u say ????
i know de point there

i dnt say thr z no point

:P :P :P :P :P :P :P

no you dont and you still dont get it, im talking about the part thats bold :P

#include<stdio.h>
int main(){
int i,j,s,ip;
scanf("%d",&s);
ip=1;
j=s;
i=2;
while(s>2&&i*i<=s){
if(j%2==0||s%i==0){ip=-1;break;}
i++;
}
if(ip==1&&j!=1)printf("prime\n");else printf("Not a prime");
return 0;
}

chamidilk
02-15-2009, 01:13 PM
no you dont and you still dont get it, im talking about the part thats bold :P

#include<stdio.h>
int main(){
int i,j,s,ip;
scanf("%d",&s);
ip=1;
j=s;
i=2;
while(s>2&&i*i<=s){
if(j%2==0||s%i==0){ip=-1;break;}
i++;
}
if(ip==1&&j!=1)printf("prime\n");else printf("Not a prime");
return 0;
}



i got t dear bt tht code z urs ... nt mine
checking isprime always n used break keyword 4 wht ?????
check ur prog :P
:P
macho optimizing a prog z nt an eZ task
api methana wada karana eke therumak naha
mama post kale mage friendslata therenna ona widiyata
dan mama dala thiyena ewawath oya dala thiyena ewawath ape kattiyata therenne naha ......
mokada kattiya C walata aluth anika oya CPP

oya cal ekak haduwa kiwwa neda ??????
oyata eka hadanna dawas keeyak giyada ?????
eken wadima hariya giye logic ekatada ???? optimization walatada ?????

madurax86
02-15-2009, 01:28 PM
i got t dear bt tht code z urs ... nt mine
checking isprime always n used break keyword 4 wht ?????
check ur prog :P
:P
macho optimizing a prog z nt an eZ task
api methana wada karana eke therumak naha
mama post kale mage friendslata therenna ona widiyata
dan mama dala thiyena ewawath oya dala thiyena ewawath ape kattiyata therenne naha ......
mokada kattiya C walata aluth anika oya CPP

oya cal ekak haduwa kiwwa neda ??????
oyata eka hadanna dawas keeyak giyada ?????
eken wadima hariya giye logic ekatada ???? optimization walatada ?????

this is my code

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
int i,n;
int isprime;
cin>>n;
isprime=1;
i=abs(n); //neglect negative numbers
n=i;
if (n%2==0 && n!=2) isprime=-1;
while (i>2 && isprime==1) {
i--;
if (n%i==0) {isprime=0; break;} ;
}
if (isprime==1&&n!=1) cout<<"Prime"<<endl; else cout<<"Not prime"<<endl;

system("PAUSE");
return EXIT_SUCCESS;
}

see i check for it on the beginning i may not try to optimize code be c++ is fast it self but i dont to things like that for sure ...dont edit my code inside quotes and post it looks like i actually posted it :P i didnt u edited it on your post above is my code if u want do a benchmark im sure that ur code isnt must faster than this because u still use the even check inside the while loop :P

chamidilk
02-15-2009, 01:44 PM
hikz hikz karala balanna benchmark test eka mage code ekai oyage code ekai ...
definitely u wil lose ...
my code z fastest still u cnt understand wht my prog dz actually

umbe code eke thiyena mage wagema aula thama mama pennuwe ....

NIPUNAYA
02-15-2009, 01:49 PM
Here are two simple exercise on C programming language. Please give me the coding for these program(at least for the second exercise)(The second one is the most important exercise)

Exercise 1:
Write a program to input a temperature reading in either Celsius(c) or Fahrenheit(f) scale and convert it to the other scale. The temperature reading consist of a decimal integer followed by letter “F” or “f” if the temperature is in Fahrenheit scale or letter “C” or “c” if it is in Celsius scale. You may use a similar format for the output of the program.


Note: c=5*(f-32)/9


$ ./a.out
Temperature reading: 100C
212F


$ ./a.out
Temperature reading: 100F
37C


Exercise 2:
Give a date as a triplet of numbers(y,m,d), with y indicating the year, m the month(m=1 for January, m=2 for February, etc.), and d the day of the month, the corresponding day of the week f(f=0 for Sunday, f=1 for Monday, etc.), can be found as follows:


(a)if m<3 let m=m+12 and let y=y-1
(b)let a=2m+6(m+1)/10
(c)let b=y+y/4-y/100+y/400
(d)let f1=d+a+b+1
(e)let f=f1 mod 7
(f)stop.


Write a program that will read a date and print the corresponding day of the week. All divisions indicated above are integer divisions.


:(:(:(

Saahena bonda oone equation ekak wagei penne :/

madurax86
02-15-2009, 01:57 PM
hikz hikz karala balanna benchmark test eka mage code ekai oyage code ekai ...
definitely u wil lose ...
my code z fastest still u cnt understand wht my prog dz actually

haha due u r funny do u think others are dumb brutes ? very funny if u insist
what you are doing is checking only until the biggest number that can divide a number with 0 as remainder the biggest number that can do is, is the square root of the number so what is there to think so much? theres no point in dividing by numbers higher than the square root of the number, why did i not use it? because i didnt want to optimize the code unlike you boasting about how fast your code works but still u dont understand that you're code gets slower much more because you are using j%2 check inside the while loop ...therene natam man mona karanada :P

chamidilk
02-15-2009, 02:04 PM
matath eka thama kiyanna thiyenne hallo

mama code eke athule j%2 check karanne
oya methana while loop eka athulema isprime chcek karanawa anika break keyword ekath pawichchi karal mokatada break ain karanna


u r de funny one who think i am a blind man

oya 29 prime kiyanna 28 idan 2 wenakan enawa
honadata balanna mage prog eka mama yanne 2 idan 6 wenakan witharai hello

u cnt challenge me

i cnt explian u further more u cnt understand i think .....
t dznt matter dnt play fool with me

madurax86
02-15-2009, 02:12 PM
pandithayoth eka katha karala wadak na

chamidilk
02-15-2009, 02:16 PM
pandithayoth eka katha karala wadak na


siera macho
hik hik umba danan idala thiyenawa neda shuk ......marai ah ...y dnt u use t
eeta passe umba ahanawa mama ai if(j%2==0&&...) check karanne ai kiyala
anunta upades didi panditha kan karana ewunta mageth wadi kamaththak naha ....
mata umba kiyana ka therenewa.
but umbata naha ...........

senewimala1988
02-18-2009, 04:30 PM
menna temperature conversion prog eka

#include<stdio.h>
main(){
int c;
int f;
char type;
printf("Temperature reading: ");
scanf("%d%c",&c,&type);
if((type=='c'||type=='C')){
printf("%dF",F(c));
}
else if((type=='f'||type=='F')){
printf("%dC",C(c));
}
else printf("Invalid Entry\nTry Again");
}

int C(int i){
return (5*(i-32)/9);
}
int F(int i){
return (32+9*i/5);
}
Macho oya red coloured set eka explain karapanko...Mama oya out put ekama enna program eka modify kala.Eth oyage oya red coloured coding eka therenne ne...

senewimala1988
02-18-2009, 04:32 PM
umbala mage thread eke yudda karala ne!!!

Im.Just.a.Fool
04-06-2009, 08:23 AM
chamidilk

oya prime numbers check karana kramaya hari. n is a prime if it is not divisible by x (2 <= x <= sqrt(n) ) . ea range eke numbers walin n bedenawada kiyala check karana kramaya thamai fast ma method eka. iratte sankya check kale nathnam program eka thawath fast wenawa. Because there is a 50% chance that any given integer is divisible by 2.


// This method assumes n is a non-negative integer, because
// prime numbers start with 2.
boolean isPrime(int n) {
if (n < 2) return false;
if (n == 2) return true;
if (n % 2 == 0) return false;
for (int i = 3; i*i <= n; i+=2)
if (n % i == 0) return false;
return true;
}

Im.Just.a.Fool
04-06-2009, 09:13 AM
Exercise 3-

[code]#include<stdio.h>
main(){
int i,j;
for(j=0;j<5;j++){
for(i=6;i>j;i--)printf("$");
printf("\n");
}
printf("$\n");
for(j=0;j<5;j++){
for(i=0;i<j+2;i++)printf("$");
printf("\n");
}
}


You can write the same program using two for loops like this (in Java):


for (int i = 0; i < 11; i++) {
for (int j = 1; j <= 6-Math.min(i, 10-i); j++)
System.out.print("$");
System.out.println();
}

System.out.print("$") = printf("$") in C
System.out.println(); = printf("\n");
Math.min(x,y) walin karanne x saha y walin kuda sankyawa hoyana eka. C/C++ walath meeta samana function ekak athi.