Nisaidieni swali hili la programming

Paa

JF-Expert Member
Jun 12, 2015
1,449
2,924
Katika picha
3df81be154d258b3cde089a8f06927bb.jpg
 
Dogo swali jepesi hivyo unashindwa kugoogle mbona code za c na c++ nyingi sana labda ingekuwa java ngoja nikusaidie
 
Dogo swali jepesi hivyo unashindwa kugoogle mbona code za c na c++ nyingi sana labda ingekuwa java ngoja nikusaidie
Google nimejaribu, sijafanikiwa. Its urgent nisaidie tafadhari
 
Katika picha
3df81be154d258b3cde089a8f06927bb.jpg
I am not that good kwenye C infact sijajifunza C, ila nimejaribu kuandika swali lako kwenye C++, nadhan ukipata idea hautashindwa ku convert kwenda kwenye C!!

Angalia hii code, kama patakua na marekebisho nitaongeza... ila naimani member wengine watatoa mawazo zaidi!!!

Version ONE, nimeweka kila kitu kwenye main function...
Code:
#include <iostream>

using namespace std;

int main()
{
    //local variables for sum and product of numbers
    int sum;
    int product;

    //for loop begins from here...
    for(int counter = 1; counter<=8; counter++){


        //takes the counter variable and find the sum of it...
        sum = counter+counter;

        //takes the counter variable and find the product of it...
        product = counter*counter;

        //display the sum of counter variable
        cout<< counter << "+" << counter << "=" << sum <<endl;


        //display the product of counter variable
        cout<< counter << "*" << counter << "=" << product <<endl;

    }

    return 0;
}


Version TWO, Nimejaribu kutenga codes kwenye functions 2 na kufanya "function prototyping"!

Code:
#include <iostream>

using namespace std;

void sumAndProduct(); //function prototyping...

int main()
{
    //calling "sumAndProduct" function
    sumAndProduct();
 
    return 0;
}


void sumAndProduct(){
 
    //local variables for sum and product of numbers
    int sum;
    int product;

    //for loop begins from here...
    for(int counter = 1; counter<=8; counter++){


        //takes the counter variable and find the sum of it...
        sum = counter+counter;

        //takes the counter variable and find the product of it...
        product = counter*counter;

        //display the sum of counter variable
        cout<< counter << "+" << counter << "=" << sum <<endl;


        //display the product of counter variable
        cout<< counter << "*" << counter << "=" << product <<endl;

    }

}

Same code in JAVA:

Code:
package com.company;

public class Main {

    public static void main(String[] args) {
   int sum, product;
// for loop begins here...
   for(int counter = 1; counter<=8; counter++){
//     sum of counter
       sum = counter+ counter;
//        product of counter
        product = counter*counter;
//        display sum of counter
        System.out.println(counter + "+" + counter + "=" + sum);

//        display product of counter
        System.out.println(counter + "*" + counter + "=" + product);
    }
    }
}
 
  • Thanks
Reactions: Paa
Umesaidia kuharibu elimu ya bongo. Huyo hatasoma tena mbali ya kucopy na kupaste jibu.




I am not that good kwenye C infact sijajifunza C, ila nimejaribu kuandika swali lako kwenye C++, nadhan ukipata idea hautashindwa ku convert kwenda kwenye C!!

Angalia hii code, kama patakua na marekebisho nitaongeza... ila naimani member wengine watatoa mawazo zaidi!!!

Version ONE, nimeweka kila kitu kwenye main function...
Code:
#include <iostream>

using namespace std;

int main()
{
    //local variables for sum and product of numbers
    int sum;
    int product;

    //for loop begins from here...
    for(int counter = 1; counter<=8; counter++){


        //takes the counter variable and find the sum of it...
        sum = counter+counter;

        //takes the counter variable and find the product of it...
        product = counter*counter;

        //display the sum of counter variable
        cout<< counter << "+" << counter << "=" << sum <<endl;


        //display the product of counter variable
        cout<< counter << "*" << counter << "=" << product <<endl;

    }

    return 0;
}


Version TWO, Nimejaribu kutenga codes kwenye functions 2 na kufanya "function prototyping"!

Code:
#include <iostream>

using namespace std;

void sumAndProduct(); //function prototyping...

int main()
{
    //calling "sumAndProduct" function
    sumAndProduct();
 
    return 0;
}


void sumAndProduct(){
 
    //local variables for sum and product of numbers
    int sum;
    int product;

    //for loop begins from here...
    for(int counter = 1; counter<=8; counter++){


        //takes the counter variable and find the sum of it...
        sum = counter+counter;

        //takes the counter variable and find the product of it...
        product = counter*counter;

        //display the sum of counter variable
        cout<< counter << "+" << counter << "=" << sum <<endl;


        //display the product of counter variable
        cout<< counter << "*" << counter << "=" << product <<endl;

    }

}

Same code in JAVA:

Code:
package com.company;

public class Main {

    public static void main(String[] args) {
   int sum, product;
// for loop begins here...
   for(int counter = 1; counter<=8; counter++){
//     sum of counter
       sum = counter+ counter;
//        product of counter
        product = counter*counter;
//        display sum of counter
        System.out.println(counter + "+" + counter + "=" + sum);

//        display product of counter
        System.out.println(counter + "*" + counter + "=" + product);
    }
    }
}
 

Similar Discussions

Back
Top Bottom