0% found this document useful (0 votes)
3 views5 pages

Programming Logic - Execute All

Uploaded by

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

Programming Logic - Execute All

Uploaded by

rithekasudhakar3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

C sample programs

1. Write a C program that prints the first n numbers of the Fibonacci series.

Input Format:
 A single line contains an integer representing the value of n.

Output Format:
 The first N numbers of the Fibonacci series are separated by a space.
 Print a space after every number, including the last number.
#include<stdio.h>
void main()
{
int a=-1,b=1,c=0,i,n;
scanf("%d",&n);
for(i=0;i<n;i++){
c=a+b;
printf("%d ",c);
a=b;
b=c;
}}

2. Write a program to print the following pattern.

Input 4 6

******

* *

* *

******

#include <stdio.h>
// Function to print the pattern
void print_pattern(int rows, int stars) {
for (int i = 0; i < rows; i++) {
if (i == 0 || i == rows - 1) {
for (int j = 0; j < stars; j++) {
printf("*");
}
printf("\n");
} else {
printf("*");
for (int j = 0; j < stars - 2; j++) {
printf(" ");
}
if (stars > 1) {
printf("*");
}
printf("\n");
}
}
C sample programs

int main() {
int rows, stars;

// Read input
scanf("%d", &rows);
scanf("%d", &stars);

// Print pattern
print_pattern(rows, stars);

return 0;
}

3. Sum of digits of a number


#include <stdio.h>
void main() {
int n, sum = 0;
printf("Enter a number: ");
scanf("%d", &n);
while( _______ ) {
sum = sum + ( _______ );
n = _______;
}
printf("Sum of digits = %d", sum);
}

4. Count digits in a number

#include <stdio.h>
void main() {
int n, count = 0;
printf("Enter a number: ");
scanf("%d", &n);
while( _______ ) {
count++;
n = _______;
}
printf("Total digits = %d", count);
}

5. Find factorial of a number


#include <stdio.h>
void main() {
int n, fact = 1;
printf("Enter a number: ");
scanf("%d", &n);
while( _______ ) {
fact = fact * n;
_______;
C sample programs

}
printf("Factorial = %d", fact);
}

6. Print digits of a number

#include <stdio.h>
void main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
while( _______ ) { // Repeat until n becomes 0
printf("%d ", _______ ); // Print last digit
n = _______; // Remove last digit
}
}
7. Print numbers from 1 to n
#include <stdio.h>
void main() {
int i = 1, n;
printf("Enter n value: ");
scanf("%d", &n);
while( _______ ) { // Condition to repeat till n
printf("%d ", i);
_______; // Increment i
}
}

8. Reverse print numbers from n to 1


#include <stdio.h>
void main() {
int i, n;
printf("Enter n value: ");
scanf("%d", &n);
for( _______; _______; _______ ) { // Decrease i each time
printf("%d ", i);
}
}

9. Print multiplication table


#include <stdio.h>
void main() {
int n, i;
printf("Enter a number: ");
scanf("%d", &n);
for( _______; _______; _______ ) {
printf("%d x %d = %d\n", n, i, n * i);
}
}
C sample programs

10. Print sum of first n numbers

#include <stdio.h>
void main() {
int i, n, sum = 0;
printf("Enter n value: ");
scanf("%d", &n);
for( _______; _______; _______ ) { // Loop till n
sum = sum + i;
}
printf("Sum = %d", sum);
}

11. Print even numbers from 1 to n

#include <stdio.h>
void main() {
int i, n;
printf("Enter n value: ");
scanf("%d", &n);
for( _______; _______; _______ ) { // Complete to print even numbers
if( _______ )
printf("%d ", i);
}
}

12. Check whether number is prime


#include <stdio.h>
void main() {
int n, i, count = 0;
printf("Enter a number: ");
scanf("%d", &n);
for( _______; _______; _______ ) {
if( _______ )
count++;
}
if(count == 2)
printf("Prime");
else
printf("Not Prime");
}

13. Display all divisors of a number

#include <stdio.h>
void main() {
int n, i;
printf("Enter a number: ");
scanf("%d", &n);
for( _______; _______; _______ ) {
C sample programs

if( _______ )
printf("%d ", i);
}
}

14. Check Palindrome

#include <stdio.h>
void main() {
int n, rev = 0, rem, temp;
printf("Enter a number: ");
scanf("%d", &n);
temp = n;
while( _______ ) {
rem = n % 10;
rev = rev * 10 + rem;
n = _______;
}
if( _______ )
printf("Palindrome");
else
printf("Not Palindrome");
}

15. Check Armstrong number


#include <stdio.h>
void main() {
int n, temp, rem, fact, sum = 0, i;
printf("Enter a number: ");
scanf("%d", &n);
temp = n;
while( _______ ) {
rem = n % 10;
fact = 1;
for( _______; _______; _______ ) {
fact = fact * i;
}
sum = sum + fact;
n = _______;
}
if( _______ )
printf("Strong Number");
else
printf("Not Strong");
}

You might also like