Sunday, April 12, 2020

c++ program to compute quotient and remainder

C++ Program to Compute Quotient and Remainder

#include 
using namespace std;

int main()
{    
    int divisor, dividend, quotient, remainder;

    cout << "Enter dividend: ";
    cin >> dividend;

    cout << "Enter divisor: ";
    cin >> divisor;

    quotient = dividend / divisor;
    remainder = dividend % divisor;

    cout << "Quotient = " << quotient << endl;
    cout << "Remainder = " << remainder;

    return 0;
}

Output

Enter dividend : 22
Enter divisor : 4
Quotient : 5
Remainder : 2

Happy Coding


Thanks for visiting my Blog.

Have any problem, feel free to contact us via Email : uoslondy@gmail.com

Have a good day!

Tuesday, April 7, 2020

c++ program to display name age and address

C++ program to display name age and address


#include 
using namespace std;

int main()
{
    int Number1, Number2, sum; 
    
    cout << "Enter  first integer number : ";
    cin >> Number1;
 cout << "Enter second integer number : ";
    cin >> Number2;
    // sum of two numbers in stored in variable sum
    sum = Number1 + Number2;

    // Prints sum 
    cout << Number1 << " + " <<  Number2 << " = " << sum;     

    return 0;
}

Output


Enter first number : 3
Enter second number : 5
3 + 5 = 8
Happy Coding

Thanks for visiting my Blog.

Have any problem, feel free to contact us via Email : uoslondy@gmail.com

Have a good day!

Monday, April 6, 2020

Thursday, April 2, 2020

c program to check given number is armstrong number or not

C program to check given number is armstrong number or not   

  • A Positive number is called Armstrong number.  

abcd.... = an + bn + cn + dn +

  • In this case of Armstrong number of 3 digits, the sum cubes of each digits is equal to the number of itself.

123 = 1*1*1 + 2*2*2 + 3*3*3
#include 
int main() 
{
    int num, originalNum, remainder, result = 0; // Integer values
    printf("Enter a three-digit integer: "); // Enter by user three digits
    scanf("%d", &num); // storing values
    originalNum = num; // replace number

    while (originalNum != 0) // while loop
    {
        // remainder contains the last digit
        remainder = originalNum % 10;
        
        result += remainder * remainder * remainder;
        
        // removing last digit from the orignal number
        originalNum /= 10;
    }

    if (result == num) // if else
        printf("%d is an Armstrong number.", num);
    else
        printf("%d is not an Armstrong number.", num);

    return 0;
}

Output

Enter three integer 371
371 is an Armstrong number
Happy coding

Thanks for visiting my Blog.

Have any problem, feel free to contact us via Email : uoslondy@gmail.com

Have a good day!

c program to print reverse of a number using while loop

C program to print reverse of a number using while loop


#include 
int main()
{
    int n, rev = 0, remainder; // Integer values
    printf("Enter an integer: "); // Enter user value
    scanf("%d", &n); // storing values
    
    while (n != 0) // while loop
    {
        remainder = n % 10;
        rev = rev * 10 + remainder;
        n /= 10;
    }
    printf("Reversed number = %d", rev); // finalize result
    return 0;
}
Output


Enter an integer : 2345
Reverse number is 5432
Happy Coding

Thanks for visiting my Blog.

Have any problem, feel free to contact us via Email : uoslondy@gmail.com

Have a good day!

c program to calculate power of a number

C program to calculate power of a number

  • The program takes two integer from user and then calculate power.
          For example : 2 pow 3 .
  • In case 2 is base number and 3 is exponent number.  (2*2*2)
#include 
int main() 
{
    int base, exp; // integer values 
    long long result = 1; // constant values
    printf("Enter a base number: "); // User enter 
    scanf("%d", &base); //storing values
    printf("Enter an exponent: "); // User enter 
    scanf("%d", &exp); // storing values

    while (exp != 0) // while loop
    {
        result = result * base;
        --exp;
    }
    printf("Answer = %lld", result); // finalize result
    return 0;
}
Output


Ente a base number 3
Enter exponent number 4
Answer = 81
Happy Coding

Thanks for visiting my Blog.

Have any problem, feel free to contact us via Email : uoslondy@gmail.com

Have a good day!

c program to check whether a number is palindrome or not using while loop

C program to check whether a number is palindrome or not using while loop

  • An integer is palindrome if the reverse of that number is equal to the original number.
#include 
int main() 
{
    int n, reversedN = 0, remainder, originalN;
    printf("Enter an integer: ");
    scanf("%d", &n);
    originalN = n;

    // reversed integer is stored in reversedN
    while (n != 0) 
 
    {
        remainder = n % 10;
        reversedN = reversedN * 10 + remainder;
        n /= 10;
    }

    // palindrome if orignalN and reversedN are equal
    if (originalN == reversedN)
        printf("%d is a palindrome.", originalN);
    else
        printf("%d is not a palindrome.", originalN);

    return 0;
}
Output


Enter an integer = 1001
1001 is palindrome 
Happy Coding

c program to check whether a character is uppercase or lowercase alphabet

c program to check whether a character is uppercase or lowercase alphabet 

#include 
int main()
{
    char c; // enter character value
    printf("Enter u to display uppercase alphabets.\n"); // U for uppcase
    printf("Enter l to display lowercase alphabets. \n"); // I for lowercase
    scanf("%c", &c); // storing values

    if (c == 'U' || c == 'u') 
     {
        for (c = 'A'; c <= 'Z'; ++c)
            printf("%c ", c);
     } else if (c == 'L' || c == 'l') 
 
     {
     for (c = 'a'; c <= 'z'; ++c)
            printf("%c ", c);
     } else 
 
     {
        printf("Error! You entered an invalid character.");
     }

    return 0;
}
Output


Enter u to display uppercase alphabets 
Enter l to display lowercase alphabets = I
a b c d e f g h i j k l m n o p q r s t u v w x y z 
Happy Coding

Thanks for visiting my Blog.

Have any problem, feel free to contact us via Email : uoslondy@gmail.com

Have a good day!

c program to display character from a to z using loop

C program to display character from a to z using loop

#include 
int main()
{
    char c; //Character variable
    for (c = 'A'; c <= 'Z'; ++c) // loop  character variable
        printf("%c ", c); // print final result
    return 0;
}
Output

 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 
  • In this program, loop is used to display the English alphabet in uppercase.


Happy Coding

c program to find lcm of two numbers using while loop

C program to find lcm of two numbers using while loop

  • The LCM of two numbers n1 and n2  is the smallest positive integer that is perfectly divisible by both n1 and n2. 
#include 
int main()
{
    int n1, n2, min; // integer variables
    printf("Enter two positive integers: "); // Entered user input
    scanf("%d %d", &n1, &n2);

    // maximum number between n1 and n2 is stored in min
    min = (n1 > n2) ? n1 : n2; 

    while (1) 
    {
        if (min % n1 == 0 && min % n2 == 0) 
   {
            printf("The LCM of %d and %d is %d.", n1, n2, min);
            break;
          }
        ++min;
    }
    return 0;
}
return 0;
}
Output

Enter tow positive integer  number = 72 120
The LCM of 72 and 120 is = 360 
Happy Coding

c program to calculate the gcd of two numbers

C program to calculate the gcd of two numbers

  • The HCF or GCD  of two integers is the largest integer that can exactly divided both numbers.
  • There are many ways to find the greatest common divisor in C program.
#include 
int main()
{
    int n1, n2, i, gcd; // integer variables

    printf("Enter two integers: "); // User enter two integers 
    scanf("%d %d", &n1, &n2); // storing integer variables

    for(i=1; i <= n1 && i <= n2; ++i) // for loop 
    {
        // Checks if i is factor of both integers
        if(n1%i==0 && n2%i==0)
            gcd = i;
    }

    printf("G.C.D of %d and %d is %d", n1, n2, gcd); // final result display

    return 0;
}
Output

Enter two integer = 81 -153
GCD = 9 
  • In this program, two integer is entered by the user are stored in variable n1 and  n2 . Then, for loop is iterated until i is less than n1 and n2. 
  • In each iterated,  if both n1 and n2  are exactly divided by i the value of i is assigned to GCD

Happy Coding  

c program fibonacci sequence upto certain number using while loop algorithm

C program fibonacci sequence upto certain number using while loop algorithm C program

#include 
int main()
{
    int a1 = 0, a2 = 1, nextTerm = 0, n; //  integer variables
    printf("Enter a positive number: "); // user input value
    scanf("%d", &n); // storing user entered value

    // displays the first two terms which is always 0 and 1
    printf("Fibonacci Series: %d, %d, ", a1, a2);
    nextTerm = a1 + a2;

    while (nextTerm <= n) // while loop 
    {
        printf("%d, ", nextTerm);
        a1 = a2;
        a2 = nextTerm;
        nextTerm = a1 + a2;
    }

    return 0;
}

Output

Enter a positive integer = 100
fibonacci series = 0,1,1,2,3,5,8,13,21,34,55,89,
Happy Coding

c program to display fibonacci series simple way example

C program to display fibonacci series simple way example 

#include 
int main()
{
    int i, n, t1 = 0, t2 = 1, nextTerm; // integer variables 
    printf("Enter the number of terms: "); // input from user
    scanf("%d", &n); // storing value entered from suer
    printf("Fibonacci Series: "); // It display the final result

    for (i = 1; i <= n; ++i) // using for loop 
    {
        printf("%d, ", t1);
        nextTerm = t1 + t2;
        t1 = t2;
        t2 = nextTerm;
    }

    return 0;
}
  • The fibonacci sequence is a sequence where the next term is the sum of its previous two terms. The two terms of terms of the fibonacci sequence followed o to 1.
The fibonacci sequence : 0,1,1,2,3,5,8,13,21,

Output

Enter the number of terms = 10
Fibonacci series 0,1,1,2,3,5,8,13,34,
Happy coding

C Program to Find Factorial of a Number

C Program Find Factorial of a Number  

  • The fractional of a positive number n is given.
The factorial of n (n!) = 1*2*3*4*5....n 
  • The factorial of a negative number doesn't exist.The factorial of 0 is 1.
#include 
int main()
{
    int n, i; // integer values
    unsigned long long fact = 1; // 32 bits ( 4 bytes)  
    printf("Enter an integer: "); // enter user 
    scanf("%d", &n); // storing value 

    // shows error if the user enters a negative integer
    if (n < 0)
        printf("Error! Factorial of a negative number doesn't exist.");
    else 
 {
        for (i = 1; i <= n; ++i)
      {
        
         fact *= i;
        
      }
        printf("Factorial of %d = %llu", n, fact);
    }

    return 0;
}

Output

Enter an integer = 10
Factorial of 10 is = 3628800

  • This program takes input from user positive number and computes the factorial using for loop. 
  • Since, the factorial of a number may be very large.the type of factorial variable is declared unsigned long long . 
  • If the user entered negative number, the program should display a custom error message.  
Happy coding

C program to Check leap year

C Program to Check leap year 

#include 
int main()
{
    int year; // integer value  
    printf("Enter a year: "); // enter user value 
    scanf("%d", &year); // storing user value
    if (year % 4 == 0) // check leap year
    {
        if (year % 100 == 0) // 
 {
            // the year is a leap year if it is divisible by 400.
            if (year % 400 == 0)
                printf("%d is a leap year.", year);
            else
                printf("%d is not a leap year.", year);
        } else
            printf("%d is a leap year.", year);
    } else
        printf("%d is not a leap year.", year);

    return 0;
}

Output 1

Enter a year = 1900
1900 is not leap year

Output 2

Enter a year = 2014
2014 is leap year

Happy coding

C program to sum of all natural number using while loop

C program to Sum of All Natural Number Using While Loop


#include 
int main()
{
    int n, i, sum = 0; // integer value
    printf("Enter a positive integer: "); // enter user positive number
    scanf("%d", &n); // storing number
    i = 1; 

    while (i <= n) // while loop 
    {
        sum = sum + i; // total sum
        ++i;
    }

    printf("Sum = %d", sum); // print sum numbers
    return 0;
}

Output

Enter positive integer = 100
Sum = 5050

  • In this program, loop is iterated n number of times. And, in each iterated, the value of  ' i '  is added to sum and  is increment by 1.  

C program to Sum of all natural number using for loop

C program to Sum of all Natural Number Using for loop 

The positive numbers are 1,2,3.............n are known as natural numbers. The sum of natural numbers are upto 10.
#include 
int main() 
{
    int n, i, sum = 0; // integer value declaration

    printf("Enter a positive integer: "); // enter user value
    scanf("%d", &n);

    for (i = 1; i <= n; ++i) // loop check natural number's
    {
        sum = sum + i; // 
    }

    printf("Sum = %d", sum); // print natural numbers
    return 0;
}

Output

Ente positive integer = 100
sum = 5050

  • The above program takes input program user and then store the variable in n .  Then, forloop is used to calculate the sum of all natural number's.

C program to check character is vowel or not

 C program to check character is vowel or not

  • Five letter 'A' 'E' 'I' 'O' 'U' are called vowels. All other alphabets excepts these vowels are called consonants.
#include 
int main()
{
    char c; // character value
    int lowercase, uppercase; // integer value
    printf("Enter an alphabet: "); // user enter character
    scanf("%c", &c); // storing value

    // evaluates to 1 if variable c is lowercase
    lowercase = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');

    // evaluates to 1 if variable c is uppercase
    uppercase = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');

    // evaluates to 1 if c is either lowercase or uppercase
    if (lowercase || uppercase)
        printf("%c is a vowel.", c);
    else
        printf("%c is a consonant.", c);
    return 0;
}

Output

Enter an alphabet : G
G is consonant.

  • The character entered by the user is stored in c.
  • The lowercase variable evaluated to 1 (true) if c is a lowercase vowel and 0 (false)  for any other character.
  • Similarly, the uppercase variable evaluated to 1 (true) if c  is an uppercase vowel and 0 (false) for any other character. 
  • if either lowercase or uppercase variable is 1( ture), the entered character is vowel. 
  • However, if both lowercase and uppercase variables are 0, the entered character is consonant.  
Happy Coding 

Recent Posts