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

20 Programs

The document contains a collection of 20 C programming examples demonstrating various programming concepts such as loops, conditionals, and basic input/output operations. Each example illustrates a specific functionality, including counting even and odd numbers, calculating sums and averages, finding the largest and smallest numbers, and generating multiplication tables. The code snippets are structured using for, while, and do-while loops to showcase different approaches to problem-solving in programming.
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)
7 views

20 Programs

The document contains a collection of 20 C programming examples demonstrating various programming concepts such as loops, conditionals, and basic input/output operations. Each example illustrates a specific functionality, including counting even and odd numbers, calculating sums and averages, finding the largest and smallest numbers, and generating multiplication tables. The code snippets are structured using for, while, and do-while loops to showcase different approaches to problem-solving in programming.
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
You are on page 1/ 24

20 PROGRAMS

IN
COMPUTER
PROGRAMMING 2

PASS BY:
PRECIOUS DOMINICA M. BISNAN

PASS TO:
ALFIE RAPISURA

1.
#include <stdio.h>

int main() {
int num;
char name[50];
printf("For looping \n");
printf("Enter your name: ");
scanf("%s", &name);
printf("Enter a number: ");
scanf("%d", &num);

for(int x = 0; x < num; x++){


printf("%s \n", &name);
}
printf("\n");
printf("For while loop \n");
int j=0;
while(j < num){
printf("%s \n", &name);
j++;
}
printf("\n");
int y=0;
printf("For do while \n");
do{
printf("%s \n", &name);
y++;
}while(y < num);
return 0;
}
2.
#include <stdio.h>
int main() {
int inputnum;
int evencount = 0;
int oddcount = 0;
int num;
printf("How many numbers do you want to enter?: ");
scanf("%d", &inputnum);
for (int i = 0; i < inputnum; i++) {
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0) {
evencount++;
} else {
oddcount++;
}
}

printf("Even numbers: %d\n", evencount);


printf("Odd numbers: %d\n", oddcount);
return 0;
}

3.

#include <stdio.h>

int main() {
int sum=0;
int n;

printf("For looping\n");
sum=0;

for (int i=0; i<100; i++) {


printf("Enter a number: ");
scanf("%d", &n);
if (n < 0) {
break;
}
sum += n;
}
printf("The sum is %d\n", sum);

printf("For while loop \n");


sum = 0;
while (1) {
printf("Enter a number: ");
scanf("%d", &n);
if(n < 0) {
break;
}
sum += n;
}
printf("The sum is: %d\n", sum);
printf("For do while loop\n");
sum = 0;
do {
printf("Enter a number: ");
scanf("%d", &n);
sum += n;
} while(n >= 0);

printf("The sum is: %d\n", sum);

return 0;
}
4.
#include <stdio.h>

int main() {
int n;
printf("Using for loop\n");
printf("Enter a number: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
if (i % 3 == 0 && i % 5 == 0) {
printf("%d ", i);
}
}
printf("\n");
printf("Using while loop\n");
int i = 1;
while (i <= n) {
if (i % 3 == 0 && i % 5 == 0) {
printf("%d ", i);
}
i++;
}
printf("\n");
printf("Using do-while loop\n");
i = 1;
do {
if (i % 3 == 0 && i % 5 == 0) {
printf("%d ", i);
}
i++;
} while (i <= n);
printf("\n");

return 0;
}
5.
#include <stdio.h>

int main() {
int num;
float average=0;
int grades;
int sum=0;
printf("For looping\n");
printf("Enter a number you want to compute: ");
scanf("%d", &num);

for(int i = 1; i <= num; i++) {


printf("Enter your grades: " );
scanf("%d", &grades);
if(grades < 0 || grades > 100){
printf("Invalid grade entered. \n");
}else{

sum+=grades;
}
}
average=sum/num;
printf("The average is %f \n", average);
printf("\n");

printf("For while loop \n");

int numx;
float averagex=0;
int gradesx;
int sumx=0;
int j = 1;

printf("Enter a number you want to compute: ");


scanf("%d", &numx);

while(j <= numx){


printf("Enter your grades: ");
scanf("%d", &gradesx);
if(grades < 0 || grades > 100){
printf("Invalid grade entered.");
}else{
sumx+=gradesx;
}
j++;

}
averagex=sumx/numx;
printf("The average is %f \n", averagex);
printf("\n");

printf("For do while loop \n");

int numb;
float averageb=0;
int gradesb;
int sumb=0;
int x=1;

printf("Enter a number you want to compute: ");


scanf("%d", &numb);

do{
printf("Enter your grades: ");
scanf("%d", &gradesb);
if(gradesb < 0 || gradesb > 100){
printf("Invalid grade entered.");
}else{
sumb+=gradesb;
}
x++;
}while(x <= numb);

averageb=sumb/numb;
printf("The average is %f \n", averageb);

return 0;
}
6.
#include <stdio.h>
#include <ctype.h>
int main() {
char string[100];
int vowels = 0;
int consonants = 0;

printf("Enter a word: ");


scanf("%s", &string);

printf("Using for loop\n");


for (int i = 0; string[i] != '\0'; i++) {
char ch = string[i];

if (ch >= 'A' && ch <= 'Z') {


ch = ch + ('a' - 'A');
}

if (ch >= 'a' && ch <= 'z') {


if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
vowels++;
} else {
consonants++;
}
}
}
printf("Vowels: %d\n", vowels);
printf("Consonants: %d\n", consonants);

printf("\n");
int vowels_w = 0;
int consonants_w = 0;
int j = 0;
while (string[j] != '\0') {
char ch = string[j];
ch = tolower(ch);

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {


vowels_w++;
} else if (isalpha(ch)) {
consonants_w++;
}
j++;
}
printf("Vowels: %d\n", vowels_w);
printf("Consonants : %d\n", consonants_w);
printf("\n");

printf("Using do-while loop\n");


int vowels_dw = 0;
int consonants_dw = 0;
int k = 0;
do {
char ch = string[k];
ch = tolower(ch);

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {


vowels_dw++;
} else if (isalpha(ch)) {
consonants_dw++;
}
k++;
} while (string[k] != '\0');
printf("Vowels: %d\n", vowels_dw);
printf("Consonants: %d\n", consonants_dw);

return 0;
}

7.
#include <stdio.h>

int main() {
printf("For looping \n");
for(int i=1; i<=5; i++){
printf("%d \n", i);
}
printf("\n");

printf("For while loop \n");


int j=1;
while (j <= 5){
printf("%d \n", j);
j++;
}
printf("\n");
printf("For do while \n");

int x=1;
do{

printf("%d \n", x);


x++;
}while(x <= 5);
return 0;
}

8.
#include <stdio.h>
int main() {
char password[] = "SECRET";
char guess[100];
int attempts = 3;
while (attempts > 0) {
printf("Enter the password: ");
scanf("%99s", guess);
int match = 1;
for (int i = 0; password[i] != '\0' || guess[i] != '\0'; i++) {
if (password[i] != guess[i]) {
match = 0;
break;
}
}
if (match) {
printf("Correct!\n");
return 0;
} else {
attempts--;
if (attempts > 0) {
printf("Incorrect. Try again.\n");
} else {
printf("You ran out of tries.\n");
}
}
}

return 0;
}

9.
#include <stdio.h>
int main() {
int n;
int num;
int largest=0;
printf("For loop \n");

printf("How many numbers will you enter?: ");


scanf("%d", &n);

for (int i = 0; i < n; i++) {


printf("Enter the numbers: ");
scanf("%d", &num);

if (num > largest) {


largest = num;
}
}
printf("The largest number is %d.\n", largest);
printf("\n");
int y = 0;
printf("For while loop \n");
while (y < n) {
printf("Enter the numbers: ");
scanf("%d", &num);
if (num > largest) {
largest = num;
}
y++;
}
printf("The largest number is %d.\n", largest);

printf("\n");

int x = 0;
printf("For do while loop \n");

do {
printf("Enter the numbers: ");
scanf("%d", &num);
if (num > largest) {
largest = num;
}
x++;
} while (x < n);
printf("The largest number is %d.\n", largest);

return 0;
}
10.
#include <stdio.h>

int main() {
int n;
int num;
int smallest;

printf("For loop \n");

printf("How many numbers will you enter?: ");


scanf("%d", &n);

for (int i = 0; i < n; i++) {


printf("Enter the number: ");
scanf("%d", &num);
if (num < smallest) {
smallest = num;
}
}
printf("The smallest number is %d.\n", smallest);

printf("\n");
int smally;
int y = 0;
printf("For while loop \n");
smally = num;

while (y < n) {
printf("Enter the number: ");
scanf("%d", &num);
if (num < smally) {
smally = num;
}
y++;
}
printf("The smallest number is %d.\n", smally);

printf("\n");

int x = 0;
int small;
printf("For do while loop \n");
small = num;
do {
printf("Enter the number: ");
scanf("%d", &num);
if (num < small) {
small = num;
}
x++;
} while (x < n);
printf("The smallest number is %d.\n", small);

return 0;
}

11.
#include <stdio.h>

int main() {
int number;
int start;
int end;

printf("For loop \n");


printf("Multiplication table:\n");

printf("Enter a number: ");


scanf("%d", &number);

printf("Enter the starting range: ");


scanf("%d", &start);
printf("Enter the ending range: ");
scanf("%d", &end);

for (int i = start; i <= end; i++) {


printf("%d x %d = %d\n", number, i, number * i);
}

if (start > end) {


printf("Invalid range.\n");
}
printf("\n");

printf("For while loop \n");


printf("Multiplication table:\n");
int i = start;
while (i <= end) {
printf("%d x %d = %d\n", number, i, number * i);
i++;
}

printf("\n");
printf("For do while loop \n");

printf("Multiplication table:\n");
i = start;
do {
printf("%d x %d = %d\n", number, i, number * i);
i++;
} while (i <= end);

return 0;
}
12.
#include <stdio.h>

int main() {
int num;
printf("Enter a non-negative integer: ");
if (scanf("%d", &num) != 1 || num < 0) {
printf("Invalid input.\n");
return 1;
}

long long factorial = 1;


for (int i = 1; i <= num; i++) {
factorial *= i;
}

printf("The factorial of %d is %lld.\n", num, factorial);


return 0;
}

#include <stdio.h>
int main() {
int num;
printf("Enter a non-negative integer: ");
if (scanf("%d", &num) != 1 || num < 0) {
printf("Invalid input.\n");
return 1;
}

long long factorial = 1;


int i = 1;
while (i <= num) {
factorial *= i;
i++;
}

printf("The factorial of %d is %lld.\n", num, factorial);


return 0;
}

#include <stdio.h>

int main() {
int num;
printf("Enter a non-negative integer: ");
if (scanf("%d", &num) != 1 || num < 0) {
printf("Invalid input.\n");
return 1;
}

long long factorial = 1;


int i = 1;
if(num == 0){
printf("The factorial of %d is 1.\n", num);
return 0;
}
do {
factorial *= i;
i++;
} while (i <= num);

printf("The factorial of %d is %lld.\n", num, factorial);


return 0;
}

13.
#include <stdio.h>

int main() {
int n;
int a = 0;
int b = 1;
int next;

printf("For loop\n");
printf("Enter a number: ");
scanf("%d", &n);

printf("%d %d ", a, b);

for (int i = 2; i <= n; i++) {


next = a + b;
if (next > 1000) {
break;
}

printf("%d ", next);

a = b;
b = next;
}

printf("\n");
printf("\n");

printf("For while loop\n");


int next1;
int c = 0, d = 1;
int j = 2;

printf("%d %d ", c, d);

while(j <= n){


next1 = c + d;
if(next1 > 1000){
break;
}
printf("%d ", next1);

c = d;
d = next1;
j++;
}
printf("\n");

printf("\n");

printf("For do-while loop\n");


int next2;
int e = 0, f = 1;
int k = 2;

printf("%d %d ", e, f);

do{
next2 = e + f;
if(next1 > 1000){
break;
}
printf("%d ", next2);

e = f;
f = next2;
k++;
}while(k <= n);

return 0;
}

14.
#include <stdio.h>
#include <string.h>

int palindrome(char str[]) {


int len = strlen(str);
for (int i = 0, j = len - 1; i < j; i++, j--) {

char left = str[i];


char right = str[j];

if (left >= 'A' && left <= 'Z') {


left = left - 'A' + 'a';
}
if (right >= 'A' && right <= 'Z') {
right = right - 'A' + 'a';
}

while (i < j && !((left >= 'a' && left <= 'z') || (left >= '0' && left <= '9'))) {
i++;
left = str[i];
if (left >= 'A' && left <= 'Z') {
left = left - 'A' + 'a';
}
}
while (i < j && !((right >= 'a' && right <= 'z') || (right >= '0' && right <= '9'))) {
j--;
right = str[j];
if (right >= 'A' && right <= 'Z') {
right = right - 'A' + 'a';
}
}

if (i < j && left != right) {


return 0;
}
}
return 1;
}

int main() {
char str[100];

printf("For loop :\n");


printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = 0;

if (palindrome(str) == 1) {
printf("Palindrome\n");
} else {
printf("Not a palindrome\n");
}
printf("\n");

printf("While loop:\n");
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = 0;

if (palindrome(str) == 1) {
printf("Palindrome\n");
} else {
printf("Not a palindrome\n");
}
printf("\n");

// Do-while loop
printf("Do-while loop:\n");
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = 0;

if (palindrome(str) == 1) {
printf("Palindrome\n");
} else {
printf("Not a palindrome\n");
}

return 0;
}

15.
#include <stdio.h>

int main() {
int num;
int prime;

printf("For loop\n");
printf("Enter a number: ");
scanf("%d", &num);

if (num <= 1) {
printf("Not prime\n");
} else {
prime = 1;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
prime = 0;
break;
}
}
if (prime == 1) {
printf("Prime\n");
} else {
printf("Not prime\n");
}
}
printf("\n");

printf("For While loop\n");


printf("Enter a number: ");
scanf("%d", &num);

if (num <= 1) {
printf("Not prime\n");
} else {
prime = 1;
int i = 2;
while (i * i <= num) {
if (num % i == 0) {
prime = 0;
break;
}
i++;
}
if (prime == 1) {
printf("Prime\n");
} else {
printf("Not prime\n");
}
}
printf("\n");

printf("For Do-while loop\n");


printf("Enter a number: ");
scanf("%d", &num);
if (num <= 1) {
printf("Not prime\n");
} else {
prime = 1;
int i = 2;
do {
if (num % i == 0) {
prime = 0;
break;
}
i++;
} while (i * i <= num);

if (prime == 1) {
printf("Prime\n");
} else {
printf("Not prime\n");
}
}

return 0;
}

16.
#include <stdio.h>

int main() {
int n;

printf("Enter a number: ");


scanf("%d", &n);

printf("\n");

printf("Using for loop:\n");

for(int i = 1; i <= n; i++){


for(int j = 1; j <= i; j++){
printf("*");
}
printf("\n");
}

printf("\n");

printf("Using while loop:\n");

int k = 1;
while(k <= n){
int l = 1;
while(l <= k){
printf("*");
l++;
}
printf("\n");
k++;
}

printf("\n");

printf("Using do-while loop:\n");

int a = 1;
do{
int b = 1;
do{
printf("*");
b++;
}while(b <= a);
printf("\n");
a++;
}while(a <= n);

return 0;
}

17.
#include <stdio.h>

int main() {
int secret;
int guess;
printf("For loop \n");
printf("Welcome to the Number Guessing Game!\n");
printf("Enter a number to be guessed: ");
scanf("%d", &secret);
printf("You have 5 tries to guess it.\n");
printf("\n");

for (int i = 1; i <= 5; i++) {


printf("Try %d: Enter your guess: ", i);
scanf("%d", &guess);

if (guess == secret) {
printf("You win! You guessed the number in %d trie/s.\n", i);
return 0;
} else if (guess < secret) {
printf("Too low!\n");
printf("\n");
} else {
printf("Too high!\n");
printf("\n");
}
}
printf("You lose. The number was %d.\n", secret);

printf("\n");

int secrets;
int guessy;
int j=1;
printf("For while loop \n");

printf("Welcome to the Number Guessing Game!\n");


printf("Enter a number to be guessed: ");
scanf("%d", &secrets);
printf("You have 5 tries to guess it.\n");
printf("\n");
while(j <= 5){
printf("Try %d: Enter your guess: ", j);
scanf("%d", &guessy);
if (guessy == secrets) {
printf("You win! You guessed the number in %d trie/s.\n", j);
return 0;
} else if (guessy < secrets) {
printf("Too low!\n");
printf("\n");

} else {
printf("Too high!\n");
printf("\n");
}
j++;
}
printf("You lose. The number was %d.\n", secrets);
printf("\n");
printf("For do while loop \n");
int secretss;
int guessyt;
int k=1;

printf("Welcome to the Number Guessing Game!\n");


printf("Enter a number to be guessed: ");
scanf("%d", &secretss);
printf("You have 5 tries to guess it.\n");
printf("\n");
do{
printf("Try %d: Enter your guess: ", k);
scanf("%d", &guessyt);
if (guessyt == secretss) {
printf("You win! You guessed the number in %d trie/s.\n", k);
return 0;
} else if (guessyt < secretss) {
printf("Too low!\n");
printf("\n");
} else {
printf("Too high!\n");
printf("\n");
}
k++;
}while( k <= 5);

printf("You lose. The number was %d.\n", secretss);

return 0;
}
18.
#include <stdio.h>

int main(){
int start;
int end;
int even = 0;

printf("Enter a starting number: ");


scanf("%d", &start);
printf("Enter an ending number: ");
scanf("%d", &end);

for(int i = 1; i <= end; i++){


if(i % 2 == 0){
even+=i;
}
}
printf("The sum of even numbers is: %d", even);

return 0;
}

#include <stdio.h>
int main() {
int started;
int ended;
int even;
int i;
i=started;
even = 0;
printf("For While loop\n");
printf("Enter a starting number: ");
scanf("%d", &started);
printf("Enter an ending number: ");
scanf("%d", &ended);

while (i <= ended) {


if (i % 2 == 0) {
even += i;
}
i++;
}
printf("The sum of even numbers is: %d\n", even);

return 0;
}
#include <stdio.h>
int main(){
int start;
int end;
int even = 0;
int k;
k = start;
printf("Do-while loop\n");
printf("Enter a starting number: ");
scanf("%d", &start);
printf("Enter an ending number: ");
scanf("%d", &end);
do {
if (k % 2 == 0) {
even += k;
}
k++;
} while (k <= end);
printf("The sum of even numbers is: %d\n", even);

return 0;
}

19.
#include <stdio.h>
int main() {
int num;
int sum = 0;
int count = 0;
float average;

printf("For loop \n");


printf("Enter numbers:\n");

for (;;) {
scanf("%d", &num);

if (num < 0) {
break;
}
if (num > 0) {
sum += num;
count++;
}
}
if (count == 0) {
printf("No positive numbers entered\n");
} else {
average = (float)sum / count;
printf("The Average: %.2f\n", average);
}
printf("For While loop \n");
printf("Enter numbers:\n");

scanf("%d", &num);
while (num >= 0) {
if (num > 0) {
sum += num;
count++;
}
scanf("%d", &num);
}
if (count == 0) {
printf("No positive numbers entered\n");
} else {
average = (float)sum / count;
printf("The Average: %.2f\n", average);
}
printf("For Do-while loop \n");
printf("Enter numbers:\n ");
do {
scanf("%d", &num);
if (num >= 0) {
if (num > 0) {
sum += num;
count++;
}
}
} while (num >= 0);
if (count == 0) {
printf("No positive numbers entered\n");
} else {
average = (float)sum / count;
printf("The Average: %.2f\n", average);
}
return 0;
}
20.
#include <stdio.h>
int main() {
int choice;
int number;
int square;
printf("Menu\n");
printf("1. Prints Hello!\n");
printf("2. Input a number to print the square.\n");
printf("3. Exit the program.\n");
for(;;){
printf("\nWhat's your order?: ");
scanf("%d", &choice);
if(choice == 1){
printf("Hello!\n");
}else if(choice == 2){
printf("Enter a number: ");
scanf("%d", &number);
square=number*number;
printf("The square of %d is %d\n", number, square);
}else if(choice == 3){
printf("Exiting program..");
return 1;
}
}
return 0;
}
#include <stdio.h>
int main() {

printf("For while loop\n");


int choices;
int numbers;
int squares;

printf("Menu\n");
printf("1. Prints Hello!\n");
printf("2. Input a number to print the square.\n");
printf("3. Exit the program.\n");

while (choices != 3) {
printf("What's your order?: ");
scanf("%d", &choices);
if(choices == 1){
printf("Hello!\n");
}else if(choices == 2){
printf("Enter a number: ");
scanf("%d", &numbers);
squares=numbers*numbers;
printf("The square of %d is %d\n", numbers, squares);
}else if(choices == 3){
printf("Exiting program..");
return 1;
}
}

return 0;
}

// Online C compiler to run C program online


#include <stdio.h>

int main() {
int choiced;
int numberd;
int squared;
choiced = 0;
printf("For Do-while loop\n");
printf("Menu\n");
printf("1. Prints Hello!\n");
printf("2. Input a number to print the square.\n");
printf("3. Exit the program.\n");

do {
printf("What's your order?: ");
scanf("%d", &choiced);

if (choiced == 1) {
printf("Hello!\n");
} else if (choiced == 2) {
printf("Enter a number: ");
scanf("%d", &numberd);
squared = numberd * numberd;
printf("The square of %d is %d\n", numberd, squared);
} else if (choiced == 3) {
printf("Exiting program..\n");
} else {
printf("Invalid choice. Please try again.\n");
}
} while (choiced != 3);

return 0;
}

You might also like