0% found this document useful (0 votes)
25 views

Lab6 2023103623

The document contains 5 C programming problems and their solutions: 1) A program to print a pyramid pattern of stars for a given number of rows 2) A program to repeatedly sum the digits of a number until it is a single digit 3) A program to read 5 numbers and print them in ascending and descending order 4) A program to print the Fibonacci series up to a specified number of terms 5) A program to print all prime numbers from 1 to a given number entered by the user

Uploaded by

Mohamed Jasim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Lab6 2023103623

The document contains 5 C programming problems and their solutions: 1) A program to print a pyramid pattern of stars for a given number of rows 2) A program to repeatedly sum the digits of a number until it is a single digit 3) A program to read 5 numbers and print them in ascending and descending order 4) A program to print the Fibonacci series up to a specified number of terms 5) A program to print all prime numbers from 1 to a given number entered by the user

Uploaded by

Mohamed Jasim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment – 6

2023103623
1. Write a C program to print the following Pyramid pattern for any given number of rows.

Code -

#include <stdio.h>

int main()

int i, space, rows, k = 0;

printf("Enter the number of rows: ");

scanf("%d", &rows);

for (i = 1; i <= rows; ++i, k = 0)

for (space = 1; space <= rows - i; ++space)

printf(" ");

while (k != 2 * i - 1) {

printf("* ");

++k;

printf("\n");

return 0;

}
Output:

2. Write a program to read a number and find the sum of its individual digits repeatedly till the result
is a single digit.
CODE :-
#include<stdio.h>
int main()
{
long int num;
int sum = 0, d;

printf("Enter a number: ");


scanf("%ld", &num);

while(num / 10 != 0)
{
sum = 0;
while(num != 0)
{
d = num % 10;
sum += d;
num = num / 10;
}

num = sum;
}

printf("sum of digits is: %d", sum);

return 0;
}

3. Read 5 numbers from user and print them in ascending and descending order.
CODE-
#include<stdio.h>
int main()
{
int a[5],temp;
printf("enter 5 numbers:\n");
for(int i=0;i<5;i++)
scanf("%d",&a[i]);
for(int i=0;i<5;i++)
{
for(int j=i+1;j<5;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("the ascending order of elements:");
for(int i=0;i<5;i++)
printf("%d\t",a[i]);
printf("\n the descending order of elements:");
for(int i=4;i>=0;i--)
printf("%d\t",a[i]);
return 0;
}
Output:-

4. Write a C program to print Fibonacci series up to a specified number of terms.


CODE-
#include<stdio.h>

int main()
{
int t1=0,t2=1,i,n,sum=0;
printf("Enter the Number of Terms: ");
scanf("%d",&n);
printf("%d %d ",t1,t2 );
for(i=1;i<n-1;i++)
{
sum=t1+t2;
printf(" %d ",sum);
t1=t2;
t2=sum;

}
return 0;
}
5. Write a C program to print all prime numbers from 1 to ‘n’. Read ‘n’ from user.\\
CODE-
#include<stdio.h>

int main(){

int num,i,count,n;
printf("Enter max range: ");
scanf("%d",&n);

for(num = 1;num<=n;num++){

count = 0;

for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}

if(count==0 && num!= 1)


printf("%d ",num);
}

return 0;
}

You might also like