Skip to main content

Posts

Showing posts from 2019

Doors and Students

Problem : There are 1000 students and 1000 doors. 1st person opens all the 1000 doors. 2nd person flips (closes if it is opened, opens if it is closed) 2nd, 4th, 6th,... doors. 3rd person flips 3rd, 6th, 9th, 12th, .... doors. Similarly all the students flips the doors accordingly. Find the number of opened doors. #include"stdio.h" int main() { int a[1001]; for(int i = 1; i < 1001; i++) a[i] = 1; // First person opens all the doors. for(int j = 2; j <= 1000; j++) { // Outer loop for remaining 999 people. for(int k = j; k < 1001; k = k+j) { // Loop for flipping the doors accordingly. if(a[k] == 1) a[k] = 0; else a[k] = 1; } } int count = 0; for(int l = 1; l < 1001; l++) { // Loop for counting the opened doors. if(a[l] == 1) count++; } printf("\n\nThe opened doors are : %d...

Reverse a given number

#include <stdio.h> #include <math.h> int main() { int n, n2 = 0 , rem; printf("Enter a number : "); scanf("%d" , &n ); while( n > 0 ) { rem = n % 10; n2 = n2 * 10 + rem; n = n / 10; } printf("\n\nThe reversed number is : %d\n\n", n2); } Link :  https://repl.it/@nithin_gudla/reverseanumber

Sum of digits in a number

#include <stdio.h> #include <math.h> int main() { int n, sum = 0, rem; printf("Enter a number : "); scanf("%d" , &n ); while( n > 0 ) { rem = n % 10; sum = sum + rem; n = n / 10; } printf("\n\nThe sum is : %d\n\n", sum); } Link :  https://repl.it/@nithin_gudla/sumofdigits

Prime numbers from 1 to 'n'

#include <stdio.h> #include <math.h> int main() { int n, flag, count = 0; printf("Enter a number : "); scanf("%d" , &n ); printf("\n\nThe prime numbers from 1 to %d are : \n\n", n ); for(int i = 2; i <= n ; i++ ) { flag = 0; for(int j = 2 ; j <= sqrt(i) ; j++ ) { if( i % j == 0 ) { flag = 1; break; } } if(flag == 0) { printf("%d ", i); count++; } } printf("\n\nTotal count : %d\n\n", count); } Link :  https://repl.it/@nithin_gudla/prime

Given Number is Prime or Not (Efficient Algorithm)

#include <stdio.h> #include <math.h> int main() { int n, flag = 0; printf("Enter a number : "); scanf("%d" , &n ); if(n < 2) { printf("\nNumber should be greater than 2"); return 0; } for(int i = 2 ; i <= sqrt(n) ; i++ ) { if( n % i == 0 ) { flag = 1; break; } } if(flag == 0) printf("\n%d is a Prime number", n); else printf("%d is not a Prime number", n); } Link : https://repl.it/@nithin_gudla/primeefficient

Given number is Prime or Not (General Procedure)

#include <stdio.h> int main() { int n, count = 0; printf("Enter a number : "); scanf("%d" , &n ); for(int i = 2 ; i < n; i++ ) { if( n % i == 0 ) count++; // counting the number of factors. } if( count == 0 ) printf("\n%d is a Prime number", n); else printf("%d is not a Prime number", n); } Link : https://repl.it/@nithin_gudla/primegeneral

Factors of a number

#include <stdio.h> int main() { int n; printf("Enter the value of n : "); scanf("%d" , &n ); printf("\nThe factors of %d are : \n\n", n ); for(int i = 1 ; i <= n; i++ ) { if( n % i == 0 ) printf("%d ", i); } } Link : https://repl.it/@nithin_gudla/factors

Multiplication table of 'n'

#include <stdio.h> int main() { int n; printf("Enter the value of n : "); scanf("%d" , &n ); printf("\n"); for(int i = 1 ; i <= 10; i++ ) { printf("%d * %d = %d\n", n, i, n*i ); } } Link : https://repl.it/@nithin_gudla/multiplicationtable

'k' th power of 'n' - ( n ^ k )

#include <stdio.h> int main() { int n, k, result; printf("Enter the value of n : "); scanf("%d" , &n ); printf("\nEnter the value of k : "); scanf("%d" , &k ); printf("\n"); result = n; for(int i = 1 ; i < k; i++ ) { result = result * n; } printf("%d ^ %d is %d", n, k, result); } Link : https://repl.it/@nithin_gudla/power

Factorial of a number

#include <stdio.h> int main() { int n, fact = 1; printf("Enter the value of n : "); scanf("%d" , &n ); printf("\n"); for(int i = 1 ; i <= n; i++ ) { fact = fact * i; } printf("The factorial of %d is %d", n, fact); } Link : https://repl.it/@nithin_gudla/factorial

Sum of 'n' natural numbers

#include <stdio.h> int main() { int n, sum = 0; printf("Enter the value of n : "); scanf("%d" , &n ); printf("\n"); for(int i = 1 ; i <= n; i++ ) { sum = sum + i; } printf("The sum of %d natural numbers is %d", n, sum); } Link : https://repl.it/@nithin_gudla/sumnnaturalnumbers

Display even numbers from 1 to 'n'

#include <stdio.h> int main() { int n; printf("Enter the value of n : "); scanf("%d" , &n ); printf("\n"); for(int i = 1 ; i <= n; i++ ) { if(i % 2 == 0) printf("%d " , i ); } } Link : https://repl.it/@nithin_gudla/evennumbers

Display numbers from 1 to 'n'

#include <stdio.h> int main() { int n; printf("Enter the value of n : "); scanf("%d" , &n ); printf("\n"); for(int i = 1 ; i <= n; i++ ) { printf("%d " , i ); } } Link :  https://repl.it/@nithin_gudla/numbers1n

Display numbers from 1 to 100

#include"stdio.h" int main() { for(int i = 1; i <= 100 ; i++ ) { printf("%d ", i); } return 0; } Link : https://repl.it/@nithin_gudla/numbers1100

Display all alphabets

#include"stdio.h" int main() { for(char ch = 'A'; ch <= 'Z' ; ch++ ) { printf("%c ", ch); } return 0; } Link :  https://repl.it/@nithin_gudla/alphabets

Display "Hello World" 5 times

#include<stdio.h> int main() { for(int i = 1; i <= 5; i++) { printf("Hello World"); printf("\n"); // New Line for each iteration. } return 0; } Link : https://repl.it/@nithin_gudla/helloworld5times

Loops in C

When we want to perform a similar task multiple times, we can use the concept of loops. Basically there are three types of loops in C language. 1. For loop 2. While loop 3. Do - While loop We can also implement loops inside other loops which is known as nested loops concept. For Loop :  The syntax for the "for" loop is  for ( initialization ; condition ; increment / decrement / changes ) {          // Statements to be executed repeatedly } The for loop is executed repeatedly until the condition becomes false. While Loop  :  The syntax for the "while" loop is  while( condition ) {          // Statements to be executed repeatedly } The while loop is executed repeatedly until the condition becomes false. Do - While Loop  :  The syntax for the "do - while" loop is  do {          // Statements to be...