×
FOCUS ON THE DESTINATION NOT THE DIFFICULTIES

# Lab Manuals for Computer Programming - CP By JNTU Heroes

• Computer Programming - CP
• Practical
• Jawaharlal Nehru Technological University Anantapur (JNTU) College of Engineering (CEP), Pulivendula, Pulivendula, Andhra Pradesh, India - JNTUACEP
• 401 Views
Jntu Heroes
0 User(s)

#### Text from page-2

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

#### Text from page-3

{ 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

#### Text from page-4

{ 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

#### Text from page-5

6. What are the two commands to control the loop ? 7. In which loop the test condition is tested at the end of the loop ? 1.8 LAB ASSIGNMENT: 1. Find the sum of the individual digits of a number ? 2. Check whether the given number is an Armstrong number or not ? 3. Display the reverse of a given number. 4. Display the series of numbers 1, 3, 5, 7, 9……………….. 5. Display all prime numbers between 1 to N. 6. Generate all combinations of 1, 2, 3 using for loop ? 7. Display the following series using for loop 1 + 2/2! + 3/3! +4/4! ……………………………………….. 8. Display all the ASCII values and their equivalent characters using while loop 9. Display the perfect numbers between 1 to N. 10. Display the friendly numbers between 1 to N. 1.9 POST LAB VIVA QUESTIONS: 1. Can we initialize more than one variable in for loop ? 2. Give one example of for loop without increment/decrement operation ? 3. Is the following code for(;;) represents an infinite loop ? 4. What is the order of movement of control in for loop ? for( expression1; expression2; expression3) { Block of statements; } 5. Can we write more than one test condition in for loop ? 6. What is the output of the following code ? #include<stdio.h> void main() { int i=1; for(;i++;) printf("%d",i); } Page 5