Showing posts with label (K) Tricky Program's. Show all posts
Showing posts with label (K) Tricky Program's. Show all posts

C program to print odd numbers from 1 to n without using if statement

Ex: Write a C program to print odd numbers between 1 to n. How to write a C program to print odd numbers between 1 to n. C program to print odd number's between 1 to n.


Input from user: 

Enter the limit: 20

Expected output:

Odd numbers between 1 to 20:

1
3
5
7
9
11
13
15
17
19






   Step by step logic to print Odd numbers between 1 to n(without using if-statement):



1. Accept limit from user store it in variable no.

2. Run for loop which iterate upto n number's. for(i=1;i<=no;i+=2)

3. We used i=1 because odd numbers starts from 1.

4.  We increment loop by two to print odd numbers.

5. Print value of i inside the for loop.




   Program to print odd number's without using if-statement:


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

 int i,no;

 printf("Enter the limit: ");
 scanf("%d",&no);

  printf("Odd numbers between 1 to %d\n",no);

 for(i=1;i<=no;i+=2)
 {
   printf("%d\n",i);
 }

 return 0;
}





   Program to print odd number's using if-statement:


In below Program we print odd numbers from 1 to n using if-statement.


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

 int i,no;

 printf("Enter the limit: ");
 scanf("%d",&no);

  printf("Odd numbers between 1 to %d\n",no);

 for(i=1;i<=no;i++)
 {
   if(i%2==1)
   printf("%d\n",i);

 }

 return 0;

}



Above program's shows the following output:




C program to print Even number's from 1 to n without using if statement

Ex: Write a C program to print even numbers between 1 to n. How to write a C program to print even numbers between 1 to n. C program to print even number's between 1 to n.


Input from user: 

Enter the limit: 20

Expected output:

Even numbers between 1 to 20:

2
4
6
8
10
12
14
16
18
20





  Step by step logic to print even numbers between 1 to n(without using if-statement):



1. Accept limit from user store it in variable no.

2. Run for loop which iterate upto n number's. for(i=2;i<=no;i+=2)

3. We used i=2 because even number's starts from 2.

4.  We increment loop by two to print even numbers.

5. Print value of i inside the for loop.




  Program to print even numbers without using if-statement:


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

int i,no;

printf("Enter the limit: ");
scanf("%d",&no);

printf("Even numbers between 1 to %d\n",no);

for(i=2;i<=no;i+=2)
{
printf("%d\n",i);

}

return 0;
}




  Program to print even numbers using if-statement:


In below program we used if-statement to print even numbers from 1 to n.


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

 int i,no;

 printf("Enter the limit: ");
 scanf("%d",&no);

  printf("Even numbers between 1 to %d\n",no);

 for(i=1;i<=no;i++)
 {
   if(i%2==0)
   printf("%d\n",i);

 }

 return 0;
}



Above two programs will show the following output:


C program to print even numbers from 1 to n,  Even numbers from 1 to n

C Program To Print "Welcome" without using semicolon's using "Switch-case"

  Program Using switch case:



#include<stdio.h>
void main()
{
switch(printf("welcome"))
{}

}



Above Program show's the following output:


C Program To Print "Welcome" without using semicolon's using "Switch-case"


C Program To Print "Welcome" without using (;) "semicolon's" using if-statement

  Print welcome without using semicolons:



#include<stdio.h>
void main()
{
       if(printf("welcome"))
        {}




Above Program show's the following output:



C program to print welcome without using semicolons using if statement



C Program To Print Number Is Even or Odd Without Using "if-else" Statement Using "Switch-Case"

Program to Print number is even or odd without using if-else statement using switch case:


#include<stdio.h>
int main()
{
int no;
printf("enter the no\n");
scanf("%d",&no);
/*If number is completely divisible by 2 means (no%2=0) is true then inside the case 0: print number is even, otherwise inside the case 1: print number is odd.*/
switch(no%2)
{
case 0: printf("no is even");
break;
case 1: printf("no is odd");
}
    return 0;
}



Above Program show's the following output:


C Program To Print Number Is Even or Odd Without Using  "if-else" Statement Using "Switch-Case"


C program to devide the intiger number without using division "/" sign

Ex: Write a C program to devide the intiger number without using division "/" sign. How to write a C program to devide the intiger number without using division "/" sign. C program to devide the intiger number without using division "/" sign.

Input from user:
Enter number1: 25
Enter number2: 5

Expected output:
Division is 5




Program:

#include<stdio.h>
void main()
{
int no1,no2,c=1;

printf("enter the no1\n");
scanf("%d",&no1);
printf("enter the 2nd no\n");
scanf("%d",&no2);

while(no1>=no2)
{
no1=no1-no2;
c++;
if(no1==no2)
{
printf("division is %d",c);
}
}

}


Above program shows the following output:

C program to devide the intiger number without using division "/" sign