Android Studio Write a program to display the n terms of square natural number and their sum.

Arah Lab
14 minute read
0

Input the number of terms : 5 Expected Output : The square natural upto 5 terms are : 1 4 9 16 25 The Sum of Square Natural Number upto 5 terms = 55





Check square natural number and their sum - XML CODE

Android Studio Java Code Box with Copy Button
     
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Series_Upto9"
    android:background="#E4EEED"
    android:orientation="vertical"
    >


    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar6"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@drawable/bottom_curved"
        app:title="Check square natural number and their sum"
        android:elevation="4dp"
        app:titleTextColor="@color/white"
        />


    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/ASDFG"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        app:errorTextColor="#FF0000"
        app:hintTextColor="#0038FF"
        android:hint="Input the number of terms"
        app:boxStrokeColor="#000DFF"
        android:textColorHint="@color/black"
        app:endIconMode="clear_text"
        app:endIconTint="#FF0000"
        >

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/kibriya"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="numberDecimal"
            android:textColor="@color/black"
            />


    </com.google.android.material.textfield.TextInputLayout>




    <Button
        android:id="@+id/Buttonkibriya"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:backgroundTint="#1100FF"
        android:text="Click Here"
        android:textColor="#FFFFFF"
        android:textSize="20sp"
        android:layout_marginRight="110dp"
        android:layout_marginLeft="110dp"
        android:layout_marginTop="10dp"
        />




    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="400dp"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_margin="20dp"
            >

           <TextView
                android:id="@+id/ruf1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="25sp"
                android:textColor="@color/black"
                android:visibility="gone"
                />

            <TextView
                android:id="@+id/ruf2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="25sp"
                android:textColor="#0F25EA"
                android:visibility="gone"
                />


            <com.airbnb.lottie.LottieAnimationView
                android:id="@+id/AnimisonLotti1as"
                android:layout_width="match_parent"
                android:layout_height="400dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.498"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                android:layout_gravity="center"
                app:lottie_autoPlay="true"
                app:lottie_loop="true"
                app:lottie_rawRes="@raw/search"
                android:visibility="visible"
                />




        </LinearLayout>

    </ScrollView>




</LinearLayout>

        


  Check square natural number and their sum - JAVA CODE
Android Studio Java Code Box with Copy Button

package com.arah.jobayerahmed;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.airbnb.lottie.LottieAnimationView;
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;

public class Series_Upto9 extends AppCompatActivity {
    TextInputEditText kibriya;
    Button Buttonkibriya;
    TextView ruf1, ruf2;
    TextInputLayout ASDFG;
    LottieAnimationView AnimisonLotti1as;
    Animation dtg1,dtg2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.series_upto9);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar6);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }

        kibriya = findViewById(R.id.kibriya);
        Buttonkibriya = findViewById(R.id.Buttonkibriya);
        ruf1 = findViewById(R.id.ruf1);
        ruf2 = findViewById(R.id.ruf2);
        ASDFG = findViewById(R.id.ASDFG);
        AnimisonLotti1as = findViewById(R.id.AnimisonLotti1as);

        dtg1 = AnimationUtils.loadAnimation(this, R.anim.home1);
        dtg2 = AnimationUtils.loadAnimation(this, R.anim.home3);

        ASDFG.setAnimation(dtg1);
        Buttonkibriya.setAnimation(dtg2);


        Buttonkibriya.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String GPS = kibriya.getText().toString();

                if (GPS.isEmpty()){
                    kibriya.setError("Enter Number");
                }else {

                    int n = Integer.parseInt(GPS);

                    StringBuilder seriesBuilder = new StringBuilder();
                    long sum = 0;

                    for (int i = 1; i <= n; i++) {
                        int square = i * i;
                        seriesBuilder.append(square);
                        if (i != n) {
                            seriesBuilder.append(" + ");
                        }
                        sum += square;
                    }
                    AnimisonLotti1as.setVisibility(View.GONE);
                    ruf1.setVisibility(View.VISIBLE);
                    ruf2.setVisibility(View.VISIBLE);
                    ruf1.setText(""+seriesBuilder.toString());
                    ruf2.setText("The sum of the series = " + sum);

                }



            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }



}

        

Post a Comment

0Comments

Post a Comment (0)