C Program To Print Square star Pattern

Ex: Write a C Program To Print Square star Pattern. How to write a C Program To Print Square star Pattern. C Program To Print Square star Pattern.


Input from user:
Enter number: 5

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




   Step by step logic to print square star pattern:

1. First accept input(number) from user  which shows how many rows and column's do you want to print.

2. After that use one external for loop which will interate from 0 to no-1 and it shows rows in your square pattern:
for(i=0;i<no;i++)

3. Then use one another for loop inside the above for loop which prints the number of columns:
for(j=0;j<no;j++)

4. Inside the inner loop means inside the second for loop print * with one space like this:
printf("* ");

5.  After that use \n for new line(new row).





   Program to print sqaure star pattern:



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

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

for(i=0;i<no;i++)
{
for(j=0;j<no;j++)
{
printf("* ");
}
printf("\n");
}
}



Above Program show's the following output:

C Program To Print Square star Pattern




C Program To Print inverted Pyramid star pattern

Ex: Write a C Program To Print inverted Pyramid star pattern. How to write a C Program To Print inverted Pyramid star pattern. C Program To Print inverted 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 number of rows, declare variable say no.

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

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

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

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





   Program to print inverted pyramid star pattern:

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

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

for(i=1;i<=no;i++)
{
for(j=1;j<i;j++)
printf(" ");

for(k=i;k<=no;k++)
printf(" *");

printf("\n");
}

}


Above program shows the following output:


C Program to print inverted pyramid star pattern



C Program To Print Given Number Is Even or Odd

EX: What is logic to print given number is even or odd. C program to print given number is even or odd. how to print given number is even or odd.


Logic to print given number is even or odd:


1. In this Program we will take number from user using no variable. 


2. Next we will check it is even or odd using the (no%2==0).

3. Means if no is complete divisible by 2 then it is even otherwise number is odd.

4. Display this output using input/output function.



C  Program To Print Given Number Is Even or Odd: 


#include<stdio.h>

void main()
{
int no; 

printf("Enter the number\n");

scanf("%d",&no);

if(no%2==0)
printf("Given number is Even\n");

else
printf("Given number is Odd\n");

}


Above Program's show's the following output:



C  Program To Print Given Number Is Even or Odd



C Program To Swap Two Numbers Without Using Third Variable

Ex: write a C program to swap two numbers without using third variable. How to swap two numbers without using third variable. C PROGRAM to swap two numbers without using third variable.


In below code we will swap values of two variable without using third variable.
Here, we will use the addition and subtraction for swap two variable's. 



C  Program To  Swap Two Numbers Without Using Third Variable:


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

 int a,b;

 printf("Enter the value of a and b\n");
 scanf("%d%d",&a,&b);

 printf("values of a & b before swap\na=%d b=%d",a,b);

 a=a+b;
 b=a-b;
 a=a-b;

 printf("\nvalues of a & b after swaping\na=%d b=%d",a,b);

}



Above Program's show's the following output:



C  Program To  Swap Two Numbers Without Using Third Variable




C Program To Print Factorial Of 1 to n Number's

Ex: What is the logic to print factorial of 1 to n Number's. How to print factorial of 1 to n numbers. C program to print factorial of 1 to n number.

Input from user:
Enter the number: 5

Expected output:
Factorial of 1 is: 1
Factorial of 1 is: 2
Factorial of 1 is: 6
Factorial of 1 is: 24
Factorial of 1 is: 120




  Logic to print factorial of 1 to n number's:


1. Accept input number from user.

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

3. Make f=i.

4. Run inner for loop from i to 2 and inside the inner for loop add given factorial logic:
for(j=i;j>1;j--)
 {
      f=f*(j-1);
 }

5. After that outside the inner loop print value of f means factorial numbers.





  C  Program To Print Factorial Of 1 to n Number's:


#include<stdio.h>
void main()
{
 int i,j,no,f;
 
    printf("Enter the number:\n");
    scanf("%d",&no);
   
    for(i=1;i<=no;i++)
    {
     f=i;
     for(j=i;j>1;j--)
     {
        f=f*(j-1);
     }
     printf("Factorial of %d is = %d\n",i,f);
    }
}


Above Program show's the following output:

C  Program To Print Factorial Of 1 to n Number's





Mostly Asked 25 C Programming Apti MCQ Questions

In this article, I will share some frequently asked C programming aptitude questions.
 

1. Which header file  do you need  to include to use  typecasting?

  • <stdin.h>
  • <ctype.h>
  • <math.h>
  • none of these


2. # include is called____________

  • Preprocessor Directive
  • Inclusion Directive
  • File inclusion directive
  • none of these


3A local  variable  is store in_______

  • code segment
  • stack  segment
  • heap segment
  • none of these


4. An exception is ________

  • Runtime Error
  • Compile Error
  • Logical Error
  • None of the above




5Which of the  following is valid expression for assigning the value to the variable?

  • a==b
  • a:=b
  • a=b
  • none of the above


6Which of the following is not a reserve keyword for c?

  • auto
  • case
  • main
  • Register


7Which of the following is not correct variable type

  • float
  • real
  • double
  • int


8. Every function  in c is followed by

  • parenthesis
  • square brackets
  • curly braces
  • All of the above


9. In command  line argument the parameter argc passed to main is of______ datatype

  • float
  • int
  • string
  • double


10. What will be the following code's output if choice = 'R'?

switch(choice)
{
   case 'R' : printf("RED");
   case 'W' : printf("WHITE");
   case 'B' : printf("BLUE");
   default  : printf("ERROR");break;
}


  • RED
  • RED WHITE BLUE ERROR
  • RED ERROR  
  • ERROR


11What is the correct value to return to the operating system on the successful  completion of key program?
  • -1
  • 1
  • 0
  • It does not return any value




12. An array of similar data types which themselves are a collection of dissimilar data types are___

  • Linked lists
  • Trees
  • Array of structure
  • All of the above


13. What will happen after compiling and running following code?

main()
{
     printf("%p", main);
}

  • Error
  • Will make an infinite loop
  • Some address will be printed.
  • None of these.


14. What will be the output if you will compile and execute the following C code?

#include<stdio.h>
int main()
{
int a=5;
float b;
printf(“%d”,sizeof(++a+b));
printf(“%d”,a);
return 0;
}

  • 26
  • 46
  • 25
  • 45


15How many times do while loop guaranteed to loop?

  • 0
  • infinity
  • 1
  • variable


16int a[5] = {1,2,3}
What is the value of a[4]?

  • 3
  • 1
  • 2
  • 0


17. Which of the following operator can be used to access value at address store in a pointer variable...

  • *
  • #
  • &&
  • @


18Which  conversion  is  not possible...

  • int to float
  • float to int
  • char to float
  • All of the above is possible


19What will be the output of the program ?

#include<stdio.h>
void main()
{
    printf(5+"Good Morning");
}


  • Good Morning
  • M  
  • Good
  • Morning


20. Program are converted into machine language with the help of ____

  • an Editor
  • a compiler
  • an operating system
  • none of these


21The  correct  order of  evaluation for the expression “z=x+y*z/4%2-1”


  • */%=+-
  • /*%-+=
  • -+=*%/
  • */%+-=


22. Which of the following function disconnected the stream  from the file pointer?

  • fclose()
  • remove()
  • fremove()
  • file pointer to be set to null


23To flush the memory used by memory n allocation functions, we  use______ function.

  • dealloc()
  • erase()
  • clear()
  • free()


24Which of the following adds one string to end of the  another

  • append()
  • stringadd()
  • strcat()
  • stradd()


25. What will be the output of the given program?

#include<stdio.h>
void main()
{
    int a=11,b=5;
    if(a=5) b++;
    printf("%d %d", ++a, b++);
}

  • 12 7
  • 5 6
  • 6 6
  • 6 7




Answer's:

1-d                                                            
2-a                                                            
3-b                                                            
4-a                                                            
5-c                                                            
6-d                                                            
7-b                                                            
8-a                                                            
9-b                                                            
10-b                                                          
11-c                                                          
12-c                                                          
13-c                                                          
14-d                                                          
15-c                                                          
16-d                                                          
17-a                                                          
18-d                                                          
19-d                                                          
20-b                                                          
21-d                                                          
22-a                                                          
23-d                                                          
24-c                                                          
25-c                                                          




C Program To Find Area Of Circle

Ex: write a C program to find Area of the circle. How to write a C program to find area of circle. C program to find area of circle.


Input from user:

Enter the radius of the circle: 5

Expected Output:

Area of circle is: 78.500000



To find area of circle we need to use pi(π). Because pi(π) represents  the ratio of the circumference of a circle to its diameter.





Step by step logic of the program:

1. First  we need to declare two variable's. One for accepting radius of the circle from user and second one is for store the area of circle.

2. So declare variable's  r for radius of the circle and a for  area of the circle.

3. To find area use area of circle's simple logic which π*r^2(pi r square).

4. Here pi's value is 3.14. so we will write a=3.14*r*r.(here r=5)

5. Last we will print value of a(area of circle) on the output screen.





Program:


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

 int r;
 float a;

 printf("enter the radius of the circle:\n");
scanf("%d",&r);

a=3.14*r*r;

 printf("Area of circle is %f",a);

return 0;
}



Above program shows the following output:

C  Program To Find Area Of Circle






C Program To Find Area Of Triangle

Ex: write a C program to find area of triangle. How to write a C program which find area of triangle. C Program To Print area of triangle.


Input from user:
Enter the breadth of triangle: 5
Enter the height of triangle: 10

Expected output:
Area of triangle: 25.0000





Step by step descriptive logic of the given program:


1. First accept breadth and height of the triangle from user.


2. Use variable b for breadth and h for height of the triangle and a for store area of triangle.

3. In the logic  multiply breadth into height and devide it by 2 a=(b*h)/2

4. Print a(area of triangle) on the output screen.





 Program:


#include<stdio.h>

int main()
{

 int b,h;

 float a;

  printf("Enter the breadth of triangle\n");

scanf("%d",&b);

 printf("Enter the height of the triangle\n");    

 scanf("%d",&h);

 a=(b*h)/2;


 printf("Area of triangle is %f",a);

return 0;


}



Above program shows the following output:



C Program To Find Area Of Triangle



C Program To Find Circumference Of Circle

Ex: Write a C program to find circumference of circle. How to write a C program to find circumference of circle. C program to find circumference of circle.


Input from user:
Enter the number: 9

Expected output:
Circumference of circle is 56.520000




Step by step logic of the given program:

1. Accept input from user declare variable say no.

2. To find circumference of circle use following circumference formula:
2πr.

3. But in logic of the program we need to add pi's value means 3.14.

4. We will find circumference of circle as follows:
c=2*3.14*r;

5. Print value of c (circumference of circleon the output screen.





Program:


#include<stdio.h>
void main()
{
int r;
float c;

printf("enter the radius of circle\n");
scanf("%d",&r);

c=2*3.14*r;

printf("circumference of circle is %f",c);

}



Above two Program's show's the following output:

C Program To Find Circumference Of  Circle