Showing posts with label Operations. Show all posts
Showing posts with label Operations. Show all posts

Write a C program to perform math operations on two input whole numbers. The operations are:

Write a C program to perform math operations on two input whole numbers. The operations are:


a) Compute square root of n1
b) Compute square root of n2
c) Raise n1 to the power of n2
d) Inverse n1
e) Inverse n2


  1. //Write a C program to perform math operations on two input whole numbers. The operations are:
  2. //a) Compute square root of n1
  3. //b) Compute square root of n2
  4. //c) Raise n1 to the power of n2
  5. //d) Inverse n1
  6. //e) Inverse n2
  7. #include<iostream>
  8. #include<math.h>
  9. using namespace std;
  10. int main()
  11. {
  12.     int n1,n2;
  13.     cin>>n1>>n2;
  14.     cout<<sqrt(n1)<<endl;
  15.     cout<<sqrt(n2)<<endl;
  16.     int aux=n1;
  17.     for(int i=1;i<n2;i++)
  18.         aux=aux*n1;
  19.     cout<<aux<<endl;
  20.     aux=0;
  21.     while(n1>0)
  22.     {
  23.         aux=aux*10+n1%10;
  24.         n1=n1/10;
  25.     }
  26.     cout<<aux<<endl;
  27.     aux=0;
  28.     while(n2>0)
  29.     {
  30.         aux=aux*10+n2%10;
  31.         n2=n2/10;
  32.     }
  33.     cout<<aux<<endl;
  34. }

C Program Operation

How to write a C Program Operation in C Programming Language ?


Solution For C Program :

C Program to Calculate 2 numbers with 1 operation

How to write a C Program to Calculate 2 numbers with 1 operation in C Programming Language ?


Reverse Polish Notation - C Program to Calculate 2 numbers with 1 operation.

Solution:

  1. #include<stdio.h>
  2.  
  3. //calculate 2 numbers with 1 operation
  4. double calc (char operator, double a, double b) {
  5.         double result = 0;
  6.        
  7.         switch (operator) {
  8.                 case '+':
  9.                         result = a + b;
  10.                         break;
  11.                 case '-':
  12.                         result = a - b;
  13.                         break;
  14.                 case '*':
  15.                         result = a * b;
  16.                         break;
  17.                 case '/':
  18.                         result = a / b;
  19.                         break;
  20.         }
  21.  
  22.         printf("[DEBUG] %lf %lf %c = %lf\n", a, b, operator, result);
  23.  
  24.         return result;
  25. }
  26.  
  27. int main (void) {
  28.         //declare new char array in c style
  29.         char term[256] = {0};
  30.  
  31.         //declare variable for result
  32.         double result = 0;
  33.  
  34.         printf("Please insert your calculation: \n");
  35.  
  36.         //read term from input stream stdin
  37.         fgets(term, 255, stdin);
  38.  
  39.         printf("Your input: %s\n", term);
  40.  
  41.         //declare variables for numbers between operation
  42.         double a = 0;
  43.         double b = 0;
  44.  
  45.         int k = 0;
  46.         char lastNumber[256] = {0};
  47.         int numberCounter = 0;
  48.         int lineSpaceCounter = 0;
  49.         int lastCharType = 0; //1 - number, 2 - operator
  50.  
  51.         //parse string
  52.         for (int i = 0; i < 256; i++) {
  53.                 //check if char is line break
  54.                 if (term[i] == '\n') {
  55.                         break;
  56.                 }
  57.  
  58.                 //converts '0' to 0, '1' to 1
  59.                 double number = term[i] - '0';
  60.  
  61.                 if (number >= 0 && number <= 9) {
  62.                         //its an number, add number to term
  63.                         lastNumber[k] = term[i];
  64.  
  65.                         //increment counter
  66.                         k++;
  67.  
  68.                         lineSpaceCounter = 0;
  69.                         lastCharType = 1;
  70.                 } else if (term[i] == ' ') {
  71.                         //its an line space, so save last number
  72.  
  73.                         //convert char array to integer
  74.                         double n = 0;
  75.  
  76.                         sscanf(lastNumber, "%lf", &n);
  77.                         printf("[DEBUG] %s %lf\n", lastNumber, n);
  78.  
  79.                         //save last number into a or b
  80.                         if (numberCounter == 0) {
  81.                                 //save lastNumber into a
  82.                                 a = n;
  83.                         } else {
  84.                                 //save lastNumber into b
  85.                                 b = n;
  86.                         }
  87.  
  88.                         //increment line space counter, break on 2 line spaces too
  89.                         lineSpaceCounter++;
  90.                         numberCounter++;
  91.                        
  92.                         //because an number size be more than 1 char, we need this counter variable
  93.                         k = 0;
  94.                 } else if (term[i] == '+' || term[i] == '-' || term[i] == '*' || term[i] == '/') {
  95.                         //its an operator, so calculate sub term
  96.                         result = calc(term[i], a, b);
  97.                         a = result;                    
  98.  
  99.                         numberCounter = 1;
  100.                         k = 0;
  101.  
  102.                         lineSpaceCounter = 0;
  103.                         lastCharType = 2;
  104.                 }
  105.         }
  106.  
  107.         //print result
  108.         printf("Result: %lf\n", result);
  109.        
  110.         return 0;
  111. }
  1. /*
  2. * Copyright (c) 2015 Justin Kuenzel, All Rights reserved.
  3. *
  4. * MIT License, https://opensource.org/licenses/MIT
  5. */

Menu driven program in C which performs the following operations on strings

How to write a menu driven program in C which performs the following operations on strings:
  1. Write separate function for each option 
  2. Check if one string is sub-string of another string 
  3. Count number of occurrences of a character in the string 
  4. Exit

Solution:

#include<stdio.h>
#include<conio.h>
#include<string.h>
  void substring();
  void char_occurrence();
  void main()
{
  char str[30],str1[30];
  int ch;
  clrscr();
  do
{
  printf("\n****MENU****");
  printf("\n1:Sub String");
  printf("\n2:Character Occurrences");
  printf("\n3:Exit");
  printf("\nEnter your choice: ");
  scanf("%d",&ch);

  switch(ch)
{
  case 1:substring();
  break;

  case 2:char_occurrance();
  break;

  case 3:exit(0);
  break;
}
}
  while(ch!=3);
  getch();
}

  void substring()
{
  char str[30],ch[30];
  fflush(stdin);
  printf("\nEnter the first string: ");
  gets(str);
  fflush(stdin);
  printf("\nEnter the second string: ");
  gets(ch);
  if(strstr(str,ch))
{
  printf("\nThe given string is sub string");
}
  else
{
  printf("\nThe given string is not sub string");
}
}

  void char_occurrennce()
{
  char str2[30],ch;
  int i,count=0;
  fflush(stdin);
  printf("\nEnter the string: ");
  gets(str2);
  fflush(stdin);
  printf("\nEnter the character from string to find occurrence: ");
  scanf("%c",&ch);
  for(i=0;str2[i]!='\0';i++)
{
  if(ch==str2[i])
{
  count++;
}
}
  printf("\nThe occurrence of character is: %d",count);
}

//OUTPUT:
//****MENU****
//1:Sub String
//2:Character Occurrences
//3:Exit

//Enter your choice: 1

//Enter the first string: vinit

//Enter the second string: vinit

//The given string is sub string

//****MENU****
//1:Sub String
//2:Character Occurrences
//3:Exit
//Enter your choice: 2

//Enter the string: vinit

//Enter the character from string to find occurrence: v

//The occurrence of character is: 1

//****MENU****
//1:Sub String
//2:Character Occurrences
//3:Exit
//Enter your choice: 3