×
ALL THE HUSTLE, LATE NIGHTS, MISSED PARTIES, EARLY MORNINGS WILL BE WORTH IT.
--Your friends at LectureNotes
Close

Computer Programming

by Jntu HeroesJntu Heroes
Type: PracticalInstitute: Jawaharlal nehru technological university anantapur college of engineering Offline Downloads: 5Views: 315Uploaded: 11 months ago

Share it with your friends

Suggested Materials

Leave your Comments

Contributors

Jntu Heroes
Jntu Heroes
COMPUTER PROGRAMMING LABORATORY MANUAL AND WORK BOOK (USING C PROGRAMMING) 2014 - 2015 Page 1
WEEK- 1 1.1 OBJECTIVE: 1. To find the sum of individual digits of a given number N. 2. Generate the Fibonacci series up to N. 3. To Generate the Prime numbers up to N . 1.2 RESOURCES: centOS 1.3 PROGRAM LOGIC: Sum of Individual digits of a given number 1. Read a number N 2. Initialize the Sum=0 3. if n > 0 then calculate reminder R= N %10 4. Add reminder R to Sum 5. Store N/10 value to N 6. If N=0 then Display Sum Fibonacci Series 1. Initialize first two number of Fibonacci series A=0, B=1 2. Read N value for displaying the Fibonacci series up to Nth term 3. If N= 1 or N=2 display the value of A and B 4. If N > 2, calculate the next term by taking a temporary variable T = A + B 5. Display the term T 6. Swap the contents of A and B using a third variable T Generate Prime Number 1. Read the prime number range N 2. Initialize flag =0 3. Take two numbers i and j, if i % j =0 then make flag=1 4. If flag = 0 then display the number i as prime number 1.4 PROCEDURE: 1. Create : Open editor vi x.c write a program after that press ESC and: wq for save and Quit. 2. Compile: gcc x.c. 3. Execute: . / a.out. 1.5 SOURCE CODE: Sum of Individual digits of a given number #include<stdio.h> void main() { int num, k=1, sum=0; printf(“Enter the number whose digits are to be added:”); scanf(“%d”, & num); while(num!=0) Page 2
{ k=num%10; sum=sum+k; k=num/10; num=k; } printf(“Sum of the digits:%d”,sum); getch(); } Fibonacci Series #include<stdio.h> void main() { int a,b,c,n,i; printf("enter n value"); scanf("%d",&n); a=0; b=1; if(n==1) printf("%d",a); elseif(n==2) printf("%d%d",a,b); else { printf("%d%d",a,b); for(i=2;i<n;i++) { c=a+b; printf("%d",c); a=b; b=c; } } } Generate Prime Number #include<stdio.h> void main() { int n,i,fact,j; printf("Enter any number:"); scanf("%d",&n); for(i=1;i<=n;i++) Page 3
{ fact=0; for(j=1;j<=i;j++) { if(i%j==0) fact++; } if(fact==2) printf("\n %d",i); } } 1.6 INPUT AND OUTPUT: Sum of Individual digits of a given number Enter the value for n: 333 Sum of individual digits is 9 Enter the value for n: 4733 Sum of individual digits is 17 Enter the value for n: -111 The given number is not valid Fibonacci Series Enter n value: 5 0 1 1 2 3 2. Enter n value: 7 0 1 1 2 3 5 8 3. Enter n value: -6 0 1 Generate Prime Number Enter the number: 5 2 3 5 Enter the number: 10 2 3 5 7 Enter the number: 12 2 3 5 7 1.7 PRE LAB VIVA QUESTIONS: 1. What are the two types of flow control used ? 2. What do you mean by looping ? 3. What is the difference between while and do while loop ? 4. Which loop is known as entry controlled loop ? 5. Which loop is known as exit control loop ? Page 4

Lecture Notes