Skip to content

Commit 015be35

Browse files
committed
added programs of Operators and for loop
1 parent e43f1d1 commit 015be35

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ This repo includes:
2626
- [Star Pattern](./milestone1/pattern2/StarPattern.cpp)
2727
- [Triangle of Numbers](./milestone1/pattern2/TriangleofNumbers.cpp)
2828
- [Diamond of Stars](./milestone1/pattern2/Diamondofstars.cpp)
29+
- [Operators and For Loop](./milestone1/operatorsandforloop/)
30+
- [Nth Fibonacci Number](./milestone1/operatorsandforloop/NthFibonacciNumber.cpp)
31+
- [All Prime Numbers](./milestone1/operatorsandforloop/AllPrimeNumbers.cpp)
32+
- [Count Characters](./milestone1/operatorsandforloop/CountCharacters.cpp)
2933

3034
# Coding Ninjas
3135
## Rate my Repo ⭐!!!
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Given an integer N, print all the prime numbers that lie in the range 2 to N (both inclusive).
2+
// Print the prime numbers in different lines.
3+
4+
#include <iostream>
5+
using namespace std;
6+
7+
int main(){
8+
int n;
9+
cin>>n;
10+
for (int i=2; i <= n; i++){
11+
bool prime = false;
12+
for(int j=2; j < i; j++){
13+
if (i % j == 0) {
14+
prime = true;
15+
break;
16+
}
17+
}
18+
if (!prime)
19+
cout<<i<<endl;
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Write a program to count and print the total number of characters (lowercase english alphabets only), digits (0 to 9) and white spaces (single space, tab i.e. '\t' and newline i.e. '\n') entered till '$'.
2+
// That is, input will be a stream of characters and you need to consider all the characters which are entered till '$'.
3+
// Print count of characters, count of digits and count of white spaces respectively (separated by space).
4+
5+
#include<iostream>
6+
using namespace std;
7+
8+
int main(){
9+
char c;
10+
c = cin.get();
11+
int cc = 0, cd = 0, cs = 0;
12+
while (c != '$'){
13+
if (c >= 'a' && c <= 'z')
14+
cc++;
15+
else if (c >= '0' && c <= '9')
16+
cd++;
17+
else if (c == ' ' || c == '\n' || c == '\t')
18+
cs++;
19+
c = cin.get();
20+
}
21+
cout<<cc<<' '<<cd<<' '<<cs<<endl;
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Nth term of Fibonacci series F(n), where F(n) is a function, is calculated using the following formula -
2+
// F(n) = F(n-1) + F(n-2),
3+
// Where, F(1) = 1,
4+
// F(2) = 1
5+
// Provided N you have to find out the Nth Fibonacci Number.
6+
7+
#include<iostream>
8+
using namespace std;
9+
10+
int fib(int n){
11+
if (n <=1)
12+
return n;
13+
else
14+
return fib(n-1)+fib(n-2);
15+
}
16+
17+
int main(){
18+
int n;
19+
cin>>n;
20+
cout<<fib(n)<<endl;
21+
}

0 commit comments

Comments
 (0)