3 C Lab Part

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

Programming in C Laboratory Department of Computer Science and Engineering, SMVEC

Develop programs using decision-making and looping


Ex No 3.
constructs.

3 a) Write a C program to generate first n terms of the Fibonacci sequence.

AIM:
To generate the first n terms of the Fibonacci sequence.

ALGORITHM:
Step 1: Start
Step 2: Read n
Step 3: Initialize f0 ← 0, f1 ← 1, f ← 0
Step 4:i=0
Step 5:
while(i<=n) do as follows
printf("%d\t",f0);
f=f0+f1;
f0=f1;
f1=f;
i=i+1;
If not goto step 7
Step 6: Stop
FLOW CHART:
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC

SOURCE CODE:
# inc lu d e < s t d io . h>
# inc lu d e < c o n io . h>
v o id ma in ( )
{
in t f0 , f1 , f, n , i;
c lrs c r () ;
p r in t f ( " E n t e r T h e V a lu e Fo r n \ n " ) ;
s c a n f( " %d ", & n );
f0 = 0 ;
f1 = 1 ;
p r in t f( " FI B O N A C C I SE Q U EN C E FO R THE FIR S T %d
T ER M S : \ n " , n );
i= 0 ;
w h ile ( i< n )
{
p r in t f( " % d \ t ", f0 );
f= f0 + f1 ;
f0 = f1 ;
f1 = f;
i= i+ 1 ;
}
}
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC

SCREENSHOT:

RESULT:
Thus the given C program to generate the first n terms of the Fibonacci
sequence has been executed and verified successfully.
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC

PRACTICE EXERCISE:
3 b)Write a C program to print multiplication table of 6.
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC

Particulars Max Marks Mark secured


Program and Execution 15
Viva 10
Total 25
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC

Result

3 c) Write a C program to find factorial of a given number.

AIM
To find the factorial of a given number.

ALGORITHM
Step 1: Start
Step 2: Read n
Step 3: Initialize factorial f ← 1
Step 4:
for(c = 1; c <= n; c++) do
f = f * c;
Step 5: print f
Step 6: Stop

FLOW CHART:
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC

SOURCE CODE:

#include <stdio.h>
int main()
{
int c, n, f = 1;

printf("Enter a number to calculate its factorial\n");


scanf("%d", &n);
for (c = 1; c <= n; c++)
{
f = f * c;
}
printf("Factorial of %d = %d\n", n, f);
return 0;
}
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC

SCREENSHOT:
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC

RESULT
Thus the program to find the factorial is executed and verified successfully.

PRACTICE EXERCISE:
3 d)Write a C program to find Sum of the Series of a given number.
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC

Particulars Max Marks Mark secured


Program and Execution 15
Viva 10
Total 25

Result
3 e) Write a C program to check whether the given number is Armstrong
Number or Not.

AIM:
To Check whether given number is Armstrong Number or Not.

DESCRIPTION:
Armstrong number
Eg: 153
Summation =13+53+33=153

ALGORITHM:

Step 1: Start
Step 2: Read n
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC

Step 3: Assign num ←0, n1=n


Step 4: while(n!=0) Begin
rem←n%10
num= num+(rem*rem*rem)
n1=n1/10
repeat step 4 until condition fail
if num==n then print n as armstrong number otherwise print not armstrong
Step 5: Stop

FLOWCHART:
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC

SOURCE CODE:
#include <stdio.h>
int main()
{
int n, n1, rem, num=0;
printf("Enter a positive integer: ");
scanf("%d", &n);
n1=n;
while(n1!=0)
{
rem=n1%10;
num+=rem*rem*rem;
n1/=10;
}
if(num==n)
printf("%d is an Armstrong number.",n);
else
printf("%d is not an Armstrong number.",n);
}
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC

SCREENSHOT:

RESULT:
Thus the program to find the armstrong no is executed and verified successfully.
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC

PRACTICE EXERCISE:
3 f) Write a simple C program to generate all prime numbers between 1 and n.
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC

Particulars Max Marks Mark secured


Program and Execution 15
Viva 10
Total 25
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC

Result

3 g) Write a simple C program to check whether a given character is vowel or


not using Switch – Case statement.

AIM :
To write a C program to check whether a given character is vowel or not using
Switch – Case statement.

ALGORITHM:
Step 1: Start
Step 2: Declare the character variable ch
Step 3: Read the character
Step 4: i) Using switch case check the character which is suitable
for the given value
ii) if no cases matched, then print the default statement.
Step 5: Stop
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC

FLOWCHART:
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC

SOURCE CODE:
# inc lu d e < s t d io . h>
# inc lu d e < c o n io . h>
v o id ma in ( )
{
c ha r c h ;
c lrs c r () ;
p r in t f( " e n te r a n y c h a ra c te r : \ n " ) ;
c h= ge tc ha r () ;
s w itc h ( c h )
{
c a s e 'a ': c a s e ' A ' :
c a s e 'e ': c a s e 'E ':
c a s e 'i' : c a s e 'I ':
c a s e 'o ': c a s e 'O ':
c a s e 'u ' : c a s e 'U ':
p r in t f( " \ n \ n It is a V o w e l" );
b re a k ;
d e fa u lt :
p r in t f( " \ n \ n It is no t a V o w e l" ) ;
}
g e tc h ( );
}
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC

SCREENSHOT:
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC

RESULT:

Thus the given C program to check whether a given character is vowel or not
using Switch – Case statement has been executed and verified successfully.

PRACTICE EXERCISE:

3 h) Write a C program to perform arithmetic operations using switch statement.


Programming in C Laboratory Department of Computer Science and Engineering, SMVEC

Particulars Max Marks Mark secured


Program and Execution 15
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC

Viva 10
Total 25

Result
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC

3 i) Write a simple C program to find the sum of ‘n’ numbers using for, do –
while statements.
AIM :
To write a C program to find the sum of ‘n’ numbers using for, do – while
statements.

ALGORITHM:
Step 1: Start
Step 2: Declare the variable n, count, sum=0, op
Step 3: get the value of n and op
Step 4: Using switch case select do while loop (or) for loop
Step 5: Within for loop, calculate sum+=count, until count<n
i) Display the sum value
Step 6: Within do while loop, calculate sum value until count<n
i) Display the sum value
Step 7: In default, display the invalid option message.
Step 8: Stop execution.
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC

FLOWCHART:
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC

SOURCE CODE:
# inc lu d e < s t d io . h >
# inc lu d e < c o n io . h >
v o id ma in ( )
{
in t n, c o u n t, s u m= 0 , o p ;
c lrs c r () ;
p r in t f( " E n t e r a n in te g e r : ") ;
s c a n f( " %d ", & n );
p r in t f( " \ n P re s s 1 -d o w h ile lo o p , 2 - fo r lo o p : " );
s c a n f( " %d ", &o p ) ;
s w itc h ( o p )
{
case 1:
c o u n t= 1 ;
do
{
s u m+ = c o u n t ;
++count;
}
w h ile ( c o u n t< = n );
p r in t f( " \ n \ n S u m = %d ", s u m) ;
b re a k ;
case 2:
fo r( c o u n t= 1 ;c o u n t< = n ;+ + c o u n t )
{
s u m+ = c o u n t ;
}
p r in t f( " \ n \ n S u m = %d ", s u m) ;
b re a k ;
d e fa u lt :
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC

p r in t f( " \ n \ n I n v a lid O p t io n " ) ;


}
g e tc h ( );
}

SCREENSHOT:

RESULT:
Thus the program to find the sum of ‘n’ numbers using for, do – while statements
has been executed and verified successfully.
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC

PRACTICE EXERCISE:
3 j)Write a C program to display the patterns in the following format.

*
**
***
****
*****
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC

Particulars Max Marks Mark secured


Program and Execution 15
Viva 10
Total 25
Programming in C Laboratory Department of Computer Science and Engineering, SMVEC

Result

You might also like