Initial commit

This commit is contained in:
Michael Reber
2019-11-15 12:59:38 +01:00
parent 40a414d210
commit b880c3ccde
6814 changed files with 379441 additions and 0 deletions
@@ -0,0 +1,38 @@
/* Ackerman Problem - Refer program for Question
There is a recursive function called the ackermans function which is popular with the lecturers of computer science and can be defined like this
If m and n are integers greater than or equal to zero then
ack(m,n)=n+1, if m=0
ack(m,n)=ack(m-1,1),if(n=0) and m>0
ack(m,n)=ack(m-1,ack(m,n-1)),otherwise
write a recursive function to implement the above mentioned algorithm. */
#include <stdio.h>
#include <conio.h>
int ack(int m, int n)
{
if(m==0)
return (n+1) ;
else if(n==0 && m>0)
return ( ack(m-1,1) ) ;
else
return ( ack(m-1, ack(m,n-1) ) );
}
void main()
{
int m, n ;
clrscr() ;
printf("Enter two numbers: ") ;
scanf("%d %d", &m, &n) ;
printf("Solution is %d", ack(m,n) ) ;
getch() ;
}
/*
Output:
Enter two numbers: 2 3
Solution is 9
*/
@@ -0,0 +1,23 @@
#include<stdio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j;
printf("Enter the First matrix: ");
for(i=0; i<3; i++)
for(j=0; j<3; j++)
scanf("%d",&a[i][j]);
printf("\nEnter the Second matrix : ");
for(i=0; i<3; i++)
for(j=0; j<3; j++)
scanf("%d",&b[i][j]);
for(i=0; i<3; i++)
for(j=0; j<3; j++)
c[i][j]=a[i][j]+b[i][j];
printf("\nThe Addition of two matrix is\n");
for(i=0; i<3; i++)
{
printf("\n");
for(j=0; j<3; j++)
printf("%d\t",c[i][j]);
}
}
@@ -0,0 +1,43 @@
/* Armstrong number 1 to 1000 - Write a function to check whether a given number is an armstrong number. Use this function to generate all armstrong numbers from 1 to 1000 */
#include <stdio.h>
#include <conio.h>
int check_armstrong(int n) ;
void main()
{
int n ;
clrscr() ;
printf("Armstrong numbers from 1 to 1000 are as shown: \n") ;
for(n=1 ; n<=1000 ; n++)
if(check_armstrong(n) == 1)
printf("%d ", n) ;
getch() ;
}
int check_armstrong(int n)
{
int sum=0, r, temp ;
temp=n ;
while(n!=0)
{
r=n%10 ; /* Extract the last digit */
sum=sum+r*r*r ; /* Finding sum */
n=n/10 ; /* Reduce number by 1 digit */
}
if(sum==temp)
return 1 ;
else
return 0 ;
}
/*
Output :
Armstrong numbers from 1 to 1000 are as shown:
1 153 370 371 407
*/
@@ -0,0 +1,44 @@
#include <stdio.h>
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
printf("Enter the number of rows and columns of first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter the number of rows and columns of second matrix\n");
scanf("%d%d", &p, &q);
if (n != p)
printf("Matrices with entered orders can't be multiplied with each other.\n");
else
{
printf("Enter the elements of second matrix\n");
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
scanf("%d", &second[c][d]);
for (c = 0; c < m; c++)
{
for (d = 0; d < q; d++)
{
for (k = 0; k < p; k++)
{
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
printf("Product of entered matrices:-\n");
for (c = 0; c < m; c++)
{
for (d = 0; d < q; d++)
printf("%d\t", multiply[c][d]);
printf("\n");
}
}
return 0;
}
@@ -0,0 +1,29 @@
/* Sum of digits use recursive function to calculate sum of digits of an integer*/
#include <stdio.h>
#include <conio.h>
int sum(int n)
{
if(n==0)
return 0 ;
else
return( n%10 + sum(n/10) ) ;
}
void main()
{
int n ;
clrscr() ;
printf("Enter the number: ") ;
scanf("%d", &n) ;
printf("Sum of digits of %d is %d", n, sum(n) ) ;
getch() ;
}
/*
Output:
Enter the number: 246
Sum of digits of 246 is 12
*/
@@ -0,0 +1,16 @@
#include<stdio.h>
void check(char);
void main()
{
char ch;
printf("enter any character");
scanf("%c",&ch);
check(ch);
}
void check(char c)
{
if(ch>=65&&ch<=90)
printf("char is upper case");
else if(ch>=97&&ch<=122)
printf("character is lower case");
}
@@ -0,0 +1,22 @@
#include<stdio.h>
void main()
{
void max();
max();
}
void max()
{
int a[5],max,n,i;
printf("How many nos you want to enter: ");
scanf("%d",&n);
printf("Enter element for the array: ");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
max=a[0];
for(i=1; i<5; i++)
{
if(max<a[i])
max=a[i];
}
printf("maximum no= %d",max);
}
@@ -0,0 +1,137 @@
/* Calculator - Write a complete C program to simulate the 4 - function calculator. The functions are +,-,*,/ and Q to quit. The program must be menu driven*/
#include <stdio.h>
#include <conio.h>
void addition(float a, float b);
void subtraction(float a, float b);
void multiplication(float a, float b);
void division(float a, float b);
void main()
{
char choice ;
float a,b ;
clrscr() ;
do
{
printf("\n Menu: \n") ;
printf("+ : Addition \n") ;
printf("- : Subtraction \n") ;
printf("* : Multiplication \n") ;
printf("/ : Division \n") ;
printf("Q : Quit \n") ;
printf("Enter your choice: ") ;
scanf(" %c", &choice) ;
if(choice=='+' || choice=='-' || choice=='*' || choice=='/')
{
printf("Enter 2 numbers: ") ;
scanf("%f %f", &a, &b);
}
switch(choice)
{
case '+' :
addition(a,b);
break;
case '-' :
subtraction(a,b);
break;
case '*' :
multiplication(a,b);
break;
case '/' :
division(a,b);
break;
case 'q' :
break;
case 'Q' :
break;
default :
printf("Invalid Choice. Enter again.\n");
}
}
while(choice!='q' && choice!='Q');
getch() ;
}
void addition(float a, float b)
{
printf("Sum of two numbers is: %f \n", a+b) ;
}
void subtraction(float a, float b)
{
printf("Diffrence of two numbers is: %f \n", a-b) ;
}
void multiplication(float a, float b)
{
printf("Product of two numbers is: %f \n", a*b) ;
}
void division(float a, float b)
{
printf("Result of division is: %f \n", a/b) ;
}
/*
Output:
Menu:
+ : Addition
- : Subtraction
* : Multiplication
/ : Division
Q : Quit
Enter your choice: +
Enter 2 numbers: 2 5
Sum of two numbers is: 7.000000
Menu:
+ : Addition
- : Subtraction
* : Multiplication
/ : Division
Q : Quit
Enter your choice: -
Enter 2 numbers: 2 5
Diffrence of two numbers is: -3.000000
Menu:
+ : Addition
- : Subtraction
* : Multiplication
/ : Division
Q : Quit
Enter your choice: *
Enter 2 numbers: 2 5
Product of two numbers is: 10.000000
Menu:
+ : Addition
- : Subtraction
* : Multiplication
/ : Division
Q : Quit
Enter your choice: /
Enter 2 numbers: 2 5
Result of division is: 0.400000
Menu:
+ : Addition
- : Subtraction
* : Multiplication
/ : Division
Q : Quit
Enter your choice: %
Invalid Choice. Enter again.
Menu:
+ : Addition
- : Subtraction
* : Multiplication
/ : Division
Q : Quit
Enter your choice: Q
*/
@@ -0,0 +1,46 @@
/* Square number - Write a function to check whether a given integer is a square number or not. Eg. 4 , 9 , 16 , 25 etc */
#include <stdio.h>
#include <conio.h>
#include <math.h>
int checksquare(int n) ;
void main()
{
int n, flag ;
clrscr();
printf("Enter an integer: ") ;
scanf("%d", &n) ;
flag=checksquare(n) ;
if(flag==1)
printf("%d is a square number", n) ;
else
printf("%d is not a square number", n) ;
getch();
}
int checksquare(int n)
{
double m;
int a;
m=sqrt(n);
a=m;
if(n==a*a)
return 1;
else
return 0;
}
/*
Output1:
Enter an integer: 16
16 is a square number
Output2:
Enter an integer: 8
8 is not a square number
*/
@@ -0,0 +1,47 @@
/* Palindrome Number - Write a function to check whether a given number is a palindrome or not , for example 1234321 */
#include <stdio.h>
#include <conio.h>
int palindrome(int n)
{
int r, rev=0, temp ;
temp=n ;
while(n!=0)
{
r=n%10 ;
rev=rev*10+r ;
n=n/10 ;
}
if(rev==temp)
return 1 ;
else
return 0 ;
}
void main()
{
int n, flag ;
clrscr() ;
printf("Enter a number: ") ;
scanf("%d", &n) ;
flag=palindrome(n) ;
if(flag==1)
printf("%d is a palindrome", n) ;
else
printf("%d is not a palindrome", n) ;
getch() ;
}
/*
Output1:
Enter a number: 24542
24542 is a palindrome
Output2:
Enter a number: 2464
2464 is not a palindrome
*/
@@ -0,0 +1,38 @@
/* Binomial Coefficient - Write a function to find factorial of a number. Using this function, find value of binomial co-efficient (B) where B = n! / r! * (n-r)! where n and r are natural numbers. */
#include <stdio.h>
#include <conio.h>
long fact(int n) ;
void main()
{
int n, r ;
long f1, f2, f3, b ;
clrscr() ;
printf("Enter natural numbers n and r (n>r): ") ;
scanf("%d %d", &n, &r) ;
f1=fact(n) ;
f2=fact(r) ;
f3=fact(n-r) ;
b=f1/(f2*f3) ;
printf("Binomial Coefficient = %ld", b) ;
getch() ;
}
long fact(int n)
{
int i;
long f=1 ;
for(i=1 ; i<=n ; i++)
f=f*i ;
return f ;
}
/*
Output:
Enter natural numbers n and r (n>r): 9 5
Binomial Coefficient = 126
*/
@@ -0,0 +1,40 @@
/* power(m,n) - Write a non-recursive function to find m^n where m is a real number and n is an integer */
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
float power(float m, int n)
{
int i ;
float result=1 ;
for(i=1 ; i<=abs(n) ; i++)
result=result*m;
if(n<0)
return (1/result) ;
else
return result;
}
void main()
{
int n ;
float m ;
clrscr() ;
printf("Enter m and n: ");
scanf("%f %d", &m, &n);
printf("m raise to n is %f", power(m,n) );
getch();
}
/*
Output1:
Enter m and n: 2.5 3
m raise to n is 15.625000
Output2:
Enter m and n: 2 -3
m raise to n is 0.125000
*/
@@ -0,0 +1,37 @@
/*
Write a program using recursive function 'power' to compute x^n
power(x,n) = 1 , if n = 0
power(x,n) = x , if n = 1
power(x,n) = x * power(x,n-1) , otherwise
*/
#include <stdio.h>
#include <conio.h>
float power(float x, int n)
{
if(n==0)
return 1 ;
else if(n==1)
return x ;
else
return ( x * power(x, n-1) ) ;
}
void main()
{
float x ;
int n ;
clrscr() ;
printf("Enter x and n: ") ;
scanf("%f %d", &x, &n) ;
printf("x raise to n = %f", power(x,n) ) ;
getch() ;
}
/*
Output:
Enter x and n: 2.5 3
x raise to n = 15.625000
*/
@@ -0,0 +1,29 @@
/* Sum of natural numbers - Write a recursive function to calculate 1+2+3+...+n */
#include <stdio.h>
#include <conio.h>
long sum(int n)
{
if(n==1)
return 1 ;
else
return ( n + sum(n-1) ) ;
}
void main()
{
int n ;
clrscr() ;
printf("Enter n: ") ;
scanf("%d", &n) ;
printf("Sum of first %d natural numbers is %ld", n, sum(n) ) ;
getch() ;
}
/*
Output:
Enter n: 3
Sum of first 3 natural numbers is 6
*/
@@ -0,0 +1,30 @@
/* Factorial - Write a recursive function to find factorial of a given number n */
#include <stdio.h>
#include <conio.h>
long fact(int n)
{
if(n==0)
return 1 ;
else
return ( n * fact(n-1) ) ;
}
void main()
{
int n ;
clrscr() ;
printf("Enter n:") ;
scanf("%d", &n) ;
printf("Factorial of %d is %ld", n, fact(n) ) ;
getch() ;
}
/*
Output:
Enter n:4
Factorial of 4 is 24
*/
@@ -0,0 +1,49 @@
/* Fibonacci - Write a recursive function to find the nth term of fibonacci series. Using this function print the first n terms of fibonacci series */
/* We assume that fibonacci series starts with 1 1 */
#include <stdio.h>
#include <conio.h>
long fibo(int n)
{
if( (n==1) || (n==2) )
return 1 ;
else
return( fibo(n-1) + fibo(n-2) ) ;
}
void main()
{
int i, n ;
clrscr() ;
printf("Enter n:") ;
scanf("%d", &n) ;
printf("First %d terms of fibonacci series are: \n", n) ;
for(i=1 ; i<=n ; i++)
printf("%ld ", fibo(i) ) ;
/* Dont write fibo(n) by mistake */
getch() ;
}
/* If fibonacci series starts with 0 1 then the following function should be given
long fibo (int n)
{
if(n==1)
return 0;
else if (n==2)
return 1;
else
return (fibo(n-1) + fibo(n-2));
}
*/
/*
Output:
Enter n:4
First 4 terms of fibonacci series are:
1 1 2 3
*/
@@ -0,0 +1,41 @@
/* GCD Recursive - Write recursive function to calculate gcd of 2 numbers using the following Euclid's recursive algorithm:
gcd(n,m) , if n>m
gcd(m,n) = m , if n=0
gcd(n,m mod n) , otherwise
*/
#include <stdio.h>
#include <conio.h>
int gcd(int m, int n)
{
if(n>m)
return ( gcd(n,m) ) ;
else if(n==0)
return m ;
else
return ( gcd(n,m%n) ) ;
}
void main()
{
int m, n ;
clrscr();
printf("Enter 2 positive integers: ") ;
scanf("%d %d", &m, &n) ;
printf("GCD is: %d", gcd(m,n) );
getch() ;
}
/*
Output1:
Enter 2 positive integers: 18 24
GCD is: 6
Output2:
Enter 2 positive integers: 5 3
GCD is: 1
*/
@@ -0,0 +1,30 @@
/* power(m,n) - Use recursive function to find value of m^n where m is a real number and n is an integer greater than or equal to zero */
#include <stdio.h>
#include <conio.h>
float power(float m, int n)
{
if(n==0)
return 1 ;
else
return (m * power(m,n-1) ) ;
}
void main()
{
float m ;
int n ;
clrscr() ;
printf("Enter m and n: ") ;
scanf("%f %d", &m, &n) ;
printf("m raise to n = %f", power(m,n) ) ;
getch() ;
}
/*
Output:
Enter m and n: 2.5 3
m raise to n = 15.625000
*/
@@ -0,0 +1,60 @@
/* Decimal to Octal , Hexa , Binary - Using recursion write a program to find octal , binary and hexadecimal equivalent of the natural (decimal) number entered by the user */
#include <stdio.h>
#include <conio.h>
void octal(int n)
{
if(n!=0)
{
octal(n/8) ;
printf("%d", n%8) ;
}
}
void binary(int n)
{
if(n!=0)
{
binary(n/2) ;
printf("%d", n%2) ;
}
}
void hexa(int n)
{
if(n!=0)
{
hexa(n/16) ;
if(n%16 < 10)
printf("%d", n%16) ;
else
printf("%c", n%16 + 55) ;
}
}
void main()
{
int n ;
clrscr() ;
printf("Enter a natural number: ") ;
scanf("%d", &n) ;
printf("Equivalent octal, binary and hexadecimal numbers are as shown: \n") ;
octal(n) ;
printf("\n") ;
binary(n) ;
printf("\n") ;
hexa(n) ;
printf("\n") ;
getch() ;
}
/*
Output:
Enter a natural number: 43
Equivalent octal, binary and hexadecimal numbers are as shown:
53
101011
2B
*/
@@ -0,0 +1,15 @@
/*
* C program to find the area of a circle, given the radius
*/
#include <stdio.h>
#include <math.h>
#define PI 3.142
void main()
{
float radius, area;
printf("Enter the radius of a circle \n");
scanf("%f", &radius);
area = PI * pow(radius, 2);
printf("Area of a circle = %5.2f\n", area);
}
@@ -0,0 +1,16 @@
/*
* C program to find the area of a triangle, given three sides
*/
#include <stdio.h>
#include <math.h>
void main()
{
int s, a, b, c, area;
printf("Enter the values of a, b and c \n");
scanf("%d %d %d", &a, &b, &c);
/* compute s */
s = (a + b + c) / 2;
area = sqrt(s * (s - a) * (s - b) * (s - c));
printf("Area of a triangle = %d \n", area);
}
@@ -0,0 +1,37 @@
/*
* C program to input real numbers and find the mean, variance
* and standard deviation
*/
#include <stdio.h>
#include <math.h>
#define MAXSIZE 10
void main()
{
float x[MAXSIZE];
int i, n;
float average, variance, std_deviation, sum = 0, sum1 = 0;
printf("Enter the value of N \n");
scanf("%d", &n);
printf("Enter %d real numbers \n", n);
for (i = 0; i < n; i++)
{
scanf("%f", &x[i]);
}
/* Compute the sum of all elements */
for (i = 0; i < n; i++)
{
sum = sum + x[i];
}
average = sum / (float)n;
/* Compute variance and standard deviation */
for (i = 0; i < n; i++)
{
sum1 = sum1 + pow((x[i] - average), 2);
}
variance = sum1 / (float)n;
std_deviation = sqrt(variance);
printf("Average of all elements = %.2f\n", average);
printf("variance of all elements = %.2f\n", variance);
printf("Standard deviation = %.2f\n", std_deviation);
}
@@ -0,0 +1,18 @@
/*
* C program to find the simple interest, given principal,
* rate of interest and time.
*/
#include <stdio.h>
void main()
{
float principal_amt, rate, simple_interest;
int time;
printf("Enter the values of principal_amt, rate and time \n");
scanf("%f %f %d", &principal_amt, &rate, &time);
simple_interest = (principal_amt * rate * time) / 100.0;
printf("Amount = Rs. %5.2f\n", principal_amt);
printf("Rate = Rs. %5.2f%\n", rate);
printf("Time = %d years\n", time);
printf("Simple interest = %5.2f\n", simple_interest);
}
@@ -0,0 +1,33 @@
/*
* C program to find the sum of cos(x) series
*/
#include <stdio.h>
#include <math.h>
void main()
{
int n, x1, i, j;
float x, sign, cosx, fact;
printf("Enter the number of the terms in a series\n");
scanf("%d", &n);
printf("Enter the value of x(in degrees)\n");
scanf("%f", &x);
x1 = x;
/* Degrees to radians */
x = x * (3.142 / 180.0);
cosx = 1;
sign = -1;
for (i = 2; i <= n; i = i + 2)
{
fact = 1;
for (j = 1; j <= i; j++)
{
fact = fact * j;
}
cosx = cosx + (pow(x, i) / fact) * sign;
sign = sign * (-1);
}
printf("Sum of the cosine series = %7.2f\n", cosx);
printf("The value of cos(%d) using library function = %f\n", x1,
cos(x));
}
@@ -0,0 +1,35 @@
/*
* C program to find the value of cos(x) using the series
* up to the given accuracy (without using user defined function)
* also print cos(x) using library function.
*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void main()
{
int n, x1;
float accuracy, term, denominator, x, cosx, cosval;
printf("Enter the value of x (in degrees) \n");
scanf("%f", &x);
x1 = x;
/* Converting degrees to radians */
x = x * (3.142 / 180.0);
cosval = cos(x);
printf("Enter the accuracy for the result \n");
scanf("%f", &accuracy);
term = 1;
cosx = term;
n = 1;
do
{
denominator = 2 * n * (2 * n - 1);
term = -term * x * x / denominator;
cosx = cosx + term;
n = n + 1;
}
while (accuracy <= fabs(cosval - cosx));
printf("Sum of the cosine series = %f\n", cosx);
printf("Using Library function cos(%d) = %f\n", x1, cos(x));
}
@@ -0,0 +1,35 @@
/*
* C program to find the value of sin(x) using the series
* up to the given accuracy (without using user defined function)
* also print sin(x) using library function.
*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void main()
{
int n, x1;
float accuracy, term, denominator, x, sinx, sinval;
printf("Enter the value of x (in degrees) \n");
scanf("%f", &x);
x1 = x;
/* Converting degrees to radians */
x = x * (3.142 / 180.0);
sinval = sin(x);
printf("Enter the accuracy for the result \n");
scanf("%f", &accuracy);
term = x;
sinx = term;
n = 1;
do
{
denominator = 2 * n * (2 * n + 1);
term = -term * x * x / denominator;
sinx = sinx + term;
n = n + 1;
}
while (accuracy <= fabs(sinval - sinx));
printf("Sum of the sine series = %f \n", sinx);
printf("Using Library function sin(%d) = %f\n", x1, sin(x));
}
@@ -0,0 +1,31 @@
/*
* C program to check whether a given number is prime or not
* and output the given number with suitable message.
*/
#include <stdio.h>
#include <stdlib.h>
void main()
{
int num, j, flag;
printf("Enter a number \n");
scanf("%d", &num);
if (num <= 1)
{
printf("%d is not a prime numbers \n", num);
exit(1);
}
flag = 0;
for (j = 2; j <= num / 2; j++)
{
if ((num % j) == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
printf("%d is a prime number \n", num);
else
printf("%d is not a prime number \n", num);
}
@@ -0,0 +1,16 @@
/*
* C program to compute the surface area and volume of a cube
*/
#include <stdio.h>
#include <math.h>
void main()
{
float side, surfacearea, volume;
printf("Enter the length of a side \n");
scanf("%f", &side);
surfacearea = 6.0 * side * side;
volume = pow(side, 3);
printf("Surface area = %6.2f and Volume = %6.2f \n", surfacearea,
volume);
}
@@ -0,0 +1,28 @@
/*
* C program to compute the value of X ^ N given X and N as inputs
*/
#include <stdio.h>
#include <math.h>
long int power(int x, int n);
void main()
{
long int x, n, xpown;
printf("Enter the values of X and N \n");
scanf("%ld %ld", &x, &n);
xpown = power(x, n);
printf("X to the power N = %ld\n", xpown);
}
/* Recursive function to computer the X to power N */
long int power(int x, int n)
{
if (n == 1)
return(x);
else if (n % 2 == 0)
/* if n is even */
return (pow(power(x, n/2), 2));
else
/* if n is odd */
return (x * power(x, n - 1));
}
@@ -0,0 +1,17 @@
/*
* C Program to Display Floyds triangle
*/
#include <stdio.h>
main( )
{
int i, j, k = 1;
printf("floyds triangle is\n");
for( i = 1; k <= 20; ++i )
{
for( j = 1; j <= i; ++j )
printf( "%d ", k++ );
printf( "\n\n" );
}
return 0;
}
@@ -0,0 +1,29 @@
/*
* C Program to Display Pascal triangle
*/
#include <stdio.h>
void main()
{
int array[15][15], i, j, rows, num = 25, k;
printf("\n enter the number of rows:");
scanf("%d", &rows);
for (i = 0; i < rows; i++)
{
for (k = num - 2 * i; k >= 0; k--)
printf(" ");
for (j = 0; j <= i; j++)
{
if (j == 0 || i == j)
{
array[i][j] = 1;
}
else
{
array[i][j] = array[i - 1][j - 1] + array[i - 1][j];
}
printf("%4d", array[i][j]);
}
printf("\n");
}
}
@@ -0,0 +1,52 @@
/*
* C program to evaluate a given polynomial by reading its coefficients
* in an array.
* P(x) = AnXn + An-1Xn-1 + An-2Xn-2+... +A1X + A0
*
* The polynomial can be written as:
* P(x) = A0 + X(A1 + X(A2 + X(A3 + X(Q4 + X(...X(An-1 + XAn))))
* and evaluated starting from the inner loop
*/
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 10
void main()
{
int array[MAXSIZE];
int i, num, power;
float x, polySum;
printf("Enter the order of the polynomial \n");
scanf("%d", &num);
printf("Enter the value of x \n");
scanf("%f", &x);
/* Read the coefficients into an array */
printf("Enter %d coefficients \n", num + 1);
for (i = 0; i <= num; i++)
{
scanf("%d", &array[i]);
}
polySum = array[0];
for (i = 1; i <= num; i++)
{
polySum = polySum * x + array[i];
}
power = num;
printf("Given polynomial is: \n");
for (i = 0; i <= num; i++)
{
if (power < 0)
{
break;
}
/* printing proper polynomial function */
if (array[i] > 0)
printf(" + ");
else if (array[i] < 0)
printf(" - ");
else
printf(" ");
printf("%dx^%d ", abs(array[i]), power--);
}
printf("\n Sum of the polynomial = %6.2f \n", polySum);
}
@@ -0,0 +1,19 @@
/*
* C Program to Find & Display Multiplication table
*/
#include <stdio.h>
int main()
{
int number, i = 1;
printf(" Enter the Number:");
scanf("%d", &number);
printf("Multiplication table of %d:\n ", number);
printf("--------------------------\n");
while (i <= 10)
{
printf(" %d x %d = %d \n ", number, i, number * i);
i++;
}
return 0;
}
@@ -0,0 +1,15 @@
/*
* C Program to Find Area of Parallelogram
*/
#include <stdio.h>
int main()
{
float base, altitude;
float area;
printf("Enter base and altitude of the given Parallelogram: \n ");
scanf("%f%f", &base, &altitude);
area = base * altitude;
printf("Area of Parallelogram is: %.3f\n", area);
return 0;
}
@@ -0,0 +1,15 @@
/*
* C Program to Find Area of rhombus
*/
#include <stdio.h>
int main()
{
float diagonal1, diagonal2;
float area;
printf("Enter diagonals of the given rhombus: \n ");
scanf("%f%f", &diagonal1, &diagonal2);
area = 0.5 * diagonal1 * diagonal2;
printf("Area of rhombus is: %.3f \n", area);
return 0;
}
@@ -0,0 +1,15 @@
/*
* C Program to Find Area of Trapezium
*/
#include <stdio.h>
int main()
{
float a, b, h;
float area;
printf("Enter the value for two bases & height of the trapezium: \n");
scanf("%f%f%f", &a, &b, &h);
area = 0.5 * (a + b) * h ;
printf("Area of the trapezium is: %.3f", area);
return 0;
}
@@ -0,0 +1,15 @@
/*
* C Program to Find Area of a Right Angled Triangle
*/
#include <stdio.h>
int main()
{
float height, width;
float area;
printf("Enter height and width of the given triangle:\n ");
scanf("%f%f", &height, &width);
area = 0.5 * height * width;
printf("Area of right angled triangle is: %.3f\n", area);
return 0;
}
@@ -0,0 +1,24 @@
/*
* C program to generate and print first N FIBONACCI numbers
* in the series.
*/
#include <stdio.h>
void main()
{
int fib1 = 0, fib2 = 1, fib3, num, count = 0;
printf("Enter the value of num \n");
scanf("%d", &num);
printf("First %d FIBONACCI numbers are ...\n", num);
printf("%d\n", fib1);
printf("%d\n", fib2);
count = 2; /* fib1 and fib2 are already used */
while (count < num)
{
fib3 = fib1 + fib2;
count++;
printf("%d\n", fib3);
fib1 = fib2;
fib2 = fib3;
}
}
@@ -0,0 +1,32 @@
/*
* C Program to find GCD of given Numbers using Recursion
*/
#include <stdio.h>
int gcd(int, int);
int main()
{
int a, b, result;
printf("Enter the two numbers to find their GCD: ");
scanf("%d%d", &a, &b);
result = gcd(a, b);
printf("The GCD of %d and %d is %d.\n", a, b, result);
}
int gcd(int a, int b)
{
while (a != b)
{
if (a > b)
{
return gcd(a - b, b);
}
else
{
return gcd(a, b - a);
}
}
return a;
}
}
@@ -0,0 +1,29 @@
/*
* C Program to Find LCM of a Number using Recursion
*/
#include <stdio.h>
int lcm(int, int);
int main()
{
int a, b, result;
int prime[100];
printf("Enter two numbers: ");
scanf("%d%d", &a, &b);
result = lcm(a, b);
printf("The LCM of %d and %d is %d\n", a, b, result);
return 0;
}
int lcm(int a, int b)
{
static int common = 1;
if (common % a == 0 && common % b == 0)
{
return common;
}
common++;
lcm(a, b);
return common;
}
@@ -0,0 +1,42 @@
/*
* C program to find prime numbers in a given range.
* Also print the number of prime numbers.
*/
#include <stdio.h>
#include <stdlib.h>
void main()
{
int num1, num2, i, j, flag, temp, count = 0;
printf("Enter the value of num1 and num2 \n");
scanf("%d %d", &num1, &num2);
if (num2 < 2)
{
printf("There are no primes upto %d\n", num2);
exit(0);
}
printf("Prime numbers are \n");
temp = num1;
if ( num1 % 2 == 0)
{
num1++;
}
for (i = num1; i <= num2; i = i + 2)
{
flag = 0;
for (j = 2; j <= i / 2; j++)
{
if ((i % j) == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
{
printf("%d\n", i);
count++;
}
}
printf("Number of primes between %d & %d = %d\n", temp, num2, count);
}
@@ -0,0 +1,26 @@
/*
* C Program to Find find Sum of the Series 1/1! + 2/2! + 3/3! + ……1/N!
*/
#include <stdio.h>
double sumseries(double);
main()
{
double number,sum;
printf("\n Enter the value: ");
scanf("%lf", &number);
sum = sumseries(number);
printf("\n Sum of the above series = %lf ", sum);
}
double sumseries(double m)
{
double sum2 = 0, f = 1, i;
for (i = 1; i <= m; i++)
{
f = f * i;
sum2 = sum2 +(i / f);
}
return(sum2);
}
@@ -0,0 +1,18 @@
/*
* C Program to Find Volume and Surface Area of Sphere
*/
#include <stdio.h>
#include <math.h>
int main()
{
float radius;
float surface_area, volume;
printf("Enter radius of the sphere : \n");
scanf("%f", &radius);
surface_area = 4 * (22/7) * radius * radius;
volume = (4.0/3) * (22/7) * radius * radius * radius;
printf("Surface area of sphere is: %.3f", surface_area);
printf("\n Volume of sphere is : %.3f", volume);
return 0;
}
@@ -0,0 +1,50 @@
/*
* C program to find out the roots of a quadratic equation
* for non-zero coefficients. In case of errors the program
* should report suitable error message.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void main()
{
float a, b, c, root1, root2;
float realp, imagp, disc;
printf("Enter the values of a, b and c \n");
scanf("%f %f %f", &a, &b, &c);
/* If a = 0, it is not a quadratic equation */
if (a == 0 || b == 0 || c == 0)
{
printf("Error: Roots cannot be determined \n");
exit(1);
}
else
{
disc = b * b - 4.0 * a * c;
if (disc < 0)
{
printf("Imaginary Roots\n");
realp = -b / (2.0 * a) ;
imagp = sqrt(abs(disc)) / (2.0 * a);
printf("Root1 = %f +i %f\n", realp, imagp);
printf("Root2 = %f -i %f\n", realp, imagp);
}
else if (disc == 0)
{
printf("Roots are real and equal\n");
root1 = -b / (2.0 * a);
root2 = root1;
printf("Root1 = %f\n", root1);
printf("Root2 = %f\n", root2);
}
else if (disc > 0 )
{
printf("Roots are real and distinct \n");
root1 =(-b + sqrt(disc)) / (2.0 * a);
root2 =(-b - sqrt(disc)) / (2.0 * a);
printf("Root1 = %f \n", root1);
printf("Root2 = %f \n", root2);
}
}
}
@@ -0,0 +1,49 @@
/*
* C program to find the areas of different geometrical shapes such as
* circle, square, rectangle etc using switch statements.
*/
#include <stdio.h>
void main()
{
int fig_code;
float side, base, length, breadth, height, area, radius;
printf("-------------------------\n");
printf(" 1 --> Circle\n");
printf(" 2 --> Rectangle\n");
printf(" 3 --> Triangle\n");
printf(" 4 --> Square\n");
printf("-------------------------\n");
printf("Enter the Figure code\n");
scanf("%d", &fig_code);
switch(fig_code)
{
case 1:
printf("Enter the radius\n");
scanf("%f", &radius);
area = 3.142 * radius * radius;
printf("Area of a circle = %f\n", area);
break;
case 2:
printf("Enter the breadth and length\n");
scanf("%f %f", &breadth, &length);
area = breadth * length;
printf("Area of a Reactangle = %f\n", area);
break;
case 3:
printf("Enter the base and height\n");
scanf("%f %f", &base, &height);
area = 0.5 * base * height;
printf("Area of a Triangle = %f\n", area);
break;
case 4:
printf("Enter the side\n");
scanf("%f", &side);
area = side * side;
printf("Area of a Square=%f\n", area);
break;
default:
printf("Error in figure code\n");
break;
}
}
@@ -0,0 +1,35 @@
/*
* C Program to find factorial of a given number using recursion
*/
#include <stdio.h>
int factorial(int);
int main()
{
int num;
int result;
printf("Enter a number to find it's Factorial: ");
scanf("%d", &num);
if (num < 0)
{
printf("Factorial of negative number not possible\n");
}
else
{
result = factorial(num);
printf("The Factorial of %d is %d.\n", num, result);
}
return 0;
}
int factorial(int num)
{
if (num == 0 || num == 1)
{
return 1;
}
else
{
return(num * factorial(num - 1));
}
}
@@ -0,0 +1,32 @@
/*
* C program to find the GCD and LCM of two integers using Euclids' algorithm
*/
#include <stdio.h>
void main()
{
int num1, num2, gcd, lcm, remainder, numerator, denominator;
printf("Enter two numbers\n");
scanf("%d %d", &num1, &num2);
if (num1 > num2)
{
numerator = num1;
denominator = num2;
}
else
{
numerator = num2;
denominator = num1;
}
remainder = numerator % denominator;
while (remainder != 0)
{
numerator = denominator;
denominator = remainder;
remainder = numerator % denominator;
}
gcd = denominator;
lcm = num1 * num2 / gcd;
printf("GCD of %d and %d = %d\n", num1, num2, gcd);
printf("LCM of %d and %d = %d\n", num1, num2, lcm);
}
@@ -0,0 +1,38 @@
/*
* C Program to find the nth number in Fibonacci series using recursion
*/
#include <stdio.h>
int fibo(int);
int main()
{
int num;
int result;
printf("Enter the nth number in fibonacci series: ");
scanf("%d", &num);
if (num < 0)
{
printf("Fibonacci of negative number is not possible.\n");
}
else
{
result = fibo(num);
printf("The %d number in fibonacci series is %d\n", num, result);
}
return 0;
}
int fibo(int num)
{
if (num == 0)
{
return 0;
}
else if (num == 1)
{
return 1;
}
else
{
return(fibo(num - 1) + fibo(num - 2));
}
}
@@ -0,0 +1,54 @@
/*
* C Program to Find the Perimeter of a Circle, Rectangle and Triangle
This C Program calculates the perimeter of a circle, rectangle and triangle. This program is used to find the perimeter of a circle, rectangle and triangle. The formula used in this program are
perimeter of rectangle: 2 * (a + b)
perimeter of General triangle: a + b + c
perimeter of Equilateral triangle: 3 * a
perimeter of Right angled triangle: width + height + sqrt(width ^ 2 + height ^ 2)
perimeter of circle: 2 * pi * r
*/
#include <stdio.h>
#include <math.h>
int main()
{
float radius, length, width, a, b, c, height;
int n;
float perimeter;
//Perimeter of rectangle
printf(" \n Perimeter of rectangle \n");
printf("---------------------------\n");
printf("\n Enter width and length of the rectangle : ");
scanf("%f%f", &width,& length);
perimeter = 2 * (width + length);
printf("Perimeter of rectangle is: %.3f", perimeter);
//Perimeter of triangle
printf("\n Perimeter of triangle n");
printf("---------------------------n");
printf("\n Enter the size of all sides of the triangle : ");
scanf("%f%f%f", &a, &b, &c);
perimeter = a + b + c;
printf("Perimeter of triangle is: %.3f", perimeter);
//Perimeter of circle
printf(" \n Perimeter of circle \n");
printf("---------------------------\n");
printf("\n Enter the radius of the circle : ");
scanf("%f", &radius);
perimeter = 2 * (22 / 7) * radius;
printf("Perimeter of circle is: %.3f", perimeter);
//Perimeter of equilateral triangle
printf(" \n Perimeter of equilateral triangle \n");
printf("---------------------------\n");
printf("\n Enter any side of the equilateral triangle : ");
scanf("%f", &a);
perimeter = 3 * a;
printf("Perimeter of equilateral triangle is: %.3f", perimeter);
//Perimeter of right angled triangle
printf(" \n Perimeter of right angled triangle \n");
printf("---------------------------\n");
printf("\n Enter the width and height of the right angled triangle : ");
scanf("%f%f", &width, &height);
perimeter = width + height + sqrt(width * width + height * height);
printf("Perimeter of right angled triangle is: %.3f", perimeter);
return 0;
}
@@ -0,0 +1,28 @@
/*
* C Program to Find the Sum of A.P Series
*/
#include <stdio.h>
#include <math.h>
int main()
{
int a, d, n, i, tn;
int sum = 0;
printf("Enter the first term value of the A.P. series: ");
scanf("%d", &a);
printf("Enter the total numbers in the A.P. series: ");
scanf("%d", &n);
printf("Enter the common difference of A.P. series: ");
scanf("%d", &d);
sum = (n * (2 * a + (n - 1)* d ))/ 2;
tn = a + (n - 1) * d;
printf("Sum of the A.P series is: ");
for (i = a; i <= tn; i = i + d )
{
if (i != tn)
printf("%d + ", i);
else
printf("%d = %d ", i, sum);
}
return 0;
}
@@ -0,0 +1,16 @@
/*
* C program to find the sum of 'N' natural numbers
*/
#include <stdio.h>
void main()
{
int i, num, sum = 0;
printf("Enter an integer number \n");
scanf ("%d", &num);
for (i = 1; i <= num; i++)
{
sum = sum + i;
}
printf ("Sum of first %d natural numbers = %d\n", num, sum);
}
@@ -0,0 +1,22 @@
/*
* C Program to Find the Sum of G.P Series
*/
#include <stdio.h>
#include <math.h>
int main()
{
float a, r, i, last_term, sum = 0;
int n;
printf("Enter the first term of the G.P. series: ");
scanf("%f", &a);
printf("Enter the total numbers in the G.P. series: ");
scanf("%d", &n);
printf("Enter the common ratio of G.P. series: ");
scanf("%f", &r);
sum = (a *(1 - pow(r, n + 1))) / (1 - r);
last_term = a * pow(r, n - 1);
printf("last_term term of G.P.: %f", last_term);
printf("\n Sum of the G.P.: %f", sum);
return 0;
}
@@ -0,0 +1,20 @@
/*
* C Program to Find the Sum of H.P Series
*/
#include <stdio.h>
void main()
{
int n;
float i, sum, term;
printf("1 + 1 / 2 + 1 / 3 +......+1 / n \n");
printf("Enter the value of n \n");
scanf("%d", &n);
sum = 0;
for (i = 1; i <= n; i++)
{
term = 1 / i;
sum = sum + term;
}
printf("the Sum of H.P Series is = %f", sum);
}
@@ -0,0 +1,22 @@
/*
* C Program to Find the Volume and Surface Area of Cuboids
*/
#include <stdio.h>
#include <math.h>
int main()
{
float width, length, height;
float surfacearea, volume, space_diagonal;
printf("Enter value of width, length & height of the cuboids:\n");
scanf("%f%f%f", &width, &length, &height);
surfacearea = 2 *(width * length + length * height +
height * width);
volume = width * length * height;
space_diagonal = sqrt(width * width + length * length +
height * height);
printf("Surface area of cuboids is: %.3f", surfacearea);
printf("\n Volume of cuboids is : %.3f", volume);
printf("\n Space diagonal of cuboids is : %.3f", space_diagonal);
return 0;
}
@@ -0,0 +1,18 @@
/*
* C Program to Find the Volume and Surface Area of cylinder
*/
#include <stdio.h>
#include <math.h>
int main()
{
float radius, height;
float surface_area, volume;
printf("Enter value for radius and height of a cylinder : \n");
scanf("%f%f", &radius, &height);
surface_area = 2 * (22 / 7) * radius * (radius + height);
volume = (22 / 7) * radius * radius * height;
printf("Surface area of cylinder is: %.3f", surface_area);
printf("\n Volume of cylinder is : %.3f", volume);
return 0;
}
@@ -0,0 +1,18 @@
/*
* C Program to Find the volume and surface area of cone
*/
#include <stdio.h>
#include <math.h>
int main()
{
float radius, height;
float surface_area, volume;
printf("Enter value of radius and height of a cone :\n ");
scanf("%f%f", &radius, &height);
surface_area = (22 / 7) * radius * (radius + sqrt(radius * radius + height * height));
volume = (1.0/3) * (22 / 7) * radius * radius * height;
printf("Surface area of cone is: %.3f", surface_area);
printf("\n Volume of cone is : %.3f", volume);
return 0;
}
@@ -0,0 +1,24 @@
/*
* C program to generate Fibonacci Series. Fibonacci Series
* is 0 1 1 2 3 5 8 13 21 ...
*/
#include <stdio.h>
void main()
{
int fib1 = 0, fib2 = 1, fib3, limit, count = 0;
printf("Enter the limit to generate the Fibonacci Series \n");
scanf("%d", &limit);
printf("Fibonacci Series is ...\n");
printf("%d\n", fib1);
printf("%d\n", fib2);
count = 2;
while (count < limit)
{
fib3 = fib1 + fib2;
count++;
printf("%d\n", fib3);
fib1 = fib2;
fib2 = fib3;
}
}
@@ -0,0 +1,65 @@
/*
* C Program to Implement a strpbrk() Function
*/
#include <stdio.h>
char* strpbrk(char *, char *);
int main()
{
char string1[50], string2[50];
char *pos;
printf("Enter the String:
");
scanf(" %[^
]s", string1);
printf("
Enter the Character Set:
");
scanf(" %[^
]s", string2);
pos=strpbrk(string1, string2);
printf("%s", pos);
}
/* Locates First occurrence in string s1 of any character in string s2,
* If a character from string s2 is found ,
* a pointer to the character in string s1 is returned,
* otherwise, a NULL pointer is returned.
*/
char* strpbrk(char *string1, char *string2)
{
int i, j, pos, flag = 0;
for (i = 0; string1[i] != ''; i++);
pos = i;
for (i = 0; string2[i] != ''; i++)
{
for (j = 0; string1[j] != ''; j++)
{
if (string2[i] == string1[j])
{
if ( j <= p1)
{
pos = j;
flag = 1;
}
}
}
}
if (flag == 1)
{
return &string1[pos];
}
else
{
return NULL;
}
}
Enter the String:
C programming Class
Enter the Character Set:
mp
programming Class
@@ -0,0 +1,21 @@
/*
* C program to find the factorial of a given number
*/
#include <stdio.h>
void main()
{
int i, fact = 1, num;
printf("Enter the number \n");
scanf("%d", &num);
if (num <= 0)
fact = 1;
else
{
for (i = 1; i <= num; i++)
{
fact = fact * i;
}
}
printf("Factorial of %d = %5d\n", num, fact);
}
@@ -0,0 +1,22 @@
/*
* C program to accept a coordinate point in a XY coordinate system
* and determine its quadrant
*/
#include <stdio.h>
void main()
{
int x, y;
printf("Enter the values for X and Y\n");
scanf("%d %d", &x, &y);
if (x > 0 && y > 0)
printf("point (%d, %d) lies in the First quandrant\n");
else if (x < 0 && y > 0)
printf("point (%d, %d) lies in the Second quandrant\n");
else if (x < 0 && y < 0)
printf("point (%d, %d) lies in the Third quandrant\n");
else if (x > 0 && y < 0)
printf("point (%d, %d) lies in the Fourth quandrant\n");
else if (x == 0 && y == 0)
printf("point (%d, %d) lies at the origin\n");
}
@@ -0,0 +1,37 @@
/*
* C program to simulate a simple calculator to perform arithmetic
* operations like addition, subtraction, multiplication and division
*/
#include <stdio.h>
void main()
{
char operator;
float num1, num2, result;
printf("Simulation of a Simple Calculator\n");
printf("*********************************\n");
printf("Enter two numbers \n");
scanf("%f %f", &num1, &num2);
fflush(stdin);
printf("Enter the operator [+,-,*,/] \n");
scanf("%s", &operator);
switch(operator)
{
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
default :
printf("Error in operationn");
break;
}
printf("\n %5.2f %c %5.2f = %5.2f\n", num1, operator, num2, result);
}
@@ -0,0 +1,31 @@
/*
* C Program to find HCF of a given Number using Recursion
*/
#include <stdio.h>
int hcf(int, int);
int main()
{
int a, b, result;
printf("Enter the two numbers to find their HCF: ");
scanf("%d%d", &a, &b);
result = hcf(a, b);
printf("The HCF of %d and %d is %d.\n", a, b, result);
}
int hcf(int a, int b)
{
while (a != b)
{
if (a > b)
{
return hcf(a - b, b);
}
else
{
return hcf(a, b - a);
}
}
return a;
}
@@ -0,0 +1,32 @@
/*
* C Program to find HCF of a given Number without using Recursion
*/
#include <stdio.h>
int hcf(int, int);
int main()
{
int a, b, result;
printf("Enter the two numbers to find their HCF: ");
scanf("%d%d", &a, &b);
result = hcf(a, b);
printf("The HCF of %d and %d is %d.\n", a, b, result);
return 0;
}
int hcf(int a, int b)
{
while (a != b)
{
if (a > b)
{
a = a - b;
}
else
{
b = b - a;
}
}
return a;
}
@@ -0,0 +1,28 @@
/*
* C Program to find Power of a Number using Recursion
*/
#include <stdio.h>
long power (int, int);
int main()
{
int pow, num;
long result;
printf("Enter a number: ");
scanf("%d", &num);
printf("Enter it's power: ");
scanf("%d", &pow);
result = power(num, pow);
printf("%d^%d is %ld", num, pow, result);
return 0;
}
long power (int num, int pow)
{
if (pow)
{
return (num * power(num, pow - 1));
}
return 1;
}
@@ -0,0 +1,18 @@
#include<stdio.h>
int findFactorial(int);
int main()
{
int i,factorial,num;
printf("Enter a number: ");
scanf("%d",&num);
factorial = findFactorial(num);
printf("Factorial of %d is: %d",num,factorial);
return 0;
}
int findFactorial(int num)
{
int i,f=1;
for(i=1; i<=num; i++)
f=f*i;
return f;
}
@@ -0,0 +1,15 @@
#include<stdio.h>
void main()
{
int rev(int);
int r,a;
clrscr();
printf("enter any no: ");
scanf("%d",&a);
r=rev(a);
printf("square is : %d",r);
}
int rev(int x)
{
return(x*x);
}
@@ -0,0 +1,22 @@
/*
* C Program to find the Sum of Series 1 + 1/2 + 1/3 + 1/4 + ... + 1/N
*/
#include <stdio.h>
void main()
{
double number, sum = 0, i;
printf("\n enter the number ");
scanf("%lf", &number);
for (i = 1; i <= number; i++)
{
sum = sum + (1 / i);
if (i == 1)
printf("\n 1 +");
else if (i == number)
printf(" (1 / %lf)", i);
else
printf(" (1 / %lf) + ", i);
}
printf("\n The sum of the given series is %.2lf", sum);
}
@@ -0,0 +1,22 @@
/*
* C Program to find the sum of series 1^2 + 2^2 + …. + n^2.
*/
#include <stdio.h>
int main()
{
int number, i;
int sum = 0;
printf("Enter maximum values of series number: ");
scanf("%d", &number);
sum = (number * (number + 1) * (2 * number + 1 )) / 6;
printf("Sum of the above given series : ");
for (i = 1; i <= number; i++)
{
if (i != number)
printf("%d^2 + ", i);
else
printf("%d^2 = %d ", i, sum);
}
return 0;
}
@@ -0,0 +1,45 @@
/* Pascal Triangle - Program to print Pascals triangle for n rows */
#include <stdio.h>
#include <conio.h>
long fact(int n) ;
void main()
{
int n, r, c, j ;
clrscr() ;
printf("Enter the number of rows: ") ;
scanf("%d", &n) ;
printf("Pascal triangle for %d rows is as shown: \n", n) ;
for(r=0 ; r<n ; r++)
{
for(j=1 ; j<=n-r-1 ; j++)
printf(" ") ;
for(c=0 ; c<=r ; c++)
printf("%ld ", fact(r) / ( fact(c) * fact(r-c) ) ) ;
printf("\n") ;
}
getch() ;
}
long fact(int n)
{
int i;
long f=1 ;
for(i=1 ; i<=n ; i++)
f=f*i ;
return f ;
}
/*
Output:
Enter the number of rows: 4
1
1 1
1 2 1
1 3 3 1
*/
@@ -0,0 +1,31 @@
#include<stdio.h>
#include<conio.h>
void main()
{
void table();
table();
getch();
}
void table()
{
int n,i,r;
printf("enter a no to know table: ");
scanf("%d",&n);
for(i=1; i<=10; i++)
{
r=n*i;
printf("%d*%d=%d\n",n,i,r);
}
}
Output:
enter a no to know table: 2
2*1=2
2*2=4
2*3=6
2*4=8
2*5=10
2*6=12
2*7=14
2*8=16
2*9=18
2*10=20
@@ -0,0 +1,17 @@
#include<stdio.h>
void main()
{
void swap(int,int);
int a,b,r;
printf("enter value for a&b: ");
scanf("%d%d",&a,&b);
swap(a,b);
}
void swap(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("after swapping the value for a & b is : %d %d",a,b);
}
@@ -0,0 +1,21 @@
/*
* C program to Calculate the Value of nPr
*/
#include <stdio.h>
void main(void)
{
printf("%d\n", fact(8));
int n, r;
printf("Enter value for n and r\n");
scanf("%d%d", &n, &r);
int npr = fact(n) / fact(n - r);
printf("\n Permutation values is = %d", npr);
}
int fact(int x)
{
if (x <= 1)
return 1;
return x * fact(x - 1);
}
@@ -0,0 +1,32 @@
/*
* C program to Calculate the value of nCr
*/
#include <stdio.h>
int fact(int z);
void main()
{
int n, r, ncr;
printf("\n Enter the value for N and R \n");
scanf("%d%d", &n, &r);
ncr = fact(n) / (fact(r) * fact(n - r));
printf("\n The value of ncr is: %d", ncr);
}
int fact(int z)
{
int f = 1, i;
if (z == 0)
{
return(f);
}
else
{
for (i = 1; i <= z; i++)
{
f = f * i;
}
}
return(f);
}
@@ -0,0 +1,28 @@
#include <stdio.h>
struct complex
{
int real, img;
};
int main()
{
struct complex a, b, c;
printf("Enter a and b where a + ib is the first complex number.\n");
printf("a = ");
scanf("%d", &a.real);
printf("b = ");
scanf("%d", &a.img);
printf("Enter c and d where c + id is the second complex number.\n");
printf("c = ");
scanf("%d", &b.real);
printf("d = ");
scanf("%d", &b.img);
c.real = a.real + b.real;
c.img = a.img + b.img;
if ( c.img >= 0 )
printf("Sum of two complex numbers = %d + %di\n", c.real, c.img);
else
printf("Sum of two complex numbers = %d %di\n", c.real, c.img);
return 0;
}
@@ -0,0 +1,41 @@
#include <stdio.h>
long factorial(int);
long find_ncr(int, int);
long find_npr(int, int);
int main()
{
int n, r;
long ncr, npr;
printf("Enter the value of n and r\n");
scanf("%d%d",&n,&r);
ncr = find_ncr(n, r);
npr = find_npr(n, r);
printf("%dC%d = %ld\n", n, r, ncr);
printf("%dP%d = %ld\n", n, r, npr);
return 0;
}
long find_ncr(int n, int r)
{
long result;
result = factorial(n)/(factorial(r)*factorial(n-r));
return result;
}
long find_npr(int n, int r)
{
long result;
result = factorial(n)/factorial(n-r);
return result;
}
long factorial(int n)
{
int c;
long result = 1;
for (c = 1; c <= n; c++)
result = result*c;
return result;
}
@@ -0,0 +1,14 @@
#include <stdio.h>
#include <stdlib.h>
int main()
{
int c, n;
printf("Ten random numbers in [1,100]\n");
for (c = 1; c <= 10; c++)
{
n = rand() % 100 + 1;
printf("%d\n", n);
}
return 0;
}
@@ -0,0 +1,70 @@
#include <stdio.h>
void merge(int [], int, int [], int, int []);
int main()
{
int a[100], b[100], m, n, c, sorted[200];
printf("Input number of elements in first array\n");
scanf("%d", &m);
printf("Input %d integers\n", m);
for (c = 0; c < m; c++)
{
scanf("%d", &a[c]);
}
printf("Input number of elements in second array\n");
scanf("%d", &n);
printf("Input %d integers\n", n);
for (c = 0; c < n; c++)
{
scanf("%d", &b[c]);
}
merge(a, m, b, n, sorted);
printf("Sorted array:\n");
for (c = 0; c < m + n; c++)
{
printf("%d\n", sorted[c]);
}
return 0;
}
void merge(int a[], int m, int b[], int n, int sorted[])
{
int i, j, k;
j = k = 0;
for (i = 0; i < m + n;)
{
if (j < m && k < n)
{
if (a[j] < b[k])
{
sorted[i] = a[j];
j++;
}
else
{
sorted[i] = b[k];
k++;
}
i++;
}
else if (j == m)
{
for (; i < m + n;)
{
sorted[i] = b[k];
k++;
i++;
}
}
else
{
for (; i < m + n;)
{
sorted[i] = a[j];
j++;
i++;
}
}
}
}