C Program to find sum of two numbers

 Lets discuss a basic c program

  • Sum of two integers using c. This might be the first practice program for all users.
  • To check sum of two integers , read input from user and store in two corresponding integer variables.
  • Also take one more variable to store result of sum of two numbers
  • c= a+b
  • Print result using printf

sum of two numbers in c


  1. #include <stdio.h>
  2. // program to check sum of two numbers in c
  3. // write a c program to find sum of two integers
  4. // www.instanceofjava.com
  5. int main()
  6. {
  7.     int x, y, sum;
  8.     // read input from user
  9.     printf(" Please enter any two numbers: ");
  10.     scanf("%d %d", &x, &y);
  11.     // sum of two numbers
  12.     sum=x+y;
  13.     
  14.     printf("Sum of %d and %d is %d", x, y, sum);

  15.     getch();
  16. }
Output:

  1. Please enter any two numbers
  2. 10
  3. 20
  4. Sum of 10 and 20 is 30

C program to print pyramid pattern of numbers

 #1: C program to print pyramid pattern of n stars

  1. #include <stdio.h>
  2. //c program to print pyramid pattern of numbers
  3. // www.instanceofjava.com
  4. int main(){
  5.    int number,k,m;

  6.     printf("Number of lines of star pattern  ");
  7.     scanf("%d", &number);

  8.     printf("\n");

  9.     for(  k = 1; k <= number; k++ )
  10.     {
  11.         for( m = 1; m <= k; m++)
  12.         {
  13.             printf("*");
  14.         }
  15.         printf("\n");
  16.     }

  17.    getch();
  18. }

Output:
pattern program in c language



#2: C program to print pyramid pattern of n Numbers

  1. #include <stdio.h>
  2. //c program to print pyramid pattern of numbers
  3. // www.instanceofjava.com
  4. //pyramid pattern in c
  5. //write a c
  6.  program to print following pattern
  7. int main(){
  8.    int number,k,m;

  9.     printf("Number of lines of star pattern  ");
  10.     scanf("%d", &number);

  11.     printf("\n");

  12.     for(  k = 1; k <= number; k++ )
  13.     {
  14.         for( m = 1; m <= k; m++)
  15.         {
  16.            printf("%-5d ", m);
  17.         }
  18.         printf("\n");
  19.     }

  20.    getch();
  21. }

Output:

pyramid pattern in c


C program for addition subtraction multiplication and division using switch case

  • Write a C program to simulate a simple calculator to perform arithmetic operations like a and division only on integers. Error message should be reported if any attempt is made to divide by zero    
  • C program for addition subtraction multiplication and division using switch case
  • Write a c program to perform arithmetic operations using switch case
  • C program to add subtract multiply and divide two numbers using functions
Program #1: Write a c program to perform arithmetic operations using switch case



  1. #include <stdio.h>
  2. #include <conio.h>
  3.  
  4. int main()
  5. {
  6.     char oper;            /* oper is an operator to be selected */
  7.     float n1, n2, result;
  8.  
  9.     printf ("Simulation of a Simple Calculator\n\n");
  10.  
  11.     printf("Enter two numbers\n");
  12.     scanf ("%f %f", &n1, &n2);
  13.  
  14.     fflush (stdin);
  15.  
  16.     printf("Enter the operator [+,-,*,/]\n");
  17.     scanf ("%c", &oper);
  18.  
  19.     switch (oper)
  20.        {
  21.         case '+': result = n1 + n2;
  22.               break;
  23.         case '-': result = n1 - n2;
  24.               break;
  25.         case '*': result = n1 * n2;
  26.               break;
  27.         case '/': result = n1 / n2;
  28.               break;
  29.         default : printf ("Error in operation\n");
  30.               break;
  31.     }
  32.  
  33.     printf ("\n%5.2f %c %5.2f= %5.2f\n", n1,oper, n2, result);
  34. getch();
  35. }


Output:

algorithm for switch case in c


  • Output
    Simulation of Simple Calculator

    Enter two numbers
    3 5
    Enter the operator [+,-,*,/]
    +

     3.00 +  5.00=  8.00
     
  • RUN2
    Simulation of Simple Calculator

    Enter two numbers
    12.75
    8.45
    Enter the operator [+,-,*,/]
    -

    12.75 -  8.45=  4.30
  • RUN3
    Simulation of Simple Calculator

    Enter two numbers
    12 12
    Enter the operator [+,-,*,/]
    *

    12.00 * 12.00= 144.00
     
  • RUN4
    Simulation of Simple Calculator

    Enter two numbers
    5
    9
    Enter the operator [+,-,*,/]
    /

     5.00 /  9.00=  0.56

C program to find ASCII value of a string or character

  • Convert string to ascii c programming.
  • C program to find ascii value of a string or character.
  • C program to print ascii value of alphabets.
  • Now we will write a C program to print ASCII value of each character of a String.



Program #1: Write a C programming code to print ASCII value of String.


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6.     char str[100];
  7.     int i=0;
  8.     printf("Enter any string to get ASCII Value of each Character \n");
  9.     scanf("%s",str);
  10.  
  11.     printf("ASCII values of each characters of given string:\n ");
  12.     while(str[i])
  13.          printf("%d \n",str[i++]);
  14.         
  15.     getch();
  16. }


Output:


Print Ascii value of string character c program

C program to print 1 to 100 without using loop

  • Print 1 to 100 without using loop in c
  • We print 1 to n numbers without using any loop.
  • By using recursive function in C Programming we can print 1 to 100 or 1 to n numbers.
  • Now We will write a C program to print one to 100 without using any loops.
  • Print 1 to 100 without loop and recursion



 Program #1: Write a C program to print 1 to 100 without loop and using recursive function.


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int print(int num);
  4. int main(int argc, char *argv[])
  5. {
  6.     int number = 1;
  7.  
  8.     print(number);
  9.  
  10.    return 0;
  11. }
  12. int print(int number){
  13.     if(number<=100){
  14.          printf("%d ",number);
  15.          print(number+1);
  16.     }
  17. }
   
Output:


print 1 to 100 in c

C Program to swap two numbers without using third variable

  • C program to swap two numbers without using third variable.
  • Yes we can swap two variables without using third variable.
  • We have four different ways to swap two numbers without using third variable.
  • Lets see an example C program on how to swap two numbers without using third variable and without using function.



Program #1: Write a simple C program to swap two numbers without using third variable

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6.   int a,b;
  7.   printf("Please enter two integer numbers to swap\n");
  8.   scanf("%d%d",&a,&b);
  9.   printf("before swapping\n");
  10.   printf("a=%d\n",a); 
  11.   printf("b=%d",b);   
  12.   
  13.     a=b+a;
  14.     b=a-b;
  15.     a=a-b;
  16.     
  17.   printf("\nafter swapping\n");
  18.   printf("a=%d\n",a); 
  19.   printf("b=%d",b); 
  20.   getch();
  21.   
  22. }

Output:


swap without third variable c program



Program #2: Write a simple C program to swap two numbers without using third variable


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6.   int a,b;
  7.   printf("Please enter two integer numbers to swap\n");
  8.   scanf("%d%d",&a,&b);
  9.   printf("before swapping\n");
  10.   printf("a=%d\n",a); 
  11.   printf("b=%d",b);   
  12.  
  13.   a=a+b-(b=a); 
  14.     
  15.   printf("\nafter swapping\n");
  16.   printf("a=%d\n",a); 
  17.   printf("b=%d",b); 
  18.   getch();
  19.   
  20. }

Output:

  1. Please enter two integers numbers to swap
  2. 30
  3. 40
  4. before swapping
  5. a=30
  6. b=40
  7. after swapping
  8. a=40
  9. b=30

Program #3: Write a simple C program to swap two numbers without using third variable : call by value

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(int argc, char *argv[])
  4. {
  5.   
  6.   int a=12, b=13;
  7.     a=a^b;
  8.     b=a^b;
  9.     a=b^a;
  10.    printf("a=%d\n",a); 
  11.  printf("b=%d",b); 
  12.   getch();
  13.   
  14. }

  Output:

  1. a=13
  2. b=12

Program #4: Write a simple C program to swap two numbers without using third variable : call by value

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(int argc, char *argv[])
  4. {
  5.   
  6.   int a=12, b=13;
  7.      a=b-~a-1;
  8.     b=a+~b+1;
  9.      a=a+~b+1;
  10.    printf("a=%d\n",a); 
  11.    printf("b=%d",b); 
  12.  
  13.  return 0;
  14.   
  15. }

  Output:

  1. a=13
  2. b=12

C program to print patterns of numbers,alphabets and stars in a pyramid shape

  • We can print *'s  in pyramid pattern using C programming language.
  • We can also print  patterns of numbers and alphabets using C program.
  • Now Check below program that how to print pyramid pattern of * using C program using for loop.
  • C program to print patterns of numbers and stars in a pyramid shape



Program #1 : Write a C program to print pyramid pattern of numbers using for loop.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(){
  5.     
  6.    int i, j, rows;
  7.  
  8.     printf("Enter number of rows: \n");
  9.     scanf("%d",&rows);
  10.  
  11.     for(i=1; i<=rows; ++i)
  12.     {
  13.         for(j=1; j<=i; ++j)
  14.         {
  15.             printf("* ");
  16.         }
  17.         printf("\n");
  18.     }
  19.         
  20.    getch();
  21.     
  22. }
   
Output:


print pyramid pattern c program

 Program #2 : Write a C program to print pyramid pattern of numbers using for loop.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(){
  5.     
  6.    int i, j, rows;
  7.  
  8.     printf("Enter number of rows to print pyramid numbers pattern \n ");
  9.     scanf("%d",&rows);
  10.  
  11.     for(i=1; i<=rows; ++i)
  12.     {
  13.         for(j=1; j<=i; ++j)
  14.         {
  15.             printf("%d ",j);
  16.         }
  17.         printf("\n");
  18.     }
  19.         
  20.    return 0;
  21.     
  22. }

Output:

  1. Enter number of rows to print pyramid numbers pattern
  2.  10
  3. 1
  4. 1 2
  5. 1 2 3
  6. 1 2 3 4
  7. 1 2 3 4 5
  8. 1 2 3 4 5 6
  9. 1 2 3 4 5 6 7
  10. 1 2 3 4 5 6 7 8
  11. 1 2 3 4 5 6 7 8 9
  12. 1 2 3 4 5 6 7 8 9 10
  
Program #3 : Write a c program to print patterns of numbers and alphabets

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(){
  5.     
  6.    int i, j;
  7.     char input, alphabet = 'A';
  8.  
  9.     printf("Enter the uppercase character you want to print in last row: ");
  10.     scanf("%c",&input);
  11.  
  12.     for(i=1; i <= (input-'A'+1); ++i)
  13.     {
  14.         for(j=1;j<=i;++j)
  15.         {
  16.             printf("%c", alphabet);
  17.         }
  18.         ++alphabet;
  19.  
  20.         printf("\n");
  21.     }
  22.     
  23.     getch();
  24.     
  25. }

Output:

  1. Enter the uppercase character you want to print in last row:
  2.  I
  3. A
  4. BB
  5. CCC
  6. DDDD
  7. EEEEE
  8. FFFFFF
  9. GGGGGGG
  10. HHHHHHHH
  11. IIIIIIIII
Select Menu