PPS-UNIT-3

SWN | PPS-Practical

Indus University

CSE Section

PPS-Practical

Unit-III:Using control statements

1.Write a program to print 1 to 10 numbers using a while loop.

#include <stdio.h>
#include <conio.h>
void main(){
int i=1;
while(i<=10){
printf("%d\n",i);
i++;
}
getch();
}

1
2
3
4
5
6
7
8
9
10

2.Write a program to read any 7 numbers and print the average value using a for loop.

#include <stdio.h>
#include <conio.h>
void main(){
int i,a;
float sum=0,avg;
for(i=0;i<8;i++){
printf("Enter number:");
scanf("%d",&a);
sum+=a;
}
avg=sum/7;
printf("Average:%f",avg);
getch();
}

Enter number:54
Enter number:23
Enter number:65
Enter number:23
Enter number:63
Enter number:98
Enter number:23
Enter number:54
Average:57.571430

3.Write a program to reverse a given integer number.

#include <stdio.h>
#include <conio.h>
void main(){
int num,rem;
printf("enter an integer:");
scanf("%d",&num);
while (num!=0){
rem = num%10;
num=num/10;
printf("%d",rem);
}
getch();
}

enter an integer:440
044

4.Write a program to print Fibonacci series of given number.

#include <stdio.h>
#include <conio.h>
void main(){
int a=0,b=1,i,sum;
for(i=0;i<9;i++){
printf("%d\t",a);
sum=a+b;
a=b;
b=sum;
}
getch();
}

0
1
1
2
3
5
8
13
21

5.Write a program to find factorial of a number.

#include <stdio.h>
#include <conio.h>
void main(){
int i,f=1,n;
printf("enter num=");
scanf("%d",&n);
for(i=n;i>=1;i--){
f=f*i;
}
printf("factorial=%d",f);
getch();
}

enter num=5
factorial=120

6.Write a program to check whether a number is a Krishnamurthy number or not. Krishnamurthy number is one whose sum of factorial of digits equals the number. Example:145 1! + 4! + 5! = 1 + 24 + 120 = 145

#include <stdio.h>
#include <conio.h>
void main(){
int num,i,j,count=0,f=1,x,sum=0,mult,y,p;
printf("enter number:");
scanf("%d",&num);
x=num;
y=num;
while(x!=0){
x=x/10;
count++;
}
for(i=1;i<=count; i++){
p=num%10;
num=num/10;
for(j=p;j>=1;j--){
f=f*j;
}
printf("factorial of %d is %d\n",p,f);
sum=sum+f;
f=1;
}
printf("sum=%d\n",sum);
if(sum == y){
printf("%d is Krishnamurthy number.",y);
}
else{
printf("%d is not Krishnamurthy number.",y);
}
getch();
}

enter number:145
factorial of 5 is 120
factorial of 4 is 24
factorial of 1 is 1
sum=145
145 is Krishnamurthy number.

7.Write a program to check whether the number is Armstrong or not. Example:153---- 13+ 53+33= 1 + 125 + 27 153

#include <stdio.h>
#include <conio.h>
void main(){
int num,i,count=0,x,sum=0,mult,y,p;
printf("enter number:");
scanf("%d",&num);
x=num;
y=num;
while(x!=0){
x=x/10;
count++;
}
for(i=1;i<=count; i++){
p=num%10;
num=num/10;
mult=p*p*p;
printf("cube of %d is %d\n",p,mult);
sum=sum+mult;
}
printf("sum=%d\n",sum);
if(sum == y){
printf("%d is Armstrong number.",y);
}
else{
printf("%d is not Armstrong number.",y);
}
getch();
}

enter number:153
cube of 3 is 27
cube of 5 is 125
cube of 1 is 1
sum=153
153 is Armstrong number.

8.Write a program to list all prime numbers within given range.

#include <stdio.h>
#include <conio.h>
void main() {
int start, end;
printf("Enter the starting point of the range: ");
scanf("%d", &start);
printf("Enter the ending point of the range: ");
scanf("%d", &end);
printf("Prime numbers between %d and %d are:\n", start, end);
for (int i = start; i <= end; ++i) {
int isPrime = 1;
for (int j = 2; j < i; ++j) {
if (i % j == 0) {
isPrime = 0;
break;
}
}
if (isPrime) {
printf("%d\n", i);
}
}
getch();
}

Enter the starting point of the range: 23
Enter the ending point of the range: 60
Prime numbers between 23 and 60 are:
23
29
31
37
41
43
47
53
59

9.Write a program to draw following patterns:(!!!:don't copy paste whole code.It will gives error.Copy code according to pattern)

*
**
***
****
*****

1
ab
123
abcd
12345

54321
  4321
    321
      21
        1

            A
         AB
      ABC
   ABCD
ABCDE

            1
        121
    12321
1234321

1
01
101
0101

#include <stdio.h>
#include <conio.h>
void main(){



//First pattern
int i,j;
for(i=0;i<5;i++){
for(j=0;j<=i;j++){
printf("*");
}
printf("\n");
}



//Second pattern
int i,j,n=0;
char ch;
for(i=1;i<6;i++){
for(j=1;j<=i;j++){
if(i%2!=0){
n++;
printf("%d",n);
ch='a';
}
else{
printf("%c",ch++);
}
}
n=0;
printf("\n");
}



//Third pattern
int i,j,k;
for(i=5;i>0;i--){
for(k=i;k<5;k++){
printf(" ");
}
for(j=i;j>0;j--){
printf("%d",j);
}
printf("\n");
}



//Fourt pattern
int i,j,k;
char ch;
for(i=0;i<5;i++){
ch='A';
for(k=i;k<4;k++){
printf(" ");
}
for(j=0;j<=i;j++){
printf("%c",ch);
ch++;
}
printf("\n");
}



//Fifth pattern
int i,j,k,r;
for(i=1;i<5;i++){
for(k=i;k<4;k++){
printf("  ");
}
for(j=1;j<=i;j++){
printf("%d",j);
}
for(r=i-1;r>=1;r--){
printf("%d",r);
}
printf("\n");
}



//Sixth pattern
int i,j;
for(i=1;i<5;i++){
for(j=i-1;j>=0;j--){
if(j%2==0){
printf("1");
}
else{
printf("0");
}
}
printf("\n");
}



getch();
}

Enter the Basic:7000
total salary is= 12200.000000