Here The Type Of Factorial Variable Is Declared As: Unsigned Long Long. It Is Because, The Factorial Is Always Positive, So Unsigned Keyword Is Used And The Factorial Of A Number Can Be Pretty Large. For Example: The Factorial Of 10 Is 3628800 Thus, Long Long Keyword Is Used. To Solve This Program, We Have Used For Loop But, This Program Also Can Be Solved In Similar Way Using While Loop.
Program To Generate Multiplication Table
This Program Asks User To Enter An Integer And This Program Will Generate The Multiplication Table Upto 10.
Source Code To Generate Multiplication Table
/* C Program To Find Multiplication Table Up To 10. */
#include<stdio.h>
#include<conio.h>
Int Main()
{
Int N, I;
Printf("Enter An Integer To Find Multiplication Table: ");
Scanf("%D",&N);
For(I=1;I<=10;++I)
{
Printf("%D * %D = %D\N", N, I, N*I);
}
Return 0;
}
Output
Enter An Integer To Find Multiplication Table: 9
9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90
This Program Generates Multiplication Table Of An Integer Up 10. But, This Program Can Be Made More User Friendly By Giving Option To User To Enter Number Up To Which, He/She Want To Generate Multiplication Table. The Source Code Below Will Perform This Task.
#include<stdio.h>
#include<conio.h>
Int Main()
{
Int N, Range, I;
Printf("Enter An Integer To Find Multiplication Table: ");
Scanf("%D",&N);
Printf("Enter Range Of Multiplication Table: ");
Scanf("%D",&Range);
For(I=1;I<=Range;++I)
{
Printf("%D * %D = %D\N", N, I, N*I);
}
Return 0;
}
Output
Enter An Integer To Find Multiplication Table: 6
Enter Range Of Multiplication Table: 4
6 * 1 = 6
6 * 2 = 12
6 * 3 = 18
6 * 4 = 24
Program To Find Hcf Of Two Numbers
The Largest Integer Which Is Perfectly Divisible To Two Or More Numbers Is Known As H.C.F Or G.C.D Of Those Two Numbers.
There Are Many Ways To Find The H.C.F Of Two Numbers Is C Programming. All Codes Below Will Take Two Integers From User And Displays The H.C.F Of Those Two Integers.
1. Source Code To Find Hcf Or Gcd
/* C Program To Find Highest Common Factor. */
#include<stdio.h>
#include<conio.h>
Int Main()
{
Int Num1, Num2, I, Hcf;
Printf("Enter Two Integers: ");
Scanf("%D %D", &Num1, &Num2);
For(I=1; I<=Num1 || I<=Num2; ++I)
{
If(Num1%I==0 && Num2%I==0) /* Checking Whether I Is A Factor Of Both Number */
Hcf=I;
}
Printf("H.C.F Of %D And %D Is %D", Num1, Num2, Hcf);
Return 0;
}
In This Program, Two Integers Are Taken From User And Stored In Variable Num1 And Num2. Then I Is Initialized To 1 And For Loop Is Executed Until I Becomes Equal To Smallest Of Two Numbers. In Each Looping Iteration, It Is Checked Whether I Is Factor Of Both Numbers Or Not. If I Is Factor Of Both Numbers, It Is Stored To Hcf. When For Loop Is Completed, The H.C.F Of Those Two Numbers Will Be Stored In Variable Hcf.
2. Source Code To Find Hcf Or Gcd
#include<stdio.h>
#include<conio.h>
Int Main()
{
Int Num1, Num2, Min,I;
Printf("Enter Two Integers: ");
Scanf("%D %D", &Num1, &Num2);
Min=(Num1>Num2)?Num2:Num1; /* Minimum Value Is Stored In Variable Min */
For(I=Min;I>=1;--I)
{
If(Num1%I==0 && Num2%I==0)
{
Printf("Hcf Of %D And %D Is %D", Num1, Num2,I);
Break;
}
}
Return 0;
}
This Program Is Little Optimized Than The Program Above To Find H.C.F. In This Program, Smallest Of Two Integers Entered By User Is Stored In Variable Min. Then I Is Initialized To Min And For Loop Is Executed. In Each Looping Iteration, Whether I Is Factor Of These Two Numbers Is Checked. If I Is A Factor Of These Two Numbers Then, I Will Be The Highest Common Divisor And Loop Is Terminated Using Break Statement.
3. Source Code To Find Hcf Or Gcd
#include<stdio.h>
#include<conio.h>
Int Main()
{
Int Num1,Num2;
Printf("Enter Two Integers: ");
Scanf("%D %D",&Num1,&Num2);
Printf("Hcf Of %D And %D Is ",Num1 , Num2);
While(Num1!=Num2)
{
If(Num1>Num2)
Num1-=Num2;
Else
Num2-=Num1;
}
Printf("%D",Num1);
Return 0;
}
This Is The Best Way To Find Hcf Of Two Numbers. In This Method, Smaller Number Is Subtracted From Larger Number And That Number Is Stored In Place Of Larger Number. This Process Is Continued Until, Two Numbers Become Equal Which Will Be Hcf.
All Source Codes Above Displays The Same Output.
Output
Enter Two Integers: 14
35
Hcf Or 14 And 35 Is 7
Program To Count Number Of Digits Of An Integer
This Program Takes An Integer From User And Calculates The Number Of Digits In That Integer. For Example: If User Enters 2319, The Output Of Program Will Be 4 Because It Contains 4 Digits.
C Program To Find Number Of Digits In A Number
#include<stdio.h>
#include<conio.h>
Int Main()
{
Int N,Count=0;
Printf("Enter An Integer: ");
Scanf("%D", &N);
While(N!=0)
{
N/=10; /* N=N/10 */
++Count;
}
Printf("Number Of Digits: %D",Count);
}
Output
Enter An Integer: 34523
Number Of Digits: 5
Explanation
This Program Takes An Integer From User And Stores That Number In Variable N. Suppose, User Entered 34523. Then,While Loop Is Executed Because N!=0 Will Be True In First Iteration. The Codes Inside While Loop Will Be Executed. After First Iteration, Value Of N Will Be 3452 And Count Will Be 1. Similarly, In Second Iteration N Will Be Equal To 345 And Count Will Be Equal To 2. This Process Goes On And After Fourth Iteration, N Will Be Equal To 3 And Count Will Be Equal To 4. Then, In Next Iteration N Will Be Equal To 0 And Count Will Be Equal To 5 And Program Will Be Terminated As N!=0 Becomes False.
Program To Reverse A Number
This Program Takes An Integer Number From User And Reverses That Number.
C Program To Reverse An Integer
#include<stdio.h>
#include<conio.h>
Int Main()
{
Int N, Reverse=0, Rem;
Printf("Enter An Integer: ");
Scanf("%D", &N);
While(N!=0)
{
Rem=N%10;
Reverse=Reverse*10+Rem;
N/=10;
}
Printf("Reversed Number = %D",Reverse);
Return 0;
}
Output
Enter An Integer: 2345
Reversed Number = 5432
Program To Check Whether A Number Is Palindrome Or Not
This Program Takes An Integer From User And That Integer Is Reversed. If The Reversed Integer Is Equal To The Integer Entered By User Then, That Number Is A Palindrome If Not That Number Is Not A Palindrome.
C Program To Check Palindrome Number
/* C Program To Check Whether A Number Is Palindrome Or Not */
#include<stdio.h>
#include<conio.h>
Int Main()
{
Int N, Reverse=0, Rem,Temp;
Printf("Enter An Integer: ");
Scanf("%D", &N);
Temp=N;
While(Temp!=0)
{
Rem=Temp%10;
Reverse=Reverse*10+Rem;
Temp/=10;
}
/* Checking If Number Entered By User And It's Reverse Number Is Equal. */
If(Reverse==N)
Printf("%D Is A Palindrome.",N);
Else
Printf("%D Is Not A Palindrome.",N);
Return 0;
}
Output
Enter An Integer: 12321
12321 Is A Palindrome.
Program To Check Whether A Number Is Prime Or Not
A Positive Integer Which Is Only Divisible By 1 And Iself Is Known As Prime Number. For Example: 13 Is A Prime Number Because It Is Only Divisible By 1 And 13 But, 15 Is Not Prime Number Because It Is Divisible By 1, 3, 5 And 15.
C Program To Check Prime Number
/* C Program To Check Whether A Number Is Prime Or Not. */
#include<stdio.h>
#include<conio.h>
Int Main()
{
Int N, I, Flag=0;
Printf("Enter A Positive Integer: ");
Scanf("%D",&N);
For(I=2;I<=;N/2;++I)
{
If(N%I==0)
{
Flag=1;
Break;
}
}
If (Flag==0)
Printf("%D Is A Prime Number.",N);
Else
Printf("%D Is Not A Prime Number.",N);
Return 0;
}
Output
Enter A Positive Integer: 29
29 Is A Prime Number.
Explanation
This Program Takes A Positive Integer From User And Stores It In Variable N. Then, For Loop Is Executed Which Checks Whether The Number Entered By User Is Perfectly Divisible By I Or Not Starting With Initial Value Of I Equals To 2 And Increasing The Value Of I In Each Iteration. If The Number Entered By User Is Perfectly Divisible By I Then, Flag Is Set To 1 And That Number Will Not Be A Prime Number But, If The Number Is Not Perfectly Divisible By I Until Test Condition I<=N/2 Is True Means, It Is Only Divisible By 1 And That Number Itself And That Number Is A Prime Number
Program To Display Prime Numbers Between Two Intervals
This Program Asks User To Enter Two Integers And This Program Will Display All Prime Numbers Between These Intervals. If You Don't Know How To Check Whether A Number Is Prime Or Not Then, This Program May Seem Little Bit Complex. You Can Visit This Page To Learn About Prime Numbers And How To Check Whether A Number Is Prime Or Not In C Programming.
Source Code To Display Prime Numbers Between Two Intervals
/* C Program To Display All Prime Numbers Between Two Interval Entered By User. */
#include<stdio.h>
#include<conio.h>
Int Main()
{
Int N1, N2, I, J, Flag;
Printf("Enter Two Numbers(Intevals): ");
Scanf("%D %D", &N1, &N2);
Printf("Prime Numbers Between %D And %D Are: ", N1, N2);
For(I=N1+1; I=1;--I)
{
For(J=1;J<=I;++J)
{
Printf("* ");
}
Printf("\N");
}
Return 0;
}
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Write A C Program To Print Inverted Half Pyramid As Using Numbers As Shown Below.
#include<stdio.h>
#include<conio.h>
Int Main()
{
Int I,J,Rows;
Printf("Enter The Number Of Rows: ");
Scanf("%D",&Rows);
For(I=Rows;I>=1;--I)
{
For(J=1;J<=I;++J)
{
Printf("%D ",J);
}
Printf("\N");
}
Return 0;
}
C Program To Display The Pyramid Of * And Digits
Write A C Program To Print Pyramid Using *.
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
#include<stdio.h>
#include<conio.h>
Int Main()
{
Int I,Space,Rows,K=0;
Printf("Enter The Number Of Rows: ");
Scanf("%D",&Rows);
For(I=1;I<=Rows;++I)
{
For(Space=1;Space<=Rows-I;++Space)
{
Printf(" ");
}
While(K!=2*I-1)
{
Printf("* ");
++K;
}
K=0;
Printf("\N");
}
Return 0;
}
Write A C Program To Print The Pyramid Of Digits In Pattern As Below.
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5
#include<stdio.h>
#include<conio.h>
Int Main()
{
Int I,Space,Rows,K=0,Count=0,Count1=0;
Printf("Enter The Number Of Rows: ");
Scanf("%D",&Rows);
For(I=1;I<=Rows;++I)
{
For(Space=1;Space<=Rows-I;++Space)
{
Printf(" ");
++Count;
}
While(K!=2*I-1)
{
If (Count<=Rows-1)
{
Printf("%D ",(I+K));
++Count;
}
Else
{
++Count1;
Printf("%D ", (I+K-2*Count1));
}
++K;
}
Count1=Count=K=0;
Printf("\N");
}
Return 0;
}
Write A C Program To Display Reverse Pyramid.
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
#include<stdio.h>
#include<conio.h>
Int Main()
{
Int Rows,I,J,Space;
Printf("Enter Number Of Rows: ");
Scanf("%D",&Rows);
For(I=Rows;I>=1;--I)
{
For(Space=0;Space
No comments:
Post a Comment