C program to generate Pascal triangle of Numbers

#include<stdio.h>

long fact(int);
int main(){
int line,i,j;

printf("Enter the no. of lines: ");
scanf("%d",&line);

for(i=0;i<line;i++){
for(j=0;j<line-i-1;j++)
printf(" ");

for(j=0;j<=i;j++)
printf("%ld ",fact(i)/(fact(j)*fact(i-j)));
printf("\n");
}
return 0;
}

long fact(int num){
long f=1;
int i=1;
while(i<=num){
f=f*i;
i++;
}
return f;
}

Write C programs that use both recursive and non-recursive functions To find the GCD (greatest common divisor) of two given integers


/* Write C programs that use both recursive and non-recursive functions
   To find the GCD (greatest common divisor) of two given integers.*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
unsigned int GcdRecursive(unsigned m, unsigned n);
unsigned int GcdNonRecursive(unsigned p,unsigned q);
 
int main(void)
{
  int a,b,iGcd;
  clrscr();
 
  printf("Enter the two numbers whose GCD is to be found: ");
  scanf("%d%d",&a,&b);
 
  printf("GCD of %d and %d Using Recursive Function is %d\n",a,b,GcdRecursive(a,b));
  printf("GCD of %d and %d Using Non-Recursive Function is %d\n",a,b,GcdNonRecursive(a,b));
 
  getch();
}

 
 


/* Non-Recursive Function*/
unsigned int GcdNonRecursive(unsigned p,unsigned q)
{
 unsigned remainder;
 int temp;
 while(1)
 {
 if(p == 0)
   {
               return q;
   }
   else if(q == 0)
   {
               return p;
   }
   else if(q<p)
   {
               temp=p%q;
               p=temp;
 
   }
   else  if(p<q)
   {
   temp=q%p;
   q=temp;
   }
 
}
}
 
 

/* Recursive Function*/
unsigned int GcdRecursive(unsigned m, unsigned n)
{
 if(n>m)
        return GcdRecursive(n,m);
 if(n==0)
         return m;
 else
     return GcdRecursive(n,m%n);
}
 
 


A Program to find GCD or HCF of two Integer Values in C

Definition of HCF (Highest common factor):

HFC is also called greatest common divisor (gcd). HCF of two numbers is a largest positive numbers which can divide both numbers without any remainder. For example HCF of two numbers 4 and 8 is 2 since 2 is the largest positive number which can dived 4 as well as 8 without a remainder.

Logic of HCF or GCD of any two numbers:

In HCF we try to find any largest number which can divide both the number.
For example: HCF or GCD of 20 and 30
Both number 20 and 30 are divisible by 1, 2,5,10.
HCF=max (1, 2, 3, 4, 10) =10

Logic for writing program:

It is clear that any number is not divisible by greater than number itself. In case of more than one numbers, a possible maximum number which can divide all of the numbers must be minimum of all of that numbers.

For example: 10, 20, and 30
Min (10, 20, 30) =10 can divide all there numbers. So we will take one for loop which will start form min of the numbers and will stop the loop when it became one, since all numbers are divisible by one. Inside for loop we will write one if conditions which will check divisibility of both the numbers.
Program:


#include<stdio.h>

int main(){

int x,y,m,i;

printf("Insert any two number: ");
scanf("%d%d",&x,&y);
if(x>y)
m=y;
else
m=x;

for(i=m;i>=1;i--){
if(x%i==0&&y%i==0){
printf("\nHCF of two number is : %d",i) ;
break;
}
}
return 0;
}

COMPUTER PROGRAMMING LAB for B.Tech CSE JNTU Hyd


JAWAHARLAL NEHRU TECHNOLOGICAL UNIVERSITY
HYDERABAD
I Year B.Tech C.S.E.                                                                                                              

COMPUTER PROGRAMMING LAB
Objectives:
·         To make the student learn a programming language.
·         To teach the student to write programs in C solve the problems
·         To Introduce the student to simple linear and non linear data structures such as lists, stacks, queues, trees and graphs.

Recommended Systems/Software Requirements:

·         Intel based desktop PC 
·         ANSI C Compiler with Supporting Editors

Week l.
a) Write a C program to find the sum of individual digits of a positive integer.
b) A Fibonacci Sequence is defined as follows: the first and second terms in the sequence are 0 and 1. Subsequent terms are found by adding the preceding two terms in the sequence. Write a C program to generate the first n terms of the sequence.
c) Write a C program to generate all the prime numbers between 1 and n, where n is a value supplied by the user.

Week 2.
a) Write a C program to calculate the following Sum:
                Sum=1-x2/2! +x4/4!-x6/6!+x8/8!-x10/10!
b) Write a C program toe find the roots of a quadratic equation.

Week 3
a)  Write C programs that use both recursive and non-recursive functions
                i) To find the factorial of a given integer.
                ii) To find the GCD (greatest common divisor) of two given integers.
                iii) To solve Towers of Hanoi problem.

Week 4
a) The total distance travelled by vehicle in ‘t’ seconds is given by distance      = ut+1/2at2 where ‘u’ and ‘a’ are the initial velocity (m/sec.) and acceleration (m/sec2). Write C program to find the distance travelled at regular intervals of time given the values of ‘u’ and ‘a’. The program should provide the flexibility to the user to select his own time intervals and repeat the calculations for different values of ‘u’ and ‘a’.
b) Write a C program, which takes two integer operands and one operator form the user, performs the operation and then prints the result. (Consider the operators +,-,*, /, % and use Switch Statement)

Week 5
a) Write a C program to find both the larges and smallest number in a list of integers.
b) Write a C program that uses functions to perform the following:
                i) Addition of Two Matrices
                ii) Multiplication of Two Matrices

Week 6
a) Write a C program that uses functions to perform the following operations:
                i) To insert a sub-string in to given main string from a given position.
                ii) To delete n Characters from a given position in a given string.
b) Write a C program to determine if the given string is a palindrome or not

Week 7
a) Write a C program that displays the position or index in the string S where the string T begins, or – 1 if S doesn’t contain T.
b) Write a C program to count the lines, words and characters in a given text.



Week 8
a) Write a C program to generate Pascal’s triangle.
b) Write a C program to construct a pyramid of numbers.

Week 9
Write a C program to read in two numbers, x and n, and then compute the sum of this geometric progression:
1+x+x2+x3+………….+xn
For example: if n is 3 and x is 5, then the program computes 1+5+25+125.
Print x, n, the sum
Perform error checking. For example, the formula does not make sense for negative exponents – if n is less than 0. Have your program print an error message if n<0, then go back and read in the next pair of numbers of without computing the sum. Are any values of x also illegal ? If so, test for them too.

Week 10
a) 2’s complement of a number is obtained by scanning it from right to left and complementing all the bits after the first appearance of a 1. Thus 2’s complement of 11100 is 00100. Write a C program to find the 2’s complement of a binary number.
b) Write a C program to convert a Roman numeral to its decimal equivalent.

Week 11
Write a C program that uses functions to perform the following operations:
                i) Reading a complex number
                ii) Writing a complex number
                iii) Addition of two complex numbers
                iv) Multiplication of two complex numbers
(Note: represent complex number using a structure.)

Week 12
a) Write a C program which copies one file to another.
b) Write a C program to reverse the first n characters in a file.
(Note: The file name and n are specified on the command line.)

Week 13
Write a C program that uses functions to perform the following operations on singly linked list.:
                i) Creation  ii) Insertion   iii) Deletion   iv) Traversal

Week 14
Write a C program that uses functions to perform the following operations on doubly linked list.:
                i) Creation ii) Insertion   iii) Deletion   iv) Traversal in both ways

Week 15
Write  C programs that implement stack (its operations) using
                i) Arrays   ii) Pointers  

Week 16
Write C programs that implement Queue (its operations) using
                i) Arrays   ii) Pointers  

Week 17
Write a C program that uses Stack operations to perform the following:
                i) Converting infix expression into postfix expression
                ii) Evaluating the postfix expression

Week 18
Write a C program that uses functions to perform the following:
                i) Creating a Binary Tree of integers
                ii) Traversing the above binary tree in preorder, inorder and postorder.

Week 19
Write C programs  that use both recursive and non recursive functions to perform the following searching operations for a Key value in a given list of integers :
                i) Linear search    ii) Binary search

Week 20
Write C programs that implement the following sorting methods to sort a given list of integers in ascending order:
                i) Bubble sort     ii) Quick sort

Week 21
Write C programs that implement the following sorting methods to sort a given list of integers in ascending order:
                i) Insertion sort   ii) Merge sort

Week 22
Write C programs to implement the Lagrange interpolation and Newton- Gregory forward interpolation.

Week 23
Write C programs to implement the linear regression and polynomial regression algorithms.

Week 24
Write C programs to implement  Trapezoidal and Simpson methods.


Text Books
1. C programming and Data Structures, P. Padmanabham, Third Edition, BS Publications
2. Data Structures: A pseudo code approach with C, second edition R.F. Gilberg and B.A. Forouzan
3. Programming in C, P.Dey & M. Ghosh, Oxford Univ.Press.
4. C and Data Structures, E Balaguruswamy, TMH publications.

Write a C program toe find the roots of a quadratic equation.

/* Write a C program toe find the roots of a quadratic equation. */

#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
float a,b,c,root1,root2;
clrscr();
printf("\n Enter values of a,b,c for finding roots of a quadratic (ax2+bx+c)eq:\n");
scanf("%f%f%f",&a,&b,&c);

/*checking condition*/
if(b*b>4*a*c)
{
 root1=(-b+sqrt(b*b-4*a*c))/(2*a);
 root2=(-b-sqrt(b*b-4*a*c))/(2*a);
 printf("\nROOTS ARE\n");
 printf("\n root1=%f\n root2=%f",root1,root2);
}
else
 printf("\n Imaginary Roots.");
 getch();
 return 0;
}
/*
OutPut:

 Enter values of a,b,c for finding roots of a quadratic (ax2+bx+c)eq:
2 -7 5

ROOTS ARE

 root1=2.500000
 root2=1.000000
*

Write a C program to calculate the following Sum: Sum=1-x2/2! +x4/4!-x6/6!+x8/8!-x10/10!

/* Write a C program to calculate the following Sum:
 Sum=1-x2/2! +x4/4!-x6/6!+x8/8!-x10/10!
*/

#include <stdio.h>
#include <math.h>

void main()
{
int counter,f_coun;
float sum=0,x,power,fact;
clrscr();

printf("PROGRAM FOR SUM OF EQ. SERIES");
printf("\n\n\tEQUATION SERIES : 1- X^2/2! + X^4/4! - X^6/6! + X^8/8! - X^10/10!");

printf("\n\tENTER VALUE OF X : ");
scanf("%f",&x);

for(counter=0, power=0; power<=10; counter++,power=power+2)
{
fact=1;
//CALC FACTORIAL OF POWER VALUE
for(f_coun=power; f_coun>=1; f_coun--)
 fact *= f_coun;
//EQ. FOR SUM SERIES
sum=sum+(pow(-1,counter)*(pow(x,power)/fact));
}

printf("SUM : %f",sum);
getch();

}
/*
Output:
PROGRAM FOR SUM OF EQ. SERIES

 EQUATION SERIES : 1- X^2/2! + X^4/4! - X^6/6! + X^8/8! - X^10/10!
 ENTER VALUE OF X : 4
SUM : -0.685785

*/

C Program for Fibonacci Series

/* A Fibonacci Sequence is defined as follows: the first and second terms in the sequence are 0 and 1. Subsequent 
terms are found by adding the preceding two terms in the sequence. Write a C program to generate
the first n terms of the sequence.
*/

#include <stdio.h>

void main()
{
int num1=0, num2=1,no,counter,fab;
clrscr();

printf("PROGRAM TO FIND THE FIBONACCI SERIES UP TO N NO. IN SERIES");
printf("\nENTER LENGTH OF SERIES (N) : ");
scanf("%d",&no);

printf("\nFIBONACCI SERIES");
printf("\t%d  %d",num1,num2);

//LOOP WILL RUN FOR 2 TIME LESS IN SERIES AS THESE WAS PRINTED IN ADVANCE
for(counter = 1; counter <= no-2; counter++)
{
fab=num1 + num2;
printf("  %d",fab);
num1=num2;
num2=fab;
}
getch();
}
/*
Output:
PROGRAM TO FIND THE FIBONACCI SERIES UP TO N NO. IN SERIES
ENTER LENGTH OF SERIES (N) : 5

FIBONACCI SERIES        0  1  1  2  3
*/

String Constants in C language


String Constants:
A string constant is a collection of characters that are enclosed in two double quotes. In generally a string constant is represented as collection of characters constants.
Example: “Mahatma Gandhi”
               “123”  ,  “atoz”


Simple program:
#include<stdio.h>
main( )
{
printf("This is a line of text to output.\n");
printf("And this is another ");
printf("line of text.\n\n");
printf("This is the third line.\n");
}

Operators in C Language


Operators:
An operator is a special character that is used for operations. ‘C’ language provides different types of Operators as follows.
-> Assignment operators
-> Arithmetic operators
->Logical operators
->Relational operators 
-->Increment/Decrement Operators
->Bitwise operators
Assignment operators
            An assignment operator is used to store the value of a constant, variable or an expression in to another variable i.e a variable on the left hand side of an operator.
The general assignment operator is equal to (=).
In addition to equal to we have some other assignment operators (updation operators) as follows: +=, --=, *=, /=, %=,   ..etc.
Eg : x = 5;   à value assigned
            x = y;     à variable based assigned
            x = x+3;     à expression based assigned.

Arithmetic Operators
These operators are used to evaluate arithmetic expressions.
‘C’ language provides five arithmetic operators:
 + (addition)
-          (substraction)
   /  (division)
  % (modulus)
  * (multiplication)


Logical Operators
These operators are used to combine two or more conditions.
“C” language provides 3 logical operations.
Logical AND              &&
Logical  OR                ││
Logical NOT                 !
Relational operators:
            These operators are used to compare two values. They are  ==,<=,>=, !=,<,>.
Eg: a=2;   (2 is stored in a )
A==2   (comparing value of ‘2’ and ‘A’)

Increment/ Decrement Operators:
                                    These variable are used to increment or decrement the value of a variable. Two types of increment and decrement operators available.
They are i) prefix operators
             ii) Postfix Operators
In case of prefix operators assignment is done by after completion of operation.
In case of postfix operators assignment is done before the operation.
 ++ (prefix/postfix increment)
--(prefix/postfix decrement)
Eg: a=40      b=a++
      a=41       b=41     value of a, b is 41.
prefix increment (++a)  
 postfix increment (a++) 
  prefix decrement(- -a)
   postfix decrement (a- -)

Bitwise Logical Operators
These operators are used to work with bits of a number.
The following are the bitwise operators:
& -- Bitwise AND
|    -- Bitwise OR
^  -- Bitwise XOR
~  -- Bitwise NOT
BITWISE SHIFT OPERATORS
Bitwise Left Shift ( << )
Bitwise Right Shift ( >> )
(positive values)
Bitwise Right Shift ( >> )
(negative values)

Comments in C Language


Comments:
            A comment is a single line or more than single line which is used to documentation.
It must start with /*  and end with */
Eg:  /* This is my first program */

Followers