Showing posts with label C Example. Show all posts
Showing posts with label C Example. Show all posts

C program to check password is correct or incorrect using switch-case

Ex: write a C program to check password is correct or incorrect using switch-case. How to write a C program to check given password is correct or incorrect using switch-case. C program to check password is correct or incorrect using switch-case.

Input from user:

Enter your password: 1010

Expected output:

Welcome



Quick links:




  Step by step logic of the given program:


1. Accept input (password) from user declare variable say ps.

2. Switch the value of ps using switch(ps).

3. There is 1 possible value of ps(1010).

4. Therefore write 1 case inside the switch.

5. If  case does not match then for default case print incorrect password. 





  C program to check password is correct or incorrect using switch-case :


#include<stdio.h>
int main()
{
int ps;

printf("Enter your password: ");
scanf("%d",&ps);

switch(ps)
{
case 1010:
printf("--WELCOME--");
break;

default:
printf("-INCORRECT PASSWORD-");
}
return 0;
}



Above program shows the following output:


C program to check password is correct or incorrect using switch-case

Bitwise Operators In C

Definition: A bitwise operator is an operator used to perform bitwise operations on bit patterns or binary numerals that involve the manipulation of individual bits.
Bitwise operators are used in communication stack.


Operations can be performed on a bit level using bitwise operators. We use bitwise operators to manipulate data  at its lowest bit level.bitwise operators works on each bit of the data.

There are six bitwise operators are in C programming:

Bitwise AND Operator In C, Bitwise OR Operator In C, Bitwise XOR Operator In C, Bitwise operators in C
Above table shows six bitwise operators in C programming:



1. Bitwise AND(&):

It takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.


[It is used to check perticular bit of data is ON(1) or OFF (0).]

Suppose we will take two intiger variables a & b, where a=9 and b=10.

8 bit binary representation of a & b is...

a= 0000 1001
b= 0000 1010

It will be evaluated as- 


Bitwise AND Operator In C, Bitwise Operators In C


So a & b is evaluated as 0000 1000 which is 8 in decimal.




2. Bitwise OR(|):

It takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 if any of the two bits is 1.


[ It is commonly used to set flag bit values.]

Suppose we will take two intiger variables a and b, where  a=4 and b=11.

8 bit binary representation of a | b is...

a= 0000 0100
b= 0000 1011

It  is evaluated as-

Bitwise OR Operator In C, Bitwise Operators In C

So a | b is evaluated as 0000 1111 which is 15 in decimal.




3. Bitwise XOR(^):

It takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.

Suppose we will take two intiger variables a and b, where a=6 and b=13.

8 bit binary representation of  a^b is....

a= 0000 0110
b= 0000 1101

It is evaluated as-


Bitwise XOR Operator In C, Bitwise operators in C

So a^b is evaluated as 0000 1011 which is 11 in decimal.



4. Bitwise Complement or NOT (~):


It takes one number and inverts all bits of it. Means it  sets bit value 1 only if corresponding bit of operand is 0.


Suppose we take intiger variable a, where a=4.

8 bit binary representation of a is-

a= 0000 0100

It is evaluated as-


Bitwise Complement or Not Operator In C, Bitwise operators in C

So a~ is  evaluated as 1111 1011 which is 124 in decimal.



5. Bitwise left shift(<<):


It takes two numbers and left shift the bits of the first operand, the second operand decides the number of places to shift.


Suppose we take an intiger variable a=15.

8 bit binary representation of a is-

a= 0000 1111

It is evaluated as-


Bitwise Left Shift Operator In C, Bitwise operators in C

Here,  ex: a<<1 means it shifts  bit one times to left which is 30  in decimal. another one example is a<<2 means it shifts bit two times to left which is 60 in decimal.
Left shifting of bit causes insertion of zero from right.




6. Bitwise right shift (>>):


It take two numbers and right shift the bits of the first operand, the second operand decides the number of places to shift.

Suppose we take intiger variable a=15.

8 bit binary representation of a is-

a= 0000 1111

It is evaluated as-


Bitwise Right Shift Operator In C, Bitwise Operators In C

Here, ex: a>>1 means it shifts bit one time to right which is 7 in decimal. another one example is a>>2 means it shifts bit two time to right which is 3 in decimals.

If you have any queries... feel free to discuss below in the comment box.   
  
Happy Coding....🙃


C Program to Print Alternate Prime Numbers from 1 to n

Ex: Write a C program to print alternate prime numbers from 1 to n. How to write a C program to print alternate prime numbers from 1 to n. C program to print alternate prime numbers from 1 to n.

Input from user:
Enter the number: 100

Expected output:
2 5 11 17 23 31 41 47 59 67 73 83 97


  • What is prime number?
Prime number means a number that is divisible only by 1 and itself.




  Step by step logic of the given program:


1. Accept input from user declare variable say no.

2. Declare e=2.

3. Run one outer for loop from 1 to no:
for(i=1;i<=no;i++)

4. Run one inner for loop from 1 to i and inside that check condition if i%j == 0 then increment c by 1:
for(j=1;j<=i;j++)
  {
  if(i%j==0)
   {
  c++;
   }
  } 


5. After that outside the inner for loop check if c==2 (it means if given number is prime) then, inside that if-statement check one more condition if e%2==0 means if e is even number then print value of (it prints   prime numbers only if e is completely divisible by 2, in short it prints alternate prime numbers):
if(c==2)
 {
 if(e%2==0)
 { 
 printf("%d ",i);
 }

6. After that, Outside the block of inner if statement increment value of e by 1.

7. Last make c=0.





  Program to Print Alternate Prime Numbers:


#include<stdio.h>
void main()
{
 int no,i,j,c=0,e;
 printf("Enter the number:\n");
 scanf("%d",&no);
 e=2;
 printf("Alternate Prime Number's Are:\n");
 for(i=1;i<=no;i++)
  {
  for(j=1;j<=i;j++)
  {
  if(i%j==0)
   {
  c++;
   }
  } 
 if(c==2)
 {
 if(e%2==0)
 { 
 printf("%d ",i);
 }
    e=e+1;
 }
 c=0;
 }
}


Above Program show's the following output:

C program to print alternate prime numbers from 1 to n, prime numbers




C Program To Print Pyramid Star Pattern

Ex: Write a C program to print  pyramid star pattern. How to write a C program to print  pyramid star pattern. C program to print  pyramid star pattern.


Input from user:
Enter the number:
7

Expected output:
       *
      **
     ***
    ****
   *****
  ******
 *******



  Step by step logic of the given program:


1. Accept input (number) from user which shows size of the pyramid, declare variable say no.

2. Run one outer for loop from 1 to no:
  for(i=1;i<=no;i++)

3. To print spaces run another for loop from i to no-1:
for(j=i;j<no;j++)
    {
    printf(" ");
    }

4. To print star run another one inner for loop from 1 to i:
for(k=1;k<=i;k++)
    {
    printf(" *");
    }

5. For new row, move to next line using \n.




Program:

#include<stdio.h>
void main()
{
  int i,j,k,no;

  printf("Enter the number:\n");
  scanf("%d",&no);

  for(i=1;i<=no;i++)
  {
  /*Print spaces*/
    for(j=i;j<no;j++)
    {
  printf(" ");
    }
    /*print star*/
    for(k=1;k<=i;k++)
    {
    printf(" *");
    }
    /*Move to the next line*/
  printf("\n");
  }
  
}



Above Program's show's the following output:

C Program to pyramid star pattern, star pattern in C


C program to print 1 3 6 10 15 21 28 36 triangular series

Ex: Write a C program to print 1 3 6 10 15 21 28 36 triangular series. How to write a C program to print 1 3 6 10 15 21 28 36 triangular series. C program to print 1 3 6 10 15 21 28 36 triangular series.


Input from user:
Enter the number: 8

Expected output:
1 3 6 10 15 21 28 36






Program:

#include<stdio.h>
void main()
{
int i,no,z=1;
printf("enter the no\n");
scanf("%d",&no);
 printf("1 ");
for(i=2;i<=no;i++)
{
z=z+i;
printf("%d ",z);

}
}

Above program shows the following Output:


C program to print 1 3 6 10 15 21 28 36 triangular series