0% found this document useful (0 votes)
75 views6 pages

C Programming Examples and Techniques

c programming

Uploaded by

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

C Programming Examples and Techniques

c programming

Uploaded by

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

1.

SWAP THE TWO NUMBERS USING THREE METHODS

// Method 1: Using Temp Variable


#include <stdio.h>
int main() {
int a = 5, b = 10, temp;
temp = a;
a = b;
b = temp;
printf("Using Temp: a = %d, b = %d\n", a, b);
return 0;
}

// Method 2: Without Temp


#include <stdio.h>
int main() {
int a = 5, b = 10;
a = a + b;
b = a - b;
a = a - b;
printf("Without Temp: a = %d, b = %d\n", a, b);
return 0;
}

// Method 3: Using XOR


#include <stdio.h>
int main() {
int a = 5, b = 10;
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("Using XOR: a = %d, b = %d\n", a, b);
return 0;
}

2. SIMPLE INTEREST AND COMPOUND INTEREST

#include <stdio.h>
#include <math.h>
int main() {
float p = 1000, r = 5, t = 2, si, ci;
si = (p * r * t) / 100;
ci = p * pow((1 + r / 100), t) - p;
printf("Simple Interest = %.2f\n", si);
printf("Compound Interest = %.2f\n", ci);
return 0;
}

3. ADDITION OF MATRIX

#include <stdio.h>
int main() {
int a[2][2] = {{1, 2}, {3, 4}}, b[2][2] = {{5, 6}, {7, 8}}, sum[2][2];
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
sum[i][j] = a[i][j] + b[i][j];
printf("%d ", sum[i][j]);
}
printf("\n");
}
return 0;
}

4. PRINT THE ARRAY ELEMENTS USING POINTERS

#include <stdio.h>
int main() {
int a[] = {10, 20, 30, 40, 50};
int *ptr = a;
for(int i = 0; i < 5; i++) {
printf("%d ", *(ptr + i));
}
return 0;
}

5. BIGGEST AND SMALLEST NO. OF AN ARRAY

#include <stdio.h>
int main() {
int a[] = {5, 10, 3, 8, 6}, max = a[0], min = a[0];
for(int i = 1; i < 5; i++) {
if(a[i] > max) max = a[i];
if(a[i] < min) min = a[i];
}
printf("Max = %d, Min = %d\n", max, min);
return 0;
}

6. COUNT NO. OF VOWELS AND ALPHABET

#include <stdio.h>
int main() {
char str[] = "Hello World";
int vowels = 0, alphabets = 0;
for(int i = 0; str[i] != '\0'; i++) {
char ch = str[i];
if((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
alphabets++;
if(ch == 'a'|| ch == 'e'|| ch == 'i'|| ch == 'o'|| ch == 'u'||
ch == 'A'|| ch == 'E'|| ch == 'I'|| ch == 'O'|| ch == 'U')
vowels++;
}
}
printf("Vowels = %d, Alphabets = %d\n", vowels, alphabets);
return 0;
}
7. STRUCTURE FOR STORING ACCOUNT DETAILS USING POINTERS

#include <stdio.h>
struct Account {
int accNo;
char name[20];
float balance;
};

int main() {
struct Account acc = {101, "John", 5000.5};
struct Account *ptr = &acc;
printf("Account No: %d\nName: %s\nBalance: %.2f\n", ptr->accNo, ptr->name,
ptr->balance);
return 0;
}

8. FAHRENHEIT TO CELSIUS AND CELSIUS TO FAHRENHEIT

#include <stdio.h>
int main() {
float c = 37, f;
f = (c * 9 / 5) + 32;
printf("Celsius to Fahrenheit = %.2f\n", f);

f = 98.6;
c = (f - 32) * 5 / 9;
printf("Fahrenheit to Celsius = %.2f\n", c);
return 0;
}

9. FUNCTION TO PRINT FIBONACCI NUMBERS

#include <stdio.h>
void fibonacci(int n) {
int a = 0, b = 1, next;
for(int i = 1; i <= n; i++) {
printf("%d ", a);
next = a + b;
a = b;
b = next;
}
}

int main() {
fibonacci(10);
return 0;
}

10. ALPHABETICAL ORDER

#include <stdio.h>
#include <string.h>
int main() {
char str[5][20] = {"Banana", "Apple", "Mango", "Cherry", "Peach"}, temp[20];
for(int i = 0; i < 5; i++) {
for(int j = i + 1; j < 5; j++) {
if(strcmp(str[i], str[j]) > 0) {
strcpy(temp, str[i]);
strcpy(str[i], str[j]);
strcpy(str[j], temp);
}
}
}
for(int i = 0; i < 5; i++) {
printf("%s\n", str[i]);
}
return 0;
}

11. ARMSTRONG NUMBERS UP TO 1000

#include <stdio.h>
#include <math.h>
int main() {
int num, temp, rem, sum;
for(num = 1; num <= 1000; num++) {
temp = num;
sum = 0;
while(temp > 0) {
rem = temp % 10;
sum += rem * rem * rem;
temp /= 10;
}
if(sum == num) {
printf("%d ", num);
}
}
return 0;
}

12. SIMPLE CALCULATOR USING SWITCH AND GOTO

#include <stdio.h>
int main() {
float a, b;
char op;
start:
printf("Enter operation (+, -, *, /): ");
scanf(" %c", &op);
printf("Enter two numbers: ");
scanf("%f %f", &a, &b);

switch(op) {
case '+': printf("%.2f\n", a + b); break;
case '-': printf("%.2f\n", a - b); break;
case '*': printf("%.2f\n", a * b); break;
case '/': if(b != 0) printf("%.2f\n", a / b); else printf("Divide by zero
error\n"); break;
default: printf("Invalid operator\n");
}
goto start;
return 0;
}

13. STRUCTURE FOR STUDENT DETAILS

#include <stdio.h>
struct Student {
int id;
char name[20];
float marks;
};

int main() {
struct Student s = {1, "John", 85.5};
printf("ID: %d\nName: %s\nMarks: %.2f\n", [Link], [Link], [Link]);
return 0;
}

14. GRADE USING IF ELSE

#include <stdio.h>
int main() {
int marks = 75;
if(marks >= 90)
printf("Grade: A");
else if(marks >= 75)
printf("Grade: B");
else if(marks >= 60)
printf("Grade: C");
else if(marks >= 40)
printf("Grade: D");
else
printf("Fail");
return 0;
}

15. FACTORIAL AND REVERSE NUMBER

#include <stdio.h>
int main() {
int n = 5, fact = 1, rev = 0, temp = n;
for(int i = 1; i <= n; i++) fact *= i;
while(n > 0) {
rev = rev * 10 + n % 10;
n /= 10;
}
printf("Factorial: %d\n", fact);
printf("Reverse: %d\n", rev);
return 0;
}
16. SWITCH CASE (5 CONDITIONS)

#include <stdio.h>
int main() {
int choice = 3;
switch(choice) {
case 1: printf("One\n"); break;
case 2: printf("Two\n"); break;
case 3: printf("Three\n"); break;
case 4: printf("Four\n"); break;
case 5: printf("Five\n"); break;
default: printf("Invalid\n");
}
return 0;
}

17. COUNT WORDS IN A SENTENCE

#include <stdio.h>
int main() {
char str[] = "This is a test string";
int i = 0, count = 1;
while(str[i] != '\0') {
if(str[i] == ' ' && str[i+1] != ' ' && str[i+1] != '\0')
count++;
i++;
}
printf("Words: %d\n", count);
return 0;
}

You might also like