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

Sample C Program

The document contains 10 C programs that demonstrate different programming concepts like data types, functions, loops, conditional statements, recursion, and patterns like half pyramids and inverted pyramids made of characters. Each program is accompanied by sample output. The programs cover basic concepts like checking data types, vowels, prime numbers as well as more advanced concepts like calculating GCD, reversing sentences recursively, and printing various pyramid patterns.

Uploaded by

ritesh patel
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Sample C Program

The document contains 10 C programs that demonstrate different programming concepts like data types, functions, loops, conditional statements, recursion, and patterns like half pyramids and inverted pyramids made of characters. Each program is accompanied by sample output. The programs cover basic concepts like checking data types, vowels, prime numbers as well as more advanced concepts like calculating GCD, reversing sentences recursively, and printing various pyramid patterns.

Uploaded by

ritesh patel
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

SUBJECT: BCP CODE: 4310702

SAMPLE IMPORTANT PROGRAM

Program: 1 Program Using the long keyword

#include <stdio.h>
int main() {
int a;
long b; // equivalent to long int b;
long long c; // equivalent to long long int c;
double e;
long double f;

printf("Size of int = %zu bytes \n", sizeof(a));


printf("Size of long int = %zu bytes\n", sizeof(b));
printf("Size of long long int = %zu bytes\n", sizeof(c));
printf("Size of double = %zu bytes\n", sizeof(e));
printf("Size of long double = %zu bytes\n", sizeof(f));

return 0;
}
Output
Size of int = 4 bytes
Size of long int = 8 bytes
Size of long long int = 8 bytes
Size of double = 8 bytes
Size of long double = 16 bytes
Program 2: Program to Check Vowel or consonant
#include <stdio.h>
int main() {
char c;
int lowercase_vowel, uppercase_vowel;
printf("Enter an alphabet: ");
scanf("%c", &c);

// evaluates to 1 if variable c is a lowercase vowel


lowercase_vowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');

// evaluates to 1 if variable c is a uppercase vowel


uppercase_vowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');

// evaluates to 1 (true) if c is a vowel


if (lowercase_vowel || uppercase_vowel)
printf("%c is a vowel.", c);
else
printf("%c is a consonant.", c);
return 0;
}
Output
Enter an alphabet: G
G is a consonant.
Program 3: Program to Check Alphabet

#include <stdio.h>
int main() {
char c;
printf("Enter a character: ");
scanf("%c", &c);
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
printf("%c is an alphabet.", c);
else
printf("%c is not an alphabet.", c);
return 0;
}
Output :
Enter a character: *
* is not an alphabet

Program 4: GCD Using for loop and if Statement

#include <stdio.h>
int main()
{
int n1, n2, i, gcd;

printf("Enter two integers: ");


scanf("%d %d", &n1, &n2);

for(i=1; i <= n1 && i <= n2; ++i)


{
// Checks if i is factor of both integers
if(n1%i==0 && n2%i==0)
gcd = i;
}

printf("G.C.D of %d and %d is %d", n1, n2, gcd);


Output

Enter two positive integers: 81


153
GCD = 9

Program :5 Program to Check Prime Number


#include <stdio.h>

int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
// 0 and 1 are not prime numbers
// change flag to 1 for non-prime number
if (n == 0 || n == 1)
flag = 1;
for (i = 2; i <= n / 2; ++i) {
// if n is divisible by i, then n is not prime
// change flag to 1 for non-prime number
if (n % i == 0) {
flag = 1;
break;
}
}
// flag is 0 for prime numbers
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);

return 0;
}
Output:
Enter a positive integer: 29
29 is a prime number.

Program 6 : // convert binary to decimal


#include <stdio.h>
#include <math.h>
// function prototype
int convert(long long);

int main() {
long long n;
printf("Enter a binary number: ");
scanf("%lld", &n);
printf("%lld in binary = %d in decimal", n, convert(n));
return 0;
}
// function definition
int convert(long long n) {
int dec = 0, i = 0, rem;
while (n != 0) {
// get remainder of n divided by 10
rem = n % 10;
// divide n by 10
n /= 10;
// multiply rem by (2 ^ i)
// add the product to dec
dec += rem * pow(2, i);
// increment i
++i;
}
return dec;
}
Output
Enter a binary number: 1101
1101 in binary = 13 in decimal
Program 7: Reverse a sentence using recursion
#include <stdio.h>
void reverseSentence();
int main() {
printf("Enter a sentence: ");
reverseSentence();
return 0;
}
void reverseSentence() {
char c;
scanf("%c", &c);
if (c != '\n') {
reverseSentence();
printf("%c", c);
}
}
Output
Enter a sentence: margorp emosewa
awesome program

Program 8 : Example 2: Half Pyramid of Numbers

1
12
123
1234
12345
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}
Program 9: Example 4: Inverted half pyramid of *

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

C Program

#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = rows; i >= 1; --i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}
return 0;
}
Program 10 : Example 8: Inverted full pyramid of *

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

C Program

#include <stdio.h>
int main() {
int rows, i, j, space;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = rows; i >= 1; --i) {
for (space = 0; space < rows - i; ++space)
printf(" ");
for (j = i; j <= 2 * i - 1; ++j)
printf("* ");
for (j = 0; j < i - 1; ++j)
printf("* ");
printf("\n");
}
return 0;
}

You might also like