Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

infinity loop demonstration #76

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added asta.txt
Empty file.
41 changes: 41 additions & 0 deletions src/roots.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use std::io;

/* Prints the roots of a quadratic equation with given coefficients,
* calculated using quadratic formula.
*/

fn main() {
println!("Quadratic Equation: ax^2 + bx + c = 0");

println!("Enter Value of a:");
let a = read_int();

println!("Enter Value of b:");
let b = read_int();

println!("Enter Value of c:");
let c = read_int();

let d = b*b-4*a*c;
if d<0 {
println!("No real roots exist");
} else {
let root_d = (d as f64).sqrt() as i32;
let r1 = (-b + root_d)/(2*a);
let r2 = (-b - root_d)/(2*a);

println!("Roots are {} and {}", r1,r2)
}
}

fn read_int() -> i32 {
let mut num = String::new();

io::stdin()
.read_line(&mut num)
.expect("Error Reading Number");

num.trim()
.parse::<i32>()
.unwrap()
}
14 changes: 14 additions & 0 deletions src/sum of digits in number
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
fn main(){
let x = 234;
let a = x%10;
println!("a={}",a);
let aa = x/10;
let s = aa%10;
println!("s={}",s);
let ss = aa/10;
let d = ss%10;
println!("d={}",d);
let f = a+s+d;
println!("the sum is ={}",f);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Please use loops to find sum of digits. The length of number can change and this program is hardcodded for exactly three digits
  2. It looks like similar program already exist in repository https://github.com/rustindia/Rust-for-undergrads/blob/master/src/algorithms_data_structures/simple_algorithms/sum_of_digits.rs

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fn main(){
let mut x=0;

loop{
x += 5;
if x==100{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this if condition to make it infinite loop

break;
}
println!("the value of x ={}",x);
}
}