Simple C++ Programs on Code Blocks

Pythonpedia
Simple C++ Programs on Code Blocks
1.Program to reverse a number?
Program:-
Pythonpedia
Code:-
#include <stdio.h>

int main()
{
   int n, reverse = 0;

   printf("Enter a number to reverse\n");
   scanf("%d", &n);

   while (n != 0)
   {
      reverse = reverse * 10;
      reverse = reverse + n%10;
      n       = n/10;
   }

   printf("Reverse of entered number is = %d\n", reverse);

   return 0;
}
Output:-
Pythonpedia
Enter a number to reverse
2142
Reverse of entered number is = 2412

Process returned 0 (0x0)   execution time : 17.345 s
Press any key to continue.
2.Program to calculate factorial?
Program:-
Pythonpedia
Code:-
#include <stdio.h>

int main()
{
  int c, n, fact = 1;

  printf("Enter a number to calculate its factorial\n");
  scanf("%d", &n);

  for (c = 1; c <= n; c++)
    fact = fact * c;

  printf("Factorial of %d = %d\n", n, fact);

  return 0;
}
Output:-
Pythonpedia
Enter a number to calculate its factorial
8
Factorial of 8 = 40320

Process returned 0 (0x0)   execution time : 3.750 s
Press any key to continue.
3.Program to perform some mathematic operations?
Program:-
Pythonpedia
Code:-
#include <stdio.h>

int main()
{
   int first, second, add, subtract, multiply;
   float divide;

   printf("Enter two integers\n");
   scanf("%d%d", &first, &second);

   add = first + second;
   subtract = first - second;
   multiply = first * second;
   divide = first / (float)second;   //typecasting

   printf("Sum = %d\n", add);
   printf("Difference = %d\n", subtract);
   printf("Multiplication = %d\n", multiply);
   printf("Division = %.2f\n", divide);

   return 0;
}
Output:-
Pythonpedia
Enter two integers
8 9
Sum = 17
Difference = -1
Multiplication = 72
Division = 0.89

Process returned 0 (0x0)   execution time : 5.719 s
Press any key to continue.
4.Program to calculate string length?
Program:-
Pythonpedia
Code:-
#include <stdio.h>
#include <string.h>

int main()
{
   char a[100];
   int length;

   printf("Enter a string to calculate it's length\n");
   gets(a);

   length = strlen(a);

   printf("Length of the string = %d\n",length);

   return 0;
}
Output:-
Pythonpedia
Enter a string to calculate it's length
please visit and share my blog pythoogram.
Length of the string = 42

Process returned 0 (0x0)   execution time : 49.518 s
Press any key to continue.
5.Program to reverse the string?
Program:-
Pythonpedia
Code:-
#include <stdio.h>
#include <string.h>

int main()
{
   char arr[100];

   printf("Enter a string to reverse\n");
   gets(arr);

   strrev(arr);

   printf("Reverse of the string is \n%s\n", arr);

   return 0;
}
Output:-
Pythonpedia
Enter a string to reverse
My name is surajit.
Reverse of the string is
.tijarus si eman yM

Process returned 0 (0x0)   execution time : 28.798 s
Press any key to continue.

-Thank you

Comments

Popular posts from this blog