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

Simple Codes

Uploaded by

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

Simple Codes

Uploaded by

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

1) Cube

#include <stdio.h>

int main() {
int x = 10;

printf("The cube for %d is %d\n", x, x * x*x);

return 0;
}
Output:
The cube for 10 is 1000

=== Code Execution Successful ===


2) Square:
#include <stdio.h>

int main() {
int n = 8;

printf("The square for %d is %d\n", n, n * n);

return 0;
}
Output:
The square for 8 is 64
3) Average:
#include <stdio.h>

int main() {
int p = 2, q = 4, r = 6;
float s;

s = (p + q + r) / 3;

printf("The average of %d, %d, and %d is: %.2f", p, q, r, s);

return 0;
}

Output:
The average of 2, 4, and 6 is: 4.00

=== Code Execution Successful ===


4) Table of 5:
#include <stdio.h>

int main() {
int n, i;

for (i = 1; i <= 10; i++) {


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

return 0;
}
Output:
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
5) Swapping:
#include <stdio.h>

int main() {
int x = 5, y = 10, t;

printf("Before swapping: x = %d, y = %d\n", x, y);

t = x;
x = y;
y = t;

printf("After swapping: x = %d, y = %d\n", x, y);

return 0;
}
Output:
Before swapping: x = 5, y = 10
After swapping: x = 10, y = 5

=== Code Execution Successful ===


6) Addition
#include <stdio.h>

int main() {
int a, b, c;

printf("Enter first no: ");


scanf("%d", &a);

printf("Enter second no: ");


scanf("%d", &b);

c = a + b;

printf("Sum of %d and %d is %d\n", a, b, c);

return 0;
}
Output:
Enter first no: 15
Enter second no: 10
Sum of 15 and 10 is 25

=== Code Execution Successful ===


7) Printing Pattern Numbers:
#include <stdio.h>
int main() {
int r = 3;
int i, j;

for (i = 1; i <= r; i++) {


for (j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n");
}

return 0;
}
Output:
1
1 2
1 2 3

=== Code Execution Successful ===


8) Printing pattern Stars:
#include <stdio.h>

int main() {
int r,i,j;

r = 3;
for(i = 1; i <= r; i++) {
for(j = 1; j <= r-i; j++)
printf(" ");

for(j = 1; j <= i; j++)


printf("* ");

printf("\n");
}
return 0;
}
Output:
*
* *
* * *

=== Code Execution Successful ===


9) Odd/ Even
#include <stdio.h>

int main()
{
int n;
printf("Enter any number n: ");
scanf("%d", &n);

//logic
if (n % 2 == 0) {
printf("No is even");
}
else {
printf("No is odd");
}
return 0;
}

Output:
Case 1: Enter any number n: 2
No is even

=== Code Execution Successful ===


Case 2: Enter any number n: 15
No is odd

=== Code Execution Successful ===


10) Uppercase/Lowercase:
#include <stdio.h>
void checkstr(char ch);
int main()
{
char ch;
printf("Enter a character");
scanf("%c",&ch);
checkstr(ch);
return 0;
}
void checkstr(char ch)
{
if(ch>=65 && ch<=90)
{
printf("Uppercase character\n");
}
else if(ch>=97 && ch<=122)
{
printf("Lowercase character\n");
}
else if( ch>=48 && ch<=57)
{
printf("Digit\n");
}
else
{
printf("Special symbol\n");
}
}

Output:
Enter a character*
Special symbol

=== Code Execution Successful ===

You might also like