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 following numbers series 7.5 9.5 15.5 27.5 47.5

Ex: Write a C program to print following numbers series 7.5 9.5 15.5 27.5 47.5...How to write a C program to print following numbers series 7.5 9.5 15.5 27.5 47.5.....C program to print following numbers series 7.5 9.5 15.5 27.5 47.5.


Input from user:
Enter a number: 5

Expected output:
7.5 9.5 15.5 27.5 47.5





Program:

#include<stdio.h>
void main()
{
int i,no;
float z=7.5;

printf("Enter a number:\n");
scanf("%d",&no);
//First print current value of z
printf("%f ",z);
for(i=1;i<no;i++)
{
z=(z)+i*(i+1);
printf("%f ",z);
}

}

Above Program show's the following output:
C program to print following numbers series 7.5 9.5 15.5 27.5 47.5.......







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


C Program to print your name 10 times without using while loop, for loop or goto statement

Ex: Write a C Program to print your name 10 times without using loops or goto statement using recursion function. How to write a C Program to print your name 10 times without using loops or goto statement using recursion function. C Program to print your name 10 times without using loops or goto statement using recursion function.


In below program we have used recursion function to print name 10 times.

What is recursion function?
Recursion function means function calling it self ...In simple words recursion() means call a function inside the same function then it is called as recursive call of the function.




  C Program to print your name 10 times without using while loop, for loop or goto statement:


#include<stdio.h>
void rec(int i)
{
if(i<=10)
{
printf("%02d omkar\n",i);
rec(i+1);
}
}
int main()
{
int i=1;
rec(i);
}


Above Program show's the following output:


C Program to print your name 10 times without using while loop, for loop or goto statement



C Program to Delete the Element from Array

Ex: Write a C program to delete the element from array. How to write a C program to delete the element from array. C program to delete the element from array.


Input from user:
Enter how many elements you want: 7
Enter the elements: 
1
2
3
4
2
5
6
Entered elements are: 
1 2 3 4 2 5 6 
Enter which element you want to delete: 2
Enter the position: 5

Expected ouput:
After deleting, array element's are:
1 2 3 4 5 6 




Program:

#include<stdio.h>
void main()
{
 int a[100],i,no,key,temp,pos;

 //Accept limit from user..
 printf("Enter how many elements you want:\n");
 scanf("%d",&key);

 //Accepting elements 
 printf("\nEnter the elements:\n");
 for(i=0;i<key;i++)
 scanf("%d",&a[i]);

 /*Print entered elements*/
 printf("Entered elements are:\n");
 for(i=0;i<key;i++)
 printf("%d ",a[i]);

 /*Accept element which want to delet*/
 printf("\nEnter which element you want to delet:\n");
 scanf("%d",&no);

 /*Accept position of element*/
 printf("Enter the position:\n");
 scanf("%d",&pos);

 /*logic to delet element*/
 for(i=pos-1;i<=key;i++)
 {
  a[i]=a[i+1];
  
 }
 /*print elements after deleting*/
 printf("After deleting, array elements are:\n");
 for(i=0;i<key-1;i++)
 printf("%d ",a[i]);

}


Above Program shows the following output:

C program to delete the element from array


    



C Program to Print Swastika Star Pattern

Ex: Write a C Program to print swastika star pattern. How to write a C Program to print swastika star pattern. C Program to print swastika star pattern.           
   
Input from user:
Enter the number:
5

Expected output:

C Program to Print Swastika Star Pattern, star patterns in C programming



           

  Step by Step logic of the given program:


1. Accept input number from user declare variable say no.

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

3. Run inner for loop from 1 to (no*2-1):
for(j=1;j<=no*2-1;j++)

4. Use if statement for add conditions to print stars:
if(i==no||j==no||i==1&&j>no||i<no&&j==1||i>no&&j==no*2-1||i==no*2-1&&j<no)
   {
  printf(" *");
   }

5. Otherwise print spaces. For next row print new line(\n).





  C Program to Print Swastika Star Pattern:


#include<stdio.h>
void main()
{
 /* Declare variables*/
 int i,j,no;
 
 printf("Enter the number:\n");
 scanf("%d",&no);
 /* Run loops*/
 for(i=1;i<=no*2-1;i++)
 {
  for(j=1;j<=no*2-1;j++)
  {
/* Conditions for print stars*/
 if(i==no||j==no||i==1&&j>no||i<no&&j==1||i>no&&j==no*2-1||i==no*2-1&&j<no)
   {
  printf(" *");
   }
   else
   {
  printf("  ");
   }
  }
  printf("\n");
 } 
}  

Above Program shows the following output:


C Program to Print Swastika Star Pattern




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 hollow diamond star pattern

Ex: Write a C Program to print hollow diamond star pattern. How to write a C Program to print hollow diamond star pattern. C Program to print hollow diamond star pattern.



We will print this pattern in two parts, first we print upper half of the pattern then we print below, half part of pattern.



  Step by step logic of the given program:


1. Accept input (number) from user declare variable say no.

2. print upper half of the pattern:

**********
****    ****
***        ***
**            **
*                *

a). Run one outer for loop from 1 to no.

b). After that run inner loop from i to no to print inverted right triangle pattern.

c). Run another one inner loop from 1 to i*2-2  to print spaces.

d). Last run one more inner loop from i to no to print another inverted right triangle and to complete right side of upper half pattern.
Inside the outer loop, for new row, print new line (\n).


3. Print lower half of the pattern:

*                *
**            **
***        ***
****    ****
**********

a). Run one outer for loop from 1 to no.

b). Inside the outer loop run inner loop from 1 to i to print  right triangle pattern.

c). Run another one inner loop from 1 to (no*2-i*2)  to print spaces.

d). Last run one more inner loop from 1 to i to print another right triangle and to complete right side of lower half pattern. 
Inside the outer loop, for new row, move to next line using \n.





Program:

#include<stdio.h>
void main()
{
int no;
int i,j;
printf("Enter the number:\n");
scanf("%d",&no);
/*loop to print upper half pattern*/
for(i=1;i<=no;i++)
{
for(j=i;j<=no;j++)
{                  printf("*");
}
for(j=1;j<=(i*2-2);j++)
{                                 
         printf(" ");
}
for(j=i;j<=no;j++)
{
         printf("*");
}
printf("\n");
}

/*loop to print lower half of the pattern*/
for(i=1;i<=no;i++)
{
for(j=1;j<=i;j++)
{                          
             printf("*");
}
for(j=1;j<=(no*2-i*2);j++)
{                                printf(" ");
}
for(j=1;j<=i;j++)
{
       printf("*");
}
printf("\n");
}
}


Above Program show's the following output:

C Program to print hollow diamond star pattern

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

C program to print hollow right triangle number pattern

Ex: Write a C program to print hollow right triangle number pattern. How to write a C program to print hollow right triangle number pattern. C program to print hollow right triangle number pattern.

Input from user:
Enter the number:
5

Expected output:
1
12
1  3
1    4
12345





Step by step logic of the given program:

1. Accept input number from user declare variable say no.

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

3. Inside outer for loop run one inner for loop from 1 to i to print it like right triangle:
for(j=1;j<=i;j++)

4. Inside inner for loop, check if condition, to print only first and last number(To print it like hollow triangle):
if(j==1||j==i)
   {
  printf("%d",j); 
   }

5. For last row of the pattern check condition to print all number's:
else if(i==no)
   {
   printf("%d",j);
   }

6. Otherwise print spaces:
else
   {
   printf(" ");
   }

7. For next row print new line(\n): 
printf("\n");





Program:

#include<stdio.h>
void main()
{
 int i,j,no;
 
 printf("Enter the number:\n");
 scanf("%d",&no);
 
 for(i=1;i<=no;i++)
 {
  for(j=1;j<=i;j++)
  {
    if(j==1||j==i)
   {
  printf("%d",j); 
   }
  else if(i==no)
   {
   printf("%d",j);
   }
   else
   {
   printf(" ");
   }
  }
  printf("\n");

 }
}


Above Program's show's the following output:
     
C program to print hollow right triangle number pattern





Another program:

#include<stdio.h>
void main()
{
 int i,j,no;
 
 printf("Enter the number: \n");
 scanf("\n%d",&no);
 
 for(i=1;i<=no;i++)
 {
  for(j=1;j<=i;j++)
  {
    if(i<no&&j==i||i<no&&j==1)
   {
  printf("%d",i);  
   }
  else if(i==no)
   {
   printf("%d",j);
   }
   else
   {
   printf(" ");
   }
  }
  printf("\n");
 }
}

Above program shows the following output:

C program to print hollow right triangle number pattern