Posts

Swapping two values with help of pointer

Image
  #include <stdio.h>  #include<conio.h> int main() {    int x, y, *a, *b, temp;      printf("Enter the value of x and y\n");    scanf("%d%d", &x, &y);      printf("Before Swapping\nx = %d\ny = %d\n", x, y);      a = &x;    b = &y;      temp = *b;    *b = *a;    *a = temp;      printf("After Swapping\nx = %d\ny = %d\n", x, y);      return 0; } Output:- Enter the value of x and y 3 4 Before Swapping x = 3 y = 4 After Swapping x = 4 y = 3

Calculate Factorial of a given no with help of Recursion

Image
  #include<stdio.h> #include<conio.h> int fact(int); int main() {     int x,n;     printf(" Enter the Number to Find Factorial :");     scanf("%d",&n);       x=fact(n);     printf(" Factorial of %d is %d",n,x);       return 0; } int fact(int n) {     if(n==0)         return(1);     return(n*fact(n-1)); } Output:- Enter the Number to Find Factorial :3  Factorial of 3 is 6

Addition, Subtraction, Multiplication and Division with Function

Image
#include<stdio.h> #include<conio.h> // functions declaration int add(int n1, int n2); int subtract(int n1, int n2); int multiply(int n1, int n2); int divide(int n1, int n2); // main function int main() {   int num1, num2;   printf("Enter two numbers: ");   scanf("%d %d", &num1, &num2);   printf("%d + %d = %d\n", num1, num2, add(num1, num2));   printf("%d - %d = %d\n", num1, num2, subtract(num1, num2));   printf("%d * %d = %d\n", num1, num2, multiply(num1, num2));   printf("%d / %d = %d\n", num1, num2, divide(num1, num2));   return 0; } // function to add two integer numbers int add(int n1, int n2) {   int result;   result = n1 + n2;   return result; } // function to subtract two integer numbers int subtract(int n1, int n2) {   int result;   result = n1 - n2;   return result; } // function to multiply two integer numbers int multiply(int n1, int n2) {   int result;   result = n1 * n2; ...

Find max. and min. element in the integer array

Image
#include<stdio.h> #include <conio.h> int main() {     int a[1000],i,n,min,max;         printf("Enter size of the array : ");     scanf("%d",&n);       printf("Enter elements in array : ");     for(i=0; i<n; i++)     {         scanf("%d",&a[i]);     }       min=max=a[0];     for(i=1; i<n; i++)     {          if(min>a[i])   min=a[i];       if(max<a[i])     max=a[i];            }      printf("minimum of array is : %d",min);           printf("\nmaximum of array is : %d",max);         return 0; } Output:- Enter size of the array: 5 Enter elements in array: 1 2 3 4 5 minimum of an array is: 1 maximum of an array is: 5

Check whether the no entered is Armstrong or not

Image
 #include<stdio.h>  #include<conio.h>  int main()     {     int n,r,sum=0,temp;     printf("enter the number=");     scanf("%d",&n);     temp=n;     while(n>0)     {     r=n%10;     sum=sum+(r*r*r);     n=n/10;     }     if(temp==sum)     printf("armstrong  number ");     else     printf("not armstrong number");     return 0;   }    Output:- enter the number=153 armstrong number enter the number=5 not armstrong number

Check whether the no entered is Palindrome or not

Image
 #include<stdio.h>  #include<conio.h> int main()     {     int n,r,sum=0,temp;     printf("enter the number=");     scanf("%d",&n);     temp=n;     while(n>0)     {     r=n%10;     sum=(sum*10)+r;     n=n/10;     }     if(temp==sum)     printf("palindrome number ");     else     printf("not palindrome");    return 0;   }    Output:- enter the number=14341 palindrome number enter the number=1341 not palindrome

Check whether the enter year is leap or not

Image
 #include <stdio.h> #include<conio.h> int main() { int yr;   printf ("Enter a year \n");   scanf ("%d",&yr);   if (yr%4 == 0) {       if(yr%100 == 0) {                  if(yr%400 == 0)              printf(" It is LEAP YEAR.");           else              printf(" It is NOT LEAP YEAR.");       }       else {              printf (" It is LEAP YEAR.");       }   }   else       printf(" It is NOT LEAP YEAR.");    return 0; } Output:- Enter a year 2004 It is LEAP YEAR Enter a year 2003 It is NOT LEAP YEAR

Print a Fibonacci series

Image
  #include<stdio.h> #include<conio.h> int main( ) {    int n, a=1, b=1, temp;        printf("Enter the number");    scanf("%d",&n);   printf("Fibonacci series is %d %d ", a,b);   while(a+b<=n)     {        temp=a;        a=b;        b=a+temp;        printf("%d ",b);     } return 0; } Output:- Enter the number6 Fibonacci series is 1 1 2 3 5  

Print multiplication of two matrix

Image
  #include <stdio.h>  #include<conio.h> int main() {   int m, n, p, q, c, d, k, sum = 0;   int first[10][10], second[10][10], multiply[10][10];     printf("Enter number of rows and columns of first matrix\n");   scanf("%d%d", &m, &n);   printf("Enter elements of first matrix\n");     for (c = 0; c < m; c++)     for (d = 0; d < n; d++)       scanf("%d", &first[c][d]);     printf("Enter number of rows and columns of second matrix\n");   scanf("%d%d", &p, &q);     if (n != p)     printf("The multiplication isn't possible.\n");   else   {     printf("Enter elements of second matrix\n");       for (c = 0; c < p; c++)       for (d = 0; d < q; d++)         scanf("%d", &second[c][d]);       for (c = 0; c < m; c++) {       for (d = 0; d ...

Print subtraction of two matrix

Image
 #include < stdio.h >    #include<conio.h> int main()    {       int m, n, c, d, first[10][10], second[10][10], difference[10][10];       printf("Enter the number of rows and columns of matrix\n");       scanf("%d%d", & m, & n);       printf("Enter the elements of first matrix\n");       for (c = 0; c < m; c++)           for (d = 0; d < n; d++) scanf("%d", & first[c][d]);       printf("Enter the elements of second matrix\n");       for (c = 0; c < m; c++)           for (d = 0; d < n; d++) scanf("%d", & second[c][d]);       printf("Difference of entered matrices:-\n");       for (c = 0; c < m; c++)        {           for (d = 0; d < ...

Print addition of two matrix

Image
  #include <stdio.h> #include<conio.h> int main() {    int m, n, c, d, first[10][10], second[10][10], sum[10][10];      printf("Enter the number of rows and columns of matrix\n");    scanf("%d%d", &m, &n);    printf("Enter the elements of first matrix\n");      for (c = 0; c < m; c++)       for (d = 0; d < n; d++)          scanf("%d", &first[c][d]);      printf("Enter the elements of second matrix\n");      for (c = 0; c < m; c++)       for (d = 0 ; d < n; d++)          scanf("%d", &second[c][d]);        printf("Sum of entered matrices:-\n");        for (c = 0; c < m; c++) {       for (d = 0 ; d < n; d++) {          sum[c][d] = first[c][d] + second[c][d];          pr...

Calculate sum of the digits of a given number

Image
  #include<stdio.h> #include<conio.h> void main() { clrscr(); int a,b,c; printf("Enter Two Numbers :- "); scanf("%d%d",&a,&b); c=a+b; printf("Sum Of Numbers:- %d",c); getch(); } Output:- Enter Two Numbers :- 2 3 Sum Of Numbers:- 5

Check character entered is a special character or not

Image
  #include<stdio.h>  #include<conio.h> int main() {    char ch;      printf("\nEnter The Character : ");    scanf("%c", &ch);      if (ch >= 'A' && ch <= 'Z') {       printf("Character is not Special");    } else if (ch >= 'a' && ch <= 'z') {       printf("Character is not Special");    } else {       printf(" Character Is Special");    }      return(0); }   Output:- Enter The Character : w Character is not Special Enter The Character : @  Character Is Special

Check character entered is in lower case or not

Image
#include<stdio.h>  #include<conio.h> int main() {    char ch;      printf("\nEnter The Character : ");    scanf("%c", &ch);      if (ch >= 'A' && ch <= 'Z') {       printf("Character is in uppercase Letters");    } else if (ch >= 'a' && ch <= 'z') {       printf("Character is in Lowercase Letters");    } else {       printf("Non alphabet character");    }      return(0); }   Output:- Enter The Character : r Character is in Lowercase Letters

find the greatest value out of three numbers

Image
#include <stdio.h> #include<conio.h> int main() { int a, b, c; printf("Enter a,b,c: "); scanf("%d %d %d", &a, &b, &c); if (a > b && a > c) { printf("a is Greater than b and c"); } else if (b > a && b > c) { printf("b is Greater than a and c"); } else if (c > a && c > b) { printf("c is Greater than a and b"); } else { printf("all are equal or any two values are equal"); } return 0; } Output:- Enter a,b,c: 2 3 4 c is Greater than a and b

Use of conditional Operators

Image
#include <stdio.h>   #include<conio.h> int main()   {       int age;  // variable declaration       printf("Enter your age");       scanf("%d",&age);   // taking user input for age variable       (age>=18)? (printf("eligible for voting")) : (printf("not eligible for voting"));  // conditional operator       return 0;   }   Output:- Enter your age13 not eligible for voting Enter your age19 eligible for voting

Using Increment/Decrement Operators

Image
#include<stdio.h>  #include<conio.h> int main() {  int x = 10,y = 20;  printf("----INCREMENT OPERATOR EXAMPLE---- \n");  printf("Value of x : %d \n", x); //Original Value  printf("Value of x : %d \n", x++); // Using increment Operator  printf("Value of x : %d \n", x); //Incremented value    printf("----DECREMENT OPERATOR EXAMPLE---- \n");  printf("Value of y : %d \n", y); //Original Value  printf("Value of y : %d \n", y--); // using decrement Operator  printf("Value of y : %d \n", y); //decremented value  return 0; } Output:- ----INCREMENT OPERATOR EXAMPLE----  Value of x : 10  Value of x : 10  Value of x : 11  ----DECREMENT OPERATOR EXAMPLE----  Value of y : 20  Value of y : 20  Value of y : 19 

Swapping Without Using Third Variable

Image
  #include<stdio.h>   #include<conio.h>  int main()     {     int a, b;       printf("Enter Two Numbers:- ");  scanf("%d%d",&a,&b); a=a+b;     b=a-b;    a=a-b; printf("\nAfter swap a=%d b=%d",a,b);     return 0;   }    Output:- Enter Two Numbers:- 2 3 After swap a=3 b=2

Swapping Variable Using Third Variable

Image
#include <stdio.h> #include<conio.h> int main() { int var1, var2, temp; printf("Enter two integers"); scanf("%d%d", &var1, &var2); printf("Before Swapping\nFirst variable = %d\nSecond variable = %d\n", var1, var2); temp = var1; var1 = var2; var2 = temp; printf("After Swapping\nFirst variable = %d\nSecond variable = %d\n", var1, var2); return 0; } Output:- Enter two integers2 3 Before Swapping First variable = 2 Second variable = 3 After Swapping First variable = 3 Second variable = 2

Swapping Two Values With The Help Of Pointer

Image
  #include<stdio.h> #include<conio.h> void main() { int a,b,c; printf("Enter Two Numbers :-"); scanf("%d%d",&a,&b); c=a+b; printf("Sum Of Numbers:- %d",c); getch(); } Output:- Enter Two Numbers :-2 3 Sum Of Numbers:- 5

Get a string Using gets() function

Image
                                  #include<stdio.h> #include<conio.h> void main() { char str[50]; printf("Enter a String:- "); gets(str); printf("You entered : %s", str); getch(); } Output:- Enter a String:- hello You entered : hello

Printing Reverse Of Given Number

Image
  #include<stdio.h> #include<conio.h> void main() { int num,res; clrscr(); printf("Enter A Number :-  "); scanf("%d",&num); while(num>0) { res=num%10; printf("%d",res); num=num/10; } getch(); } Output:- Enter A Number :-  213 312

Table Of Given Number

Image
#include<stdio.h> #include<conio.h> void main() { int num,res,i; printf("Enter The Number :- "); scanf("%d",&num); for(i=1;i<=10;i++) { res=num*i; printf("%d",res); printf("\n"); } getch(); } Output:- Enter The Number :- 3 3 6 9 12 15 18 21 24 27 30

Checking The Entered Number is Even Or Odd

Image
#include<stdio.h> #include<conio.h> void main() { clrscr(); int a; printf("Enter a Numbers"); scanf("%d",&a); if(a%2==0) {  printf("Number Is even"); } else { printf("Number is odd"); } getch(); } Output:- Enter two Numbers2 Number Is even

Printing Hello World In C++

Image
  #include<iostream> using namespace std; void main() {   cout<<"Hello World"; getch(); }

Checking Entered Number .. Prime or Not

Image
#include <stdio.h>    #include<conio.h> int   main () {     int   n , i , m = 0 , flag = 0 ;  //n variable is used to store the value of entered number //varible i for loop //variable m for storing the result //variable flag for used for if statement   printf ( "Enter to check the number :" );     scanf ( "%d" ,& n );     m = n / 2 ;     for ( i = 2 ; i <= m ; i ++)     {     if ( n % i == 0 )     {     printf ( "Number is not prime" );     flag = 1 ;     break ;     }     }     if ( flag == 0 ) {     ...