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


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 Print Maximum Number Between Two Numbers Using Switch Case

Ex: write a C program to find maximum number between two numbers without using if-else using switch case. How to write a C program to find maximum number between two numbers without using if-else using switch case.
c program to print maximum out of two numbers without using if-else statement using Switch Case.


Input from user:

Enter value of a: 5

Enter value of b: 15

Expected Output:

Maximum is b=15.


In below code we will print max out of two numbers without using if-else statement's. Here we will use switch-case to find the max number of given two numbers.




  Step by Step Logic of the program:

1. First we will accept two numbers from user which say a & b.


2. After that we will store the output of a/b in the switch. Switch (a/b)

3. If output in zero then case 0 will work means max is b.


4. Otherwise in default- a is max.




  C Program To Print Maximum Number  Between Two Numbers  Using Switch Case:


#include<stdio.h>

int main()
{
  int a,b;


printf("enter value of a\n");

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

scanf("%d",&b);

  switch(a/b)

  {
   case 0:

   printf("max is b=%d",b);
   break;

  default:

   printf("max is a=%d",a);

}

  return 0;


}


Above Program show's the following output:



C Program To Print Maximum Number  Between Two Numbers  Using Switch Case




C Program To Reverse the String without using "strrev()" function

Ex: Write a C Program To Reverse the String without using "strrev()" function. How to write a C Program To Reverse the String without using "strrev()" function. C Program To Reverse the String without using "strrev()" function.




Program:

#include<stdio.h>
#include<string.h>
void main()
{
char a[100],b[100];
int i,j,len;

printf(" \n Please Enter Any String : ");
gets(a);

j=0;
/*Calculate length of string using strlen() function and store result in variable len*/
len=strlen(a);

//Run for loop from len-1 to 0.
for(i=len-1;i>=0;i--)
{
//store reversed string in array b.
b[j]=a[i];
    j++;
}
//Print reversed string
printf("\n String After Reversing = %s",b);

}



Above Program show's the following output:

C Program To Reverse the String without using "strrev()" function