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 

Tuesday, March 31, 2020

C program to check character is alphabet or not


C program to check character is alphabet or not


#include 
int main()
{
    char c; // character variables
    printf("Enter a character: "); // user enter character value
    scanf("%c", &c); // stpre value
    
        // check character or not
    if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) 
        printf("%c is an alphabet.", c);
    else
        printf("%c is not an alphabet.", c);
        
    return 0;
}

Output

Enter character *
* is not character

How program works ?

  • In this program, 'a'  is used instead of  97 and 'z' is instead of  122.
  • Similarly, "A" is used instead of 65 and "Z" is instead of 90

Happy coding

C Program check number is positive or negative using if else

C Program check number is positive or negative using if else 

#include 
int main()
{
    double num; // constant number
    printf("Enter a number: "); // enter user 
    scanf("%lf", &num); // storing constant value
    if (num <= 0.0) // check number is greater or not
 {
        if (num == 0.0)
            printf("You entered 0.");
        else
            printf("You entered a negative number.");
    } else
        printf("You entered a positive number.");
    return 0;
}

Output

Enter a number : 32
You entered Positive number

Happy Coding

C Program to Swap two number without using temporary variable

C program to Swap two Numbers Without Using Temporary Variable

#include 
int main()
{
    double a, b; // initialization constant variables
    printf("Enter a: "); // user enter number
    scanf("%lf", &a); // storing value of user number
    printf("Enter b: "); // user enter number
    scanf("%lf", &b); // storing value of user number

    // Swapping process
    a = a - b;
    b = a + b;
    a = b - a;

    printf("After swapping, a = %.2lf\n", a); //swaping 
    printf("After swapping, b = %.2lf", b); //swaping
    return 0;
}

Output

Enter a: 5.25
Enter b: -6.5
After swapping, a = -6.5
After swapping, b = 5.25

Happy Coding 

C program to swap two numbers

C program to swap two numbers

#include
int main()
{
      double first, second, temp; // initialization of constant value 
      printf("Enter first number: "); // user input value
      scanf("%lf", &first); // Storing value
      printf("Enter second number: "); // user input value
      scanf("%lf", &second); // Storing value

      // Value of first is assigned to temp
      temp = first;

      // Value of second is assigned to first
      first = second;

      // Value of temp (initial value of first) is assigned to second
      second = temp;

      printf("\nAfter swapping, firstNumber = %.2lf\n", first); // Final display result
      printf("After swapping, secondNumber = %.2lf", second); // Final display result
      return 0;
}

Output 

Enter first number: 4.6
Enter second number: 4.5

After swapping, firstNumber = 4.5
After swapping, secondNumber = 4.6
How program works ?

  • In this program, the temp variable is assigned the value of the  first  variable. 
  • Then, the value of the first variable is assigned to the  second variable.
  • Finally, temp  (Which hold the initial value of first ) is assigned to second.This completes the swap process.
Happy coding

Monday, March 30, 2020

c program to Find ASCII Value of a Character

C Program to Find ASCII Value of a Character

In C programing, a Character variables holds ASCII value (an integer number between 0 to 127)
rather than that character itself. That value is known as it's ASCII value.
For example, the ASCII value of  'A' is 65.
What this means that, if you assign 'A' to a character variable, 65 is stored in the variable rather than 'A' itself.
Now, how we can print ASCII value of character in C programming. 
#include 
int main()
 {  
    char c; // Character value
    printf("Enter a character: "); // user input 
    scanf("%c", &c);  // Storing value 
    
    // %d displays the integer value of a character
    // %c displays the actual character
    printf("ASCII value of %c = %d", c, c);
    
    return 0;
}

Output

Enter a character: D
ASCII value of G = 68
In this program, the user is asked to entered a character. The character is stored in variable  c.
When %d format string is used, 68( the ASCII value of  D) is displayed.
When  %c format string is used, 'D' itself displayed.

Happy coding

c program to find the size of int float double and char

C program to find the size of int, float, double and char 


#include
int main() 
{
    int intType; // integer value
    float floatType; // Constant value
    double doubleType; // double Constant value
    char charType; // Character value

    // sizeof evaluates the size of a variable
    printf("Size of int: %ld bytes\n", sizeof(intType));
    printf("Size of float: %ld bytes\n", sizeof(floatType));
    printf("Size of double: %ld bytes\n", sizeof(doubleType));
    printf("Size of char: %ld byte\n", sizeof(charType));
    
    return 0;
}

Output 

Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte
In this program, 4 variables Int, float, double  and char are declared.
Then, the size of each variables is computing using the sizeof operator. 

Happy coding

c program to multiplay two numbers

C Program to Multiply two number


#include 
int main()
{
    double a, b, product; // Initialization
    printf("Enter two numbers: "); //ask to user enter two numbers
    scanf("%lf %lf", &a, &b);  // storing values 
 
    // Calculating product
    product = a * b;

    // Result up to 2 decimal point is displayed using %.2lf
    printf("Product = %.2lf", product);
    
    return 0;
}

Output

Enter two numbers: 3.4
5.10
Product = 8.5

How program is work ?

  • In this program, the user is asked to entered two number which is stored in the variables a and b. 
  • Then, the product of a and b is evaluated and the result is stored in product.
product = a * b ;
  • Finally, Product is displayed on the screen using printf()
printf("Product = %.2lf", product);
Happy Coding

c program to add two integer numbers

C program to add  two integer number

#include 
int main()
{    

    int number1, number2, sum;
    
    printf("Enter two integers: "); // Value from user
    scanf("%d %d", &number1, &number2); // Storing value

    // calculating sum
    sum = number1 + number2;      
    
    printf("%d + %d = %d", number1, number2, sum); // Final result 
    return 0;
}

Output

Enter two integers: 12
11
12 + 11 = 23

How program works ?

  • In this program, the user asked to enter two integer values. These integer values are stored in number1 and number2
  • Then, these two integer number are adding in ' + ' operator, and result is stored in the sum variable.
Sum = number1 + number2;
  • Finally, printf()  function is used to display the sum of two integer number.
printf("%d + %d = %d", number1, number2, sum); // Final result 

c program hello world program

C Program to display " hello world "

#include 
int main()
{
   // printf() displays the string inside quotation
   
   printf("Hello, World!");
   
   return 0;
}

Output 

Hello, World!

How "hello world" program works ?

  • The #include is preprocessor command that tells the compiler to include the content of stdio.h ( Standard input and output ) file in the program.
  • The stdio.h file contains function such as scanf() and print() to takee input and display output respectively.
  • If you see the printf() function without writing #include<stdio.h>, the program will ot be compiled.
  • The execution of C program starts from the main() function.
  • printf() is a library function to send formatted output to the screen. In this program, printf() display hello world to the screen.
  • The return 0; statement is the Execution of the program. In simple terms. The program ends with this statement.  

Sunday, March 29, 2020

What is Input Output in C Language .

Input and Output (I/O)

In this you will learn to use scanf() function to take input from user and printf() function to display the result to the user.

C Output

In C programming, Printf() funcation is the main output function to display result to user. The function sends formatted output to the screen.

Example 1 : C Output 

                                                                                                                                                                                                                                                                                                                                                                                                                         


Output
C Programming

How does this program work

  • All valid C program must contain the main()  function.The codes execution begins from the start the main() function.
  • The printf() is a library function to send formatted output to the screen. The function prints the string inside the quotations.
  • To use prinf() in our program, we need to include stdio.h  header file the #include<stdio.h> statement.
  • The return 0; inside the main() function is the " Exist status " of the program.

Example 2 : C Integer Output 

  

                                                                              







Output
Number =5

We use %d specifier to print int types. Here, the %d inside the quotations will be replaced the value of testInteger.

Example 3 : C float and double Output











Output
number1 = 13.5000
number2 = 12.4000

To print float, we use %f  format specifier, similarly we use %if to print double
values.
Example 4 : C print character








Output
character = a
To print char , we use %c format specifier.

Input

In C programming, scanf is one of the commonly used function to take input from the user. The scanf() function reads formatted input from the standard input such as keyword. 

Example 5 : C integer Input/Output












Output 
Enter an integer : 4
Result 
Number = 4 
Here, we have used %d format specifier inside the scanf() function to take int from the user. When the user entered an integer, it is stored  in the testinteger variable.

Example 6 : C float and double Input/Output



















Output
Enter a number =12.54
Enter another number =34.54
num1=12.54
num2=34.54

We use %f and %if format specifier for float and double respectively.

Example 7 : C Character Input/Output













Output
Enter a character : a
You entered a

When a character entered by the user in the above program, the character itself is not stored. Instead, an integer value ASCII is stored.
And when we display that value using  %c text format, the entered character is displayed. If we use %d to display the character, its ASCII value is printed.

Example 8 : C ASCII value Input/Output















Output
When a character :g
you entered :g
ASCII value is 103

Input/Output Multiple Values












Output
Enter integer and then float :  -3
3.5
You entered -3 and -3.50000

Format Specifier for I/O

  • %d      <=====>    Interger_value
  • %f       <=====>   float_value
  • %if      <=====>   double_value
  • %c      <=====>   Character_value

Recent Posts