Java,Python,PHP,Web Design Tution in Kolkata

Java,Python,PHP,Web Design Tution in Kolkata Find in depth tuition on Java/JSP,PHP & MySQL, Linux and Web Design/Graphic Design in Kolkata I have another blog on knowledge sharing purpose.

Hello I am Sourav Bhattacharya ,technology enthusiast,movie freak and rock and pop fan,Currently I am working on Excel VBA, PHP/MySQL and wordpress,joomla and drupal as cms,I like to share my experience with all of you and hopefully learn a lot too.I have also been teaching Java,JSP,Oracle,PHP & MySQL,VB.Net,Web Design for the last 6 years,so feel free to contact me anytime at 9748184075 regarding

teaching assignments. http://wowmoron.blogspot.com/

hope you pay a visit. Sourav Bhattacharya

Kolkata

আজকাল ক্যানভার মতো অনলাইন টুল দিয়ে খুব সিহিজেই লোগো বানিয়ে দেয়া যায় ,খুব অরিজিনাল নয় ,কিন্তু কাজ চালানোর জন্য যথেষ্ট ,আম...
30/10/2024

আজকাল ক্যানভার মতো অনলাইন টুল দিয়ে খুব সিহিজেই লোগো বানিয়ে দেয়া যায় ,খুব অরিজিনাল নয় ,কিন্তু কাজ চালানোর জন্য যথেষ্ট ,আমাদের ছোটবেলায় এইসব ছিল না ,একটা সেলুনের লোগো ডিসাইন করলাম

26/02/2024

অনেক বয়স হয়ে গেল বুঝলেন তো ,একসময় কারোর বয়স বত্রিশ শুনলেই অবজ্ঞায় ঠোঁট বেঁকে যেত ,বুড়ো মাল ,ঘাটের মরা এইসব ভাবতাম ,এড়িয়ে চলতাম বলা চলে ,এখন নিজেই চল্লিশ ,চুল কম পাকলো ,দাড়ি বেশি পেকে গেলো ,সেই দেখে আবার এক বাচ্চা ছেলে দাদু বলে ডেকেছিল

এই বয়েসে একটা জিনিস লোকে এড়িয়ে চলে ,ফালতু হেনস্থা হওয়া ,কিন্তু মানুষের সঙ্গে কথা বলার কাজ আমার ,কাজেই ওই হেনস্থার জায়গাটা কখনো বন্ধ হবে না ,একটা উদাহরণ দি ,একবার হাওড়া ময়দানে এক বিহারি মেয়েকে পড়াতাম ,জ্ঞান তপস্সিনি বলা যায় ,শিবপুরের ছাত্রী ,তবে এখন আবার শিবপুর কে শিবপুর কেউ বলে না ,আই আই ই এস টি না কি যেন বলে ,হাওড়া ময়দানের ঘুপচি ঘরে পড়াতাম ,কিছুদিন পড়েছিল ,গড়গড় এর দুঃস্বপ্ন বলা চলে ,তবে আমার খারাপ লাগে নি ,দুটো প্রশ্ন এই পর্যন্ত পড়ে মাংস র মাথায় আসবে ,ঘুপচি ঘরে কি পাং পাং হয়েছিল ,শিবপুরের ছাত্রী আমার মতো অপোগন্ড গণ্ডমূর্খের কাছে পড়লো ইটা কিভাবে সম্ভব ,দুটো প্রশ্নের উত্তর ই হতাশা জনক ,এই পোস্ট হবে শুস্ক কাষ্ঠঙ ,লিখতেও মজা লাগছে ,এ যেন স্কিৎজোফ্রেনিয়া রোগীর ডায়েরি ,নিজের সঙ্গে নিজের কথোপকথন

শিবপুরের ব্যাপারস্যাপার হলো ক্লাস এসাইনমেন্ট হেবি কঠিন,কিন্তু পরীক্ষা সহজ ,পরিশ্রমী ছাত্র ছাত্রীরা প্রোগ্রামিং করতে গিয়ে একটু সংকটে পড়ে ,আমি কি এই বয়েসে বুঝতাম কিছু ,নাঃ পানু দেখতাম আর সিনেমা দেখতাম ,বন্ধুদের সঙ্গে হাহা হিহি চলতো ,আদি রসাত্মক ইয়ার্কি না হলে বিরক্ত লাগতো ,হু হু বয়েজ স্কুলের অভিশাপ আর কি

হেনস্থার প্রসঙ্গে আসি ,ছাত্রী সি প্রোগ্রামিং এ ইনপুট এ একটি বাক্য নিতে পারছে না,খুব উৎসাহ নিয়ে scanf বোঝাতে গেলাম,দেখলাম নিজের ও হলো না ,কেন ,কারণ ডব্লিউ বিউটি তে শেখানো হয় নি ,scanf দিয়ে যদি ইনপুট নেন ,ইউজার যদি ইনপুট এ অতিরিক্ত ব্ল্যান্ক স্পেস টাইপ করে বা নিউলাইন ক্যারেক্টার টাইপ করে scanf ইনপুট নেবেই না ,ছাত্রী কে আরেকটা কাজ দিয়ে নিজে একটু গুগুল করে নিলাম ,দেখলাম সমস্যার সমাধান আছে ,কপি করে দিলাম




int main()
{
char sen[100];
scanf("%[^\n]s", sen );
printf("Output : %s", sen);
return 0;
}

এই প্রোগ্রামটি একটা সেন্টেন্স কে ইনপুট হিসেবে নিতে পারে,ছাত্রীকে দেখিয়ে বেশ একটা কের্দানি করেছি কি করিনি ,ছাত্রী বলে আরে এতো প্রথম সমস্যা ,মূল সমস্যার ছোট একটি অংশ মাত্র

আমি ঘাবড়ে গেলাম,বুজলাম আরো হেনস্থা আছে কপালে ,বললাম কি সমস্যা আছে বলো ,মনে মনে ভাবছি বেশি জটিল যেন না হয় ভগবান ,এই মাসের মাইনে বাকি

বলে এইভাবে একটা কালেকশন অফ স্ট্রিং নিতে হবে ইনপুট হিসেবে ,স্ট্রিং মানে জানেন তো ,ধরুন আপনার নাম sourav ,s একটা ক্যারেক্টার ,এরকম একাধিক ক্যারেক্টার কে হয় একটা স্ট্রিং ,মানে sourav একটা string ,ক্যারেক্টার মানে যা ইচ্ছা হতে পারে ,নাম্বার ও হতে পারে ,সেই হিসেবে ১০০ ও একটা স্ট্রিং হতে পারে,তবে স্ট্রিং ডবল কোটেশনে ঢাকা থাকবে ,"১০০" বা "সৌরভ",ক্যারেক্টার হলে সিঙ্গল কোটেশনে ঢাকা থাকবে 'স',এই হলো গিয়ে সি প্রোগ্রামিং এর নিয়ম কানুন

বললাম সে ঠিক আছে একটা কালেকশন অফ স্ট্রিং নিতে হবে ,তারপর ,উপরের প্রোগ্রামটি তো একটা কালেকশন অফ স্ট্রিং বা একটি বাক্যকে ইনপুট হিসেবে নিয়ে একটি স্ট্রিং ভ্যারিয়েবল এ স্টোর করতে পারে তাই না ,তার মানে প্রব্লেম তো সল্ভড

সে বিরক্ত হয়ে বলে আরে নানা একটা ভ্যারিয়েবল নয় একটা এরে তে স্টোর করতে হবে ,তারপর শব্দগুলো সর্টিং করতে হবে ,সর্টিং মানে কি ,না আলফাবেট এর ক্রোম অনুযায়ী আগে পরে বসবে

একটা উদাহরণ দি ইন্টারনেট থেকে কপি করে

ধরুন ইনপুট এসেছে

Input: arr[] = {“geeks”, “for”, “geeksforgeeks”}
Output:
for
geeks
geeksforgeeks

বুঝতেই পারছেন প্রতিটা শব্দের সঙ্গে অন্য শব্দগুলোর তুলনা বলে ,এরকম তুলনার একটা নাম আছে ,lexicographical তুলনা ,এখানে প্রতিটা স্ট্রিং এর প্রতিটা অক্ষর আরেকটি স্ট্রিং এর সিম পজিশনে থাকা অক্ষরের সঙ্গে তুলনা হয়

ইন্টারনেট থেকে কপি করে একটা প্রোগ্রাম দেখাই



void compareStrings(char* s1, char* s2)
{
int i;
// comparing each character
for (i = 0; s1[i] != '\0' || s2[i] != '\0'; i++) {
if (s1[i] > s2[i]) {
printf("String 1 is lexicographically greater "
"than string 2");
return;
}
else if (s2[i] > s1[i]) {
printf("String 2 is lexicographically greater "
"than string 1");
return;
}
}
// comparing length of two strings
if (s1[i] != '\0') {
printf("String 1 is lexicographically greater than "
"string 2");
}
else if (s2[i] != '\0') {
printf("String 2 is lexicographically greater than "
"string 1");
}
else {
printf("Both strings are lexicographically equal");
}
}
int main()
{

// declaring two strings
char s1[20] = "help";
char s2[20] = "held";

// function call
compareStrings(s1, s2);
return 0;
}

এই প্রোগ্রামটি কি করতে পারে ,দুটো স্ট্রিং এর মধ্যে কম্পেয়ার করতে পারে ,কিন্তু এতে আমার কাজ শেষ হচ্ছে না ,আমাকে পুরো বাক্যের প্রতিটা শব্দের বা স্ট্রিং এর সঙ্গে ওপর প্রতিটি বাক্যের বা স্ট্রিং এর এরকম কম্পারিসন করতে হইবে

এই প্রোগ্রামটি কি ভাবে কাজ করছে সেটাও নেট থেকে কপি করে লিখছি

Iterate over both the strings using a for-loop.
Compare each character of the two strings till an unmatched character is found.
For the unmatched character at position i, if s1[i] < s2[i], s1 is lexicographically smaller string.
Otherwise, s2 is the lexicographically smaller string.
If no unmatched character is found, compare the length of both strings.
The longer string is lexicographically smaller.

সমস্যার ছোট একটি অংশ সমাধান হলো

এবার এই অংশ টিকে ব্যবহার করতে হবে এমন একটি প্রোগ্রাম এ যেখানে প্রোগ্রাম প্রতিটা স্ট্রিং এর মধ্যে এই উপরের প্রোগ্রামটি চালাবে

আবার গুগুল করে চুরি করে লিখে দিলাম একটা ,নির্লজ্জের মতো লাইন বই লাইন কপি বলাই বাহুল্য



int main()
{
char str[5][50],temp[50];
int i,j;

printf("Enter 5 Words:\n");

for(i=0;i

25/12/2023

লিনাক্স বিপ্লবে অনিচ্ছুক সৈনিক - পর্ব এক

এখন দুই হাজার তেইশ সাল,দুই হাজার পাঁচ সাল অর্থাৎ আঠেরো বছর আগে আমি লিনাক্স অপারেটিং সিস্টেম এর সঙ্গে পরিচিত হই,তখন ডট কম বুম চলে এসেছে ,আইটি জনপ্রিয় হয়েছে কেরিয়ার চয়েস হিসেবে ,আমি সরকারি স্কুলের অমনোযোগী ছাত্র ছিলাম ,স্কুলে বেসিক ল্যাঙ্গুয়েজ পড়ানো হলেও আমি বিশেষ কিছু শিখিনি ,বাড়িতে তখন টেক স্যাভি লোকজন ছিল না ,ফলে কম্পিউটার প্রথম কেনা হয় কলেজে পড়ার সময় ,তখন ও আমি জিনিসটায় স্বচ্ছন্দ বোধ করতাম না ,ফলে বাড়ির লোককে কম্পিউটারের সুইচ টিপে চালিয়ে দিতে হতো এমন ও দিন গেছে

এরকম অবস্থায় কিছু বিজ্ঞাপন দেখে আমি রেড হ্যাট লিনাক্স এর কোর্স এ ভর্তি হই,উচিত হয়নি বলাই বাহুল্য ,কারণ যে কম্পিউটার এর মূলগত জিনিষ ঠিকমত জানে না সে লিনাক্স ভালোভাবে শিখবে কি করে ,সম্ভব হয় ও নি ,টাকা জলে গেছে ,এমনিতে সেই সময় যারা লিনাক্স শিখেছেন তারা লিনাক্সের ওই একঘেয়ে টার্মিনালে কমান্ড টাইপ করার যন্ত্রণা জানেন ,অসম্ভব খারাপ একটা অভিজ্ঞতা ,বোকামির ফল এরকম ই হয় ,শুধু জেনেছিলাম লিনাক্স বেশি সিকিওর উইন্ডোজের থেকে ,এইটুকুই ছিল কোর্সের প্রাপ্তি ,কোর্সের প্রশিক্ষক ও অনিচ্ছুক ছাত্র দেখে মুখ ঘুরিয়ে নিয়েছিলেন ,কেনোই বা এক্সট্রা এফোরট দেবেন ,পয়সা আগে পেয়ে গেলে সেই যুগের ছোট প্রাইভেট ইনস্টিটিউটের শিক্ষক অত খাটবেন কেন

যে কোনো জিনিষ শিখতে গেলে বাড়িতে প্র্যাকটিস করতে হয় ,আজকাল অনেকে ইনস্টিটিউট এ বিদেশি ভাষা শিখতে যায় ,ব্যাপারটা অদ্ভুত লাগে ,জিনিসটা চব্বিশ ঘন্টা আপনার অংশ না হলে আপনি প্রফিশিয়েন্সি পাবেন কি করে,লিনাক্স এও তাই ব্যাপার ,বাড়িতে ইনস্টল করতে হবে ,নিয়মিত ঘাটতে হবে ,তবে না শিখবেন আপনি ,এবার সেটা করতে গিয়ে পড়লাম সমস্যায় ,কারণ লিনাক্স ইনস্টল করতে জানি না ,যদি বা কাউকে পয়সা দিয়ে করালাম ড্রাইভারের অভাব ,ইন্টারনেট কাজ করে না ,তখনকার সময় লিনাক্স এত আধুনিক ছিল না ,অনেক ডিভাইস চিনতে পারতো না ,চেনাতে হতো,অত জ্ঞান আমার ছিল না ,ফলে ওই টার্মিনাল খুলে দুই চারটি বেসিক কমান্ড অভ্যাস করা ছাড়া কিছু করতে পারিনি

সেই যুগ ছিল ভাইরাসময়,উইন্ডোজ ৯৮ থেকে আপগ্রেড করে যারা এক্স পি তে এসেছেন তারা জানেন নিয়মিত ভাইরাস এলে কেমন লাগে ,বিশেষ করে আমার মত ইয়ং পুলাপান যারা রাতের দিকে একটু আধটু ইয়ে মার্কা ভিডিও সাইট খুলতো ,এই জিনিসে অবশ্য হাতে খড়ি হয়েছিল সাইবার কাফেতে ,আমি কে অক্ষর শিখিয়েছে কে লিখতে শিখিয়েছে ভুলে গেছি ,কিন্তু চরম রক্ষনশীল দেশে কে পানু দেখতে শিখিয়েছে চিরকাল মনে থাকবে ,যায় হোক উইন্ডোজ এক্সপিতে পানু
দেখতে গিয়ে ব্রাউজার করাপ্ট হয়ে যেত ,ভাইরাস বিশেষ করে উইন৩২ সিরিজের ভাইরাস অনেক ফাইল করাপ্ট করে দিত ,এরকম ইনফেকটেড কম্পিউটারে পেন ড্রাইভ ঢোকালে ভাইরাস দ্রুত ছড়িয়ে পড়ত,তখনকার ফ্রি এন্টি ভাইরাস যেমন এভাস্ট এভিজি এসব আটকাতে পারতো না ,পয়সা খরচ করে ক্যাসপারস্কি কিনলে সুবিধা হত কিন্তু অত টাকা ছিল না ,প্লাস কম্পিউটার স্লো হয়ে যেত ,ফলে লিনাক্স এর একটা ব্যবহারিক প্রয়োগ খুঁজে পেলাম ,লাইভ লিনাক্স এ কম্পিউটার বুট করিয়ে পেন ড্রাইভ ক্লিন করতাম ,এই করে বছর খানেক কাটলো

প্রথমে কম্পিউটারে মেমোরি ছিল দুশো ছাপ্পান্ন এমবি যতদূর মনে আছে , পরে আর একটু বাড়িয়ে করা হলো পাঁচশো বারো ,আসতে আসতে মেমোরির দাম কমতে লাগলো ,আরো বাড়িয়ে এক জিবি করলাম কিছু বছর বাদে ,তখন লিনাক্স অভ্যাস করার জন্য একটা নতুন অপশন পেলাম,ভার্চুয়াল মেশিন বানিয়ে তাতে চালানো ,vmware ব্যবহার করতে শুরু করলাম ,তাতে দেখলাম লিনাক্স ইন্টারনেট পাচ্ছে ,ফাইল শেয়ার হচ্ছে ,আগ্রহ বাড়তে আরম্ভ করলো

চলবে

23/11/2023

Send email using python

20/08/2023

arraylist in java

import java.util.ArrayList;
import java.util.Collections;

public class Main
{
public static void main(String[] args) {
ArrayList array1=new ArrayList(5);
array1.add(50);
array1.add(46);
array1.add(79);
array1.add(32);
array1.add(47);
System.out.println("The size of the array1 is "+array1.size()+" \nand the elements are \n"+array1);
for(int i=0;i

18/08/2023

arrays in java Java,Python,PHP,Web Design Tution in Kolkata

import java.util.Scanner;
public class Main
{
public static void main(String[] args) {

int nums[];
//int[] nums;
int len=5;
nums=new int[len];
nums=new int[]{81,42,35,55,59};
/* nums[0]=81;
nums[1]=42;
nums[2]=35;
nums[3]=55;
nums[4]=59;*/

//System.out.println(nums[0]);
//System.out.println(nums[1]);

System.out.println("the length of nums array is "+nums.length);

/*for(int i=0;i=0;i--)
System.out.println(nums[i]);

/* nums=new int[len+1];
for(int i=(nums.length-1);i>=0;i--)
System.out.println(nums[i]); */


}
}

13/08/2023

tuple example in Java,Python,PHP,Web Design Tution in Kolkata

names=("Vaishnavi","Sourav","Asim")
print(names)
print(names[0])
for i in range(0,3):
print(names[i])

i=0
while(i

13/08/2023

Let us output an integer in Rust.
Rust has a simple line of code that we use in all programs to print to the console. This is the line that you see in the program to your right. (Ignore what this line means for now - we will explain it later)
• What matters for now is just Line 2
"println!" (pronounced "print-line"). This is what we use to output/print anything to the screen.
• Every Rust statement ends with a semicolon (;)
This code just prints 12 on the screen when you run it.

fn main() {
println!("{}", 12);
}

In the last problem, we saw how to output an integer (using println!).
Now let's see how to output some text.
There is only one difference. All textual content should start and end with double quotes.
For eg -
println!("_");
Task
• Replace the _ (underscores) in the editor with the text "I love Rust" to output "I love Rust".
We can also perform mathematical operations with 'println!'.
These use the familiar symbols of:
• ++ for addition
• −− for subtraction
• ∗∗ for multiplication
• // for division
Try printing the sum of 21 and 40.
Notice that on the editor on the right, the instructions appear in gray color. This is because they are comments and don't affect the code. The way we write comments is by adding "//" before it.

fn main() {
println!("{}", 21 + 40); //Replace the _ with the correct value
}

Which line of code will output the string "Hi"?
println!("Hi");
print!("Hi");
println!("{}", "Hi");
print!("{}", "Hi");

Which line of code will output the sum of 7 and 19?

We can add as many cout statements as we want.
Write a program which does the following
• Output: 3 + 4
• Output: 2 + 1.
Note - Notice that in the output they are printed together without any space between them.

fn main() {
print!("{}",3 + 4);
print!("{}", 2 + 1);
}

Write a program for the following problem
• Let’s consider a rectangle of sides 11 and 13.
• Output the following on separate lines
o Area of the rectangle
o Perimeter of the rectangle
fn main() {
let length = 11;
let width = 13;

let area = _____ * _____;
let perimeter = 2 * (_____ + _____);

println!("{}", area);
println!("{}", perimeter);
}

fn main() {
let length = 11;
let width = 13;

let area = length * width;
let perimeter = 2 * (length + width);

println!("{}", area);
println!("{}", perimeter);
}

Rearrange the code to produce this output:
Hello
World

• fn main() {
• println!("{}","");
print!("Hello");
print!("World");
• }

• fn main() {
• print!("Hello");
println!("{}","");
print!("World");
• }

You can also have multiple outputs together in a single "print!" command.
So, for example,
print!("{}" , 2);
println!("{}");
print!("{}" , 3);
and
print!("{}\n{}" , 2 , 3 );
are equivalent.
Task
Write a program which does the following
• Output: 3 + 4
• Output: 2 + 1 in a separate line using the syntax suggested above.

fn main () {
print!("{}\n{}" , 3+4 , 2+1 );
}

Write a Rust program that performs the following tasks:
• Outputs the result of the expression 3 + 4
• On the same line, separated by a single space, outputs the result of the expression 2 + 1. The two results should be displayed on the console on the same line with a space separating them.
Remember to use the space between the {} in the format string to represent the empty space between the two output values.
fn main() {

print!("{} {}",3+ 4 , 2 + 1 );

}

If we want any other text in between the outputs we can provide that text inside " " sign.
Write a program which does the following
• Output the result of 3 + 4
• Output the result of 2 + 1 on the same line
• Insert the text "and" between the outputs.
• Note: We have a space before and after the text as well, as without those, the output would be "7and3", instead of "7 and 3".

fn main() {

print!("{} and {}",3+ 4 , 2 + 1 );

}

What will be the output of this code?

fn main() {
print!("{}{} {}" , 82 , 2 , 3);
}

82 2 3
8223
822 3
822" "3

What will be the output of this code?

fn main() {

print!("{}{} {}{}","Add", 2 , "to" , 3);

}

Add2to3
Add 2 to 3
Add2 to3
Add2to 3

Write a program which does the following
• Output the result - Product of the integers 88 & 99
• Output the result - Sum of 100100 & 200200 multiplied by 22
• Output both results to separate lines

fn main () {

print!("{}\n{}",8*9,(100+200)*2);
}

Write a program which does the following
• Output the result - Difference of the integers 99 & 22
• Output the result - Division of 3030 by 55
• Output both results to the console on separate lines

fn main () {
print!("{}",9-2);
println!();
print!("{}" , 30/5);

}

Let us now look at some of the nuts and bolts of Rust.
Rust, like C++, uses several predefined functions and macros to perform specific tasks, such as handling inputs and outputs.
• fn main() { ... }: This is the main function of the program. In Rust, ex*****on begins with the main function. Any code inside its curly braces {} will be executed. If there are other functions in your code, main() is the function that gets executed first..
• Similar to C++, Rust ignores white space, and we can use it to make the code more readable.
• Comments in Rust start with // for single line comments, and /* ... */ for multi-line comments.
• "print!" - We've already seen what this does.
• Rust does not require a return 0; statement at the end of the main() function like C++ does. In Rust, the main() function returns () (the unit type) by default. If your main() function needs to return a value, it should return a Result.
• Do not forget to add the closing curly bracket "}" to actually end the main function.
Try displaying the sum of 12 & 7 using the given code
fn main() {

print!("{}" ,12+7); //Replace the _ with correct digits

}

What is the difference between
fn main() {

print!("{}",12);
print!("{}",11);

}
and
fn main() {

print!("{}",12);

print!("{}",11);

}

The first code will have outputs on the same line, and the second code will output in separate lines.

The two codes are equivalent.

The second code is wrong, and will not compile.

Suppose we now want to find the area and perimeter of the rectangle again which has length = 11 and width = 13.
But now the width has changed from 13 to 15.
So we'll have to change that number in both - the equation of area and the equation of perimeter.
Instead of changing it in two places each time we change the width, won’t it be easier if we can change it just once?
For that, let's consider doing something like this:
• Let’s take a box, call it "width" and put the number 13 inside it.
• Similarly, let’s also take a box called "length" and put the number 11 inside it.
• Now, to find the area, we just take the numbers inside these two boxes and multiply them. That is, println!("{}", length * width);.
• Similarly, to print the perimeter, we just do println!("{}", 2 * (length + width));.
Now, if we want to change the width from 13 to 15, we just have to change the number inside that box, and nothing else. Both the formulas will now be correct since they take the value from the box.
Note -
• let: In Rust, the let keyword is used to declare or initialize a variable.
• mut: The mut keyword in Rust makes a variable mutable, allowing its value to be changed after it is initially declared.
In Rust, we should also tell what kind of stuff we are going to put into every box.
• Here, we are only going to put integers inside the box called "width".
• So we write
let mut width = 13;
• This means that the box called "width" will contains i32 (i.e. 32-bit integers), and right now, we have put in the integer 13 into it. This is needed only when we name the box for the very first time.
• When we change the value inside the box later on, we'll just write
width = 15
• Note that we don't write "let" at the beginning here because the variable is mutable and we are not declaring a new one but changing its value.
Task
Update the value of 'width' to 15 in your Rust environment and then proceed to calculate the area and perimeter of the rectangle.
Did you like the problem?

fn main() {
let length = 11;
let mut width = 13;

// Calculate and print the area and perimeter
println!("{}", length * width);
println!("{}", 2 * (length + width));

// Update the width
width = __; // replace the underscore with the correct value

// Calculate and print the new area and perimeter
println!("{}", length * width);
println!("{}", 2 * (length + width));
}

Solution:

fn main() {
let length = 11;
let mut width = 13;

// Calculate and print the area and perimeter
println!("{}", length * width);
println!("{}", 2 * (length + width));

// Update the width
width = 15; // replace the underscore with the correct value

// Calculate and print the new area and perimeter
println!("{}", length * width);
println!("{}", 2 * (length + width));
}

The box we discussed in the previous problem is what is known as a Variable in Rust.
And the number that we put inside that box is called the 'Value' of the variable.
When we create a box, we should also specify what Type of box it is.
• If it is an "i32" box, then you can only place integers inside it. You cannot place 3.5 into it..
• For that, you will need to specify that you are going to place decimal values inside it while creating the box, which would make it an "f64" box. These are called Variable Types..
These are the various main types in Rust:
• i32 - stores integers (whole numbers), without decimals, such as 123 or -123
• f64 - stores floating point numbers, with decimals, such as 19.99 or -19.99
• char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
• String - stores text, such as "Hello World". String values are surrounded by double quotes
• bool - stores values with two states: true or false
• Variables are immutable by default in Rust, meaning their values cannot be modified once assigned. However, by using the mut keyword, variables can be made mutable and their values can be changed.
NOTE
• In Rust, you can explicitly define a variable's data type by using a colon : after the variable's name, followed by the desired type. For example, let number: i32 = 10; explicitly declares number as a 32-bit integer and assigns it the value 10.
• While it's possible to explicitly specify a variable's type in Rust, it's often not necessary because Rust has a feature known as type inference. This feature allows the Rust compiler to automatically determine the types of most variables based on the values they are assigned.
Explicitly specifying the type of a variable is most useful in scenarios where:
• The type cannot be automatically inferred by the compiler.
• There could be ambiguity about which type to use.
• You want to ensure that a specific type is used for a certain variable, regardless of the value it's assigned initially.
Task
Write a program which does the following
• Create an i32 variable named NumberNumber
• Assign the value 19 to NumberNumber
• Use the println! macro to output the value of NumberNumber to the console.
Solution
fn main() {
let Number:i32=19; // Update the value assigned to number
println!("{}", Number);
}

Write a program which does the following
• Declare two variables aa and bb
• Assign the value 2323 to aa and 2020 to bb
• Output the sum of aa and bb to the console
Solution:

fn main () {

let a =23;
let b=20;

println!("{ }",a+b);

}

Write a program which does the following
• Find out and display the area of a rectangle of sides 45 and 76 respectively.
• Declare variables lengthlength, widthwidth and areaarea and assign the relevant values to them
• Make sure to add some text as well, so that the person reading the output knows what the output means.

Solution:

fn main() {
let length =45;
let width = 76;
let area = length *width;
println!("The Area of the given rectangle is {}",area);
}

If we want to introduce decimal values we use the f64 data type when declaring the variable.
Task
Write a program which does the following:
• Find the area of a circle whose radius is 8.9 cm. Take pi = 3.14
• Declare variables radiusradius, p**i and areaarea and assign the relevant values to them
• Note: Area of a circle is P**i ×× radius2radius2
NOTE
• To limit the output to three decimal places, use the println! macro with {:.3} as the placeholder in the output string. This formatting directive will round the floating point number to the nearest value with exactly three decimal places
Type Inference in Rust
• In Rust, the compiler uses Type Inference to automatically determine the data types of variables based on the values they are assigned.
• This means that even if you remove explicit type annotations like f64, the program would still work correctly as the compiler infers the correct type based on the assigned values, simplifying code and enhancing readability.
Did you like the problem?

Solution:

fn main() {
let pi: f64 = 3.14; // pi is declared as an 'f64' (double precision floating point)
let radius: f64 = 8.9;
let area: f64 = pi * radius *radius;
println!("The Area of the given Circle is {:.3}", area);
}

The &str and String types are used to store a sequence of characters, i.e. text. String values must be surrounded by double quotes, ie. " ".
Task
Write a program which does the following
• Declare two variables aa & bb
• Assign the values "Code" & "Chef" to aa and bb respectively
• Display the variables in a single line without any space between them
Note
In Rust, you have two main ways to represent strings
• &str: These are string slices, and they're typically used for fixed-length strings or string literals. They're immutable and usually point to a sequence of UTF-8 characters
• String : The String type is a growable, mutable, owned buffer of UTF-8 bytes. You might use String when you need to modify or own the string data.
For this task, you can use either.
Solution:
fn main() {
let a:&str= "Code";
let b:&str = "Chef";
println!("{}{}", a,b) // should print "CodeChef"
}

Address

Alipore
Kolkata
700027

Telephone

8910141720

Website

http://wowmoron.blogspot.in/

Alerts

Be the first to know and let us send you an email when Java,Python,PHP,Web Design Tution in Kolkata posts news and promotions. Your email address will not be used for any other purpose, and you can unsubscribe at any time.

Contact The Business

Send a message to Java,Python,PHP,Web Design Tution in Kolkata:

Share