0% found this document useful (0 votes)
6 views3 pages

PSPC Lab-2

The document contains five programming exercises in C that demonstrate the use of iterative constructs such as for loops and while loops. The exercises include printing factors of a number, calculating the factorial, checking for palindrome numbers, determining if a number is prime, and generating the Fibonacci series. Each exercise is accompanied by source code that illustrates the implementation of the respective functionality.

Uploaded by

srp.ch12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

PSPC Lab-2

The document contains five programming exercises in C that demonstrate the use of iterative constructs such as for loops and while loops. The exercises include printing factors of a number, calculating the factorial, checking for palindrome numbers, determining if a number is prime, and generating the Fibonacci series. Each exercise is accompanied by source code that illustrates the implementation of the respective functionality.

Uploaded by

srp.ch12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

LAB-2

Iterative Constructs - I: For Loop, While Loop &Do. While

1. Write a program print all the factors of a given number


Source Code:-

#include<stdio.h>
int main()
{
int i,num;
printf("Enter the number : \n");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
if(num%i==0)
{
printf("%d ",i);
}
}
printf("are the factors of %d . \n",num);
return 0;
}

2. Write a program to find the factorial of a given number


Source Code:-

#include<stdio.h>
int main()
{
int i,num,f=1;
printf("Enter the number : \n");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
f=f*i;
}
printf("The factorial of %d is %d . \n",num,f);
return 0;
}

3. Write a program to find whether a given number is Palindrome or not.


Source Code:-

#include<stdio.h>
int main()
{
int n,rev=0,rem,temp;
printf("Enter the number : \n");
scanf("%d",&n);
temp=n;
while(temp>0)
{
rem=temp%10;
rev=rev*10+rem;
temp=temp/10;
}
if(rev==n)
{
printf("%d is a Palindrome number . \n",n);
}
else
{
printf("%d is not a Palindrome numnber . \n",n);
}
return 0;
}

4. Write a program to find whether a given number is Prime or not.


Source Code:-

#include<stdio.h>
int main()
{
int n,i,c=0;
printf("Enter the number : \n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
{
c=c+1;
}
}
if(c==2)
{
printf("%d is a Prime number . \n",n);
}
else
{
printf("%d is not a Prime number . \n",n);
}
return 0;
}

5. Write a program to print the Fibonacci series upto given ‘n’ number of terms.
Source Code:-

#include<stdio.h>
int main()
{
int i,n,a=0,b=1,s=0;
printf("Enter the number : \n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("%d ",s);
a=b;
b=s;
s=a+b;
}
printf("\n");
return 0;
}

You might also like