Skip to main content

Posts

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...
Recent posts

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...

Largest of three numbers

#include <stdio.h> int main() { int a, b, c, largest; printf ("Enter three numbers : "); scanf ("%d%d%d", &a, &b, &c); largest = a; // Let 'a' be the largest number if(b > largest) largest = b; if(c > largest) largest = c; printf ("\n%d is the largest number.", largest); // To find the smallest number, replace '>' with '<' return 0; } Link : https://repl.it/@nithin_gudla/largest3numbers Enter three numbers : 52 98 64 98 is the largest number.

Compare two numbers

#include <stdio.h> int main() {    int a,b;    printf("Enter two numbers : ");    scanf("%d%d", &a, &b);    if(a > b)       printf("\n%d is greater than %d.", a, b);    else       printf("\n%d is greater than %d.", b, a);    return 0; } Enter two numbers : 5 7 7 is greater than 5.

Leap year or not

#include <stdio.h> int main() { int year; printf("Enter year : "); scanf("%d", &year); if (((year % 4 == 0) && (year % 100!= 0)) || (year % 400 == 0)) printf("\n%d is a leap year", year); else printf("\n%d is not a leap year", year); return 0; } Link :  https://repl.it/@nithin_gudla/leapyear Enter year : 2020 2020 is a leap year.

Whether a character is Vowel or Consonant

#include <stdio.h> int main() {     char ch;     printf("Enter an alphabet\t:\t");     scanf("%c", &ch);     if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'||        ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')         printf("\n'%c' is a vowel", ch);     else         printf("\n'%c' is a consonant", ch);     return 0; } Enter an alphabet : U 'U' is a vowel

Swapping (Exchanging) of two numbers without using a third variable

#include <stdio.h> int main() {   int a,b;   printf("Enter two numbers ");   scanf("%d%d", &a, &b );   printf("\nBefore swapping, a = %d and b = %d", a, b);   a = a + b;   b = a - b;   a = a - b;   printf("\nAfter swapping, a=%d and b=%d", a, b);   return 0; } Enter two numbers 7 9 Before swapping, a = 7 and b = 9 After swapping, a=9 and b=7

Swapping (Exchanging) of two numbers using third variable

#include <stdio.h> int main() {   int a,b,c;   printf("Enter two numbers ");   scanf("%d%d", &a, &b );   printf("\nBefore swapping, a = %d and b = %d", a, b);   c = a;   a = b;   b = c;   printf("\nAfter swapping, a=%d and b=%d", a, b);   return 0; } Enter two numbers 3 4 Before swapping, a = 3 and b = 4 After swapping, a=4 and b=3

Given number is +ve or -ve

#include <stdio.h> int main() {   int n;   printf("Enter a number\t:\t");   scanf("%d", &n );   if(n == 0)     printf("\n%d is a neutral number", n);   else   if(n > 0)     printf("\n%d is a positive number", n);   else     printf("\n%d is a negative number", n);   return 0; } Enter a number : -6 -6 is a negative number

Area and Perimeter of a circle

#include <stdio.h> #define pi 3.14   // defining pi=3.14 int main() {   float radius,area,perimeter;   printf("Radius\t:\t");   scanf("%f", &radius);   area = pi*radius*radius;   perimeter = 2*pi*radius;   printf("\nArea\t\t:\t%.2f", area);   printf("\nPerimeter\t:\t%.2f", perimeter);   return 0; } Radius : 5 Area : 78.50 Perimeter : 31.40

Area and Perimeter of a Rectangle

#include <stdio.h> int main() {   float length, breadth, area, perimeter ;   printf("Length\t:\t");   scanf("%f", &length );   printf("Breadth\t:\t");   scanf("%f", &breadth );   area=length*breadth;   perimeter=2*( length + breadth );   printf("\nArea\t:\t%f", area);   printf("\nPerimeter\t:\t%f", perimeter);   return 0; } Length : 5.64 Breadth : 4.86 Area : 27.41 Perimeter : 21.00

Division of two numbers

#include <stdio.h> int main() {   int a,b,rem,quo;   float finalresult;   printf("Enter two numbers\t:\t");   scanf("%d%d", &a , &b );   quo=a / b;   rem=a % b;   finalresult=(float) a / b;   printf("\nQuotient\t:\t%d", quo);   printf("\nRemainder\t:\t%d", rem);   printf("\nFinal Result\t:\t%f", finalresult);   return 0; } Enter two numbers : 24 9 Quotient : 2 Remainder : 6 Final Result : 2.666667

Absolute difference between two numbers

#include <stdio.h> #include<stdlib.h>  // since we are using abs() function int main() {   int a,b,c;   printf("Enter two numbers\t:\t");   scanf("%d%d", &a , &b );   c= abs (a - b);   printf("The absolute difference between %d and %d is %d", a, b, c);   return 0; } Enter two numbers : 5 6 The average of 5 and 6 is 1

Average of two numbers

#include <stdio.h> int main() {   int a, b;   float avg;   printf("Enter two numbers\t:\t");   scanf("%d%d", &a , &b );   avg = (float) (a + b) / 2;   printf("The average of %d and %d is %f", a, b, avg);   return 0; } Enter two numbers : 5 6 The average of 5 and 6 is 5.500000

'int main()' and 'void main()'

In C programming both the 'int main()' and 'void main()' are accepted. 'void main()' means that the main function doesn't return anything. void main() {     // statements   } 'int main()' says that the main function will return an integer to the operating system. For that we will return an integer to the operating system. int main() {     // statements     return 0; // since 0 is an integer } Some compilers are designed in such a way that they can't accept main function without returning anything, for those compilers we have to use 'int main()'.

Read input from the keyboard

To read input from the keyboard, we have to use a pre-defined function "scanf()". The syntax for "scanf()" function is scanf("format string", arguments list); A format string can be : %d (integer), %s (string) , %f (floating point number), %c (character) , %lf etc. Ex : scanf("%d", &input); // for single input       Here '%d' represents an integer and 'input' is the name assigned to that integer. scanf("%d%c", &input1 , &input2 ); // for multiple inputs To use 'scanf()' function also, we have to include a header file "stdio.h", i,e #include<stdio.h> or #include"stdio.h"

How to display text on monitor

We can display the text on monitor/ screen using a standard library function "printf()". The syntax for the "printf()" statement is printf(" "); printf(" Hi "); The above statement displays "Hi' on monitor. printf("format string", arguments list ); A format string can be  : %d (integer), %s (string) , %f (floating point number), %c (character) , %lf etc. Ex : printf ( " %d " , output); // for single output     Here '%d' represents an integer and 'output' is the name assigned to that integer. printf("%d%c", output1 , output2 ); // for multiple outputs To use printf statement in our program, we have to include a header file "stdio.h" i.e   #include<stdio.h> or #include"stdio.h"