[go: up one dir, main page]

0% found this document useful (0 votes)
6 views44 pages

Pps Codess

The document contains a collection of C programming code snippets demonstrating various concepts such as basic input/output, arithmetic operations, data types, control structures, loops, arrays, and pointers. Each section includes a brief description of the functionality along with the corresponding code. It serves as a practical guide for learning and understanding fundamental programming techniques in C.

Uploaded by

rajamrit0603
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)
6 views44 pages

Pps Codess

The document contains a collection of C programming code snippets demonstrating various concepts such as basic input/output, arithmetic operations, data types, control structures, loops, arrays, and pointers. Each section includes a brief description of the functionality along with the corresponding code. It serves as a practical guide for learning and understanding fundamental programming techniques in C.

Uploaded by

rajamrit0603
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
You are on page 1/ 44

C Program

Collection of Code
Hello World
• #include<stdio.h>
• int main (){
• printf("Hello, World!");
• return 0;
• }
Sum of Two Numbers
• #include<stdio.h>
• int main (){
• int a,b;
• printf("enter a and b");
• scanf("%d %d",&a,&b);
• printf("sum is %d",a+b);
• return 0;
• }
Size of Data Type
• #include<stdio.h>
• int main()
• {
• int a;
• float b;
• char c;
• double d;
• long double e;
• short f;
• printf("size of int is %d\n",sizeof(a));
• printf("size of float is %d\n",sizeof(b));
• printf("size of char is %d\n",sizeof(c));
• printf("size of double is %d\n",sizeof(d));
• printf("size of long double is %d\n",sizeof(e));
• printf("size of short is %d\n",sizeof(f));
• return 0;
• }
Increment Operator
• #include<stdio.h>
• int main (){
• int a=4,b=5,c;
• c=++a + b--;
• printf("%d %d %d ",a,b,c);

return 0;
• }

Decrement Operator
• #include<stdio.h>
• int main (){
• int a=4,b=5,c;
• c=--a + b--;
• printf("%d %d %d ",a,b,c);

return 0;
• }

Bitwise and (&) Operator
• #include <stdio.h>
• int main() {
• int a = 5; // 0101 in binary
• int b = 3; // 0011 in binary
• int result = a & b; // Bitwise AND: 0001 in binary (1
in decimal)

• printf("Bitwise AND of %d and %d is: %d\n", a, b,
result);
• return 0;
• }
Bitwise or (|) Operator
• #include <stdio.h>
• int main() {
• int a = 5; // 0101 in binary
• int b = 3; // 0011 in binary
• int result = a | b; // Bitwise OR: 0111 in binary (7
in decimal)

• printf("Bitwise OR of %d and %d is: %d\n", a, b,
result);
• return 0;
• }
Bitwise xor (^) Operator
• #include <stdio.h>
• int main() {
• int a = 5; // 0101 in binary
• int b = 3; // 0011 in binary
• int result = a ^ b; // Bitwise XOR: 0110 in binary (6
in decimal)
• printf("Bitwise XOR of %d and %d is: %d\n", a, b,
result);
• return 0;
• }
Bitwise not (~) Operator
• #include <stdio.h>
• int main() {
• int a=5;
• int b=~a;
• printf("%d",b);
• return 0;
• }
Left Shift Operator
• #include <stdio.h>
• int main() {
• int a = 5; // 0101 in binary
• int result = a << 1; // Left shift by 1 position:
1010 in binary (10 in decimal)
• printf("Left shift of %d is: %d\n", a, result);
• return 0;
• }
Bitwise Right Operator
• #include <stdio.h>
• int main() {
• int a = 5; // 0101 in binary
• int result = a >> 1; // Right shift by 1 position:
0010 in binary (2 in decimal)
• printf("Right shift of %d is: %d\n", a, result);
• return 0;
• }

Conditional or Ternary Operator
• #include <stdio.h>
• int main() {
• int n;
• printf("enter a number: ");
• scanf("%d",&n);
• //condition ? exp1 : exp2;
• n%2==0 ? printf("even number") : printf("odd number");
• return 0;
• }

Print 1 to 100
• #include<stdio.h>
• int main (){
• int n=100;
• for (int i=1;i<=n;i++){
• printf("%d\n",i);
• }
• return 0;
• }
Factorial by using loop
• #include<stdio.h>
• int main (){
• int n;
• int fact=1;
• printf("enter a number");
• scanf("%d",&n);
• for(int i=n;i>0;i--){
• fact=fact*i;
• }
• printf("factorial of %d is %d",n,fact);

return 0;
• }
Fibonacci Series by using loop
• #include<stdio.h>
• int main (){
• int n;
• printf("enter number of terms");
• scanf("%d",&n);
• int t1=0,t2=1,NextTerm;
• printf("fibonacci series: %d %d ",t1,t2);
• for(int i=2;i<n;i++){
• NextTerm=t1+t2;
• t1=t2;
• t2=NextTerm;
• printf("%d ",NextTerm);
• }
• return 0;
• }
Expand AP
• // expand an A.P. ---- 3,5,7,9,11,13.........

#include<stdio.h>
• void main (){
• int n;
• int a=3;
• printf("enter the term upto be expand");
• scanf("%d",&n);
• printf("A.P. is:- ");
• for (int i=1;i<=n;i++){
• printf("%d ",a);
• a=a+2;

• }
• }
Print 10 to 1 by do-while loop
• #include<stdio.h>
• int main (){
• int n=10;
• do {
• printf("%d ",n);
• n--;
• }
• while (n>0);
• return 0;
• }
Factorial by While Loop
• #include<stdio.h>
• int main(){
• int n;
• printf("enter a number :");
• scanf("%d",&n);
• int i=1,fact=1;
• while(i<=n){
• fact=fact*i;
• i++;
• }
• printf("factorial of %d is %d",n,fact);
• return 0;
• }
Odd-Even
• #include<stdio.h>
• int main (){
• int n;
• printf("enter a number");
• scanf("%d",&n);
• if(n%2==0){
• printf("%d is an even number",n);
• }
• else {
• printf("%d is an odd number",n);
• }
• return 0;
• }
Right Angle Star Pattern
• #include<stdio.h>
• void main (){
• int n;
• printf("enter number of rows");
• scanf("%d",&n);
• for(int i=1;i<=n;i++){
• for(int j=1;j<=i;j++){
• printf("*");
• }
• printf("\n");
• }
• }
Root of a Quadratic Equation
• #include<stdio.h>
• #include<math.h>
• int main (){
• int a,b,c,D;
• printf("enter a , b and c");
• scanf("%d %d %d",&a,&b,&c);
• D=(b*b)-(4*a*c);
• if(D<0){
• printf("root of equation is complex");
• }
• else {
• float root1=(-b+D)/2*a;
• float root2=(-b-D)/2*a;
• printf("first root is %f\n",root1);
• printf("second root is %f",root2);
• }
• return 0;
• }
Calculator by using if-else
• #include<stdio.h>
• int main (){
• int a,b;
• char ch;
• printf("enter a :");
• scanf("%d",&a);
• printf("enter a opertaor(+,-,*,/) :");
• scanf(" %c",&ch);
• printf("enter b :");
• scanf("%d",&b);
• if(ch=='+'){
• printf("sum is %d",a+b);
• }
• else if (ch=='-'){
• printf("substraction is %d",a-b);
• }
• else if (ch=='*'){
• printf("multiplication is %d",a*b);
• }
• else if (ch=='/'){
• printf("division is %f",(float)a/b);
• }
• else {
• printf("enter a valid operator");
• }

return 0;
• }
Calculator by using Switch case
• #include<stdio.h>
• int main (){
• int a;
• printf("enter a");
• scanf("%d",&a);
• char ch;
• printf("enter a operator (+,-,*,/)");
• scanf(" %c",&ch);
• int b;
• printf("enter b");
• scanf("%d",&b);
• switch(ch){
• case '+':
• printf("sum is %d",a+b);
• break;
• case '-':
• printf("substraciton is %d",a-b);
• break;
• case '*':
• printf("multiplication is %d",a*b);
• break;
• case '/':
• printf("division is %f",(float)a/b);
• break;
• default:
• printf("enter a valid operator");
• }
• return 0;
• }
Print all element of Array
• #include<stdio.h>
• int main (){
• int arr[5]={1,2,3,4,5};
• for(int i=0;i<5;i++){
• printf("%d\n",arr[i]);
• }
• return 0;
• }
Insert an element in array
• #include<stdio.h>
• int main (){
• int a[100],n,position,element;
• printf("enter the number of element of array");
• scanf("%d",&n);
• printf("enter all the element");
• for(int i=0;i<n;i++){
• scanf("%d",&a[i]);
• }
• printf("enter the element to be inserted in array");
• scanf("%d",&element);
• printf("enter the position");
• scanf("%d",&position);
• for(int i=n;i>=position;i--){
• a[i]=a[i-1];
• }
• a[position-1]=element;
• printf("here is the new array: ");
• for(int i=0;i<=n;i++){
• printf("%d ",a[i]);
• }
• return 0;
• }

Delete an element in Array
#include<stdio.h>
• int main (){
• int a[100],n,position;
• printf("enter the number of element of array");
• scanf("%d",&n);
• printf("enter all the element");
• for(int i=0;i<n;i++){
• scanf("%d",&a[i]);
• }
• printf("enter the position to be deleted");
• scanf("%d",&position);
• for(int i=position-1;i>=n;i++){
• a[i]=a[i+1];
• }

• printf("here is the new array: ");
• for(int i=0;i<n-1;i++){
• printf("%d ",a[i]);
• }
• return 0;
• }
Maximum element of Array
• #include<stdio.h>
• #include<limits.h>
• void main (){
• int a[5][2]={1,2,3,5,6,3,44,5,-2,0};
• int max=INT_MIN;
• for(int i=0;i<5;i++)
• {
• for(int j=0;j<2;j++){

if(max<a[i][j])
• max=a[i][j];
• }
• }
• printf("%d",max);
• }
Minimum element of Array
• #include<stdio.h>
• #include<limits.h>
• void main (){
• int a[4][4]={1,2,3,4,4,4,5,6,7,8,8,99,9,-5,-112,0};
• int min=INT_MAX;
• int i,j;
• for( i=0;i<4;i++){

for( j=0;j<4;j++){
• if(min>a[i][j]){
• min=a[i][j];
• }

}
• }
• printf("minimum element in array is:-%d\n",min);

}
Bounds Checking
➢ In C, there is no built-in support for bounds checking on arrays. The language allows direct memory
access, which means it's the programmer's responsibility to ensure that array bounds are respected.

• #include <stdio.h>

int main() {
• int array[5] = {10, 20, 30, 40, 50}; // Array with 5 elements

// Accessing an element out of bounds
• printf("array[7] = %d\n", array[7]); // Undefined behavior

return 0;
• }

Take input by user and print 2-D Array
• #include<stdio.h>
• int main (){
• int array[100][100];
• int n;
• printf("enter the no of element of array: ");
• scanf("%d",&n);
• printf("enter all the element: ");
• for(int i=0;i<n;i++){
• for(int j=0;j<n;j++){
• scanf("%d",&array[i][j]);
• }
• }
• printf("display the element of array: ");
• for(int i=0;i<n;i++){
• for(int j=0;j<n;j++){
• printf("%d ",array[i][j]);
• }
• printf("\n");
• }
• return 0;
• }
Sum of 2 Matrix
• #include<stdio.h>
• void main (){

• int a[2][3]={1,2,3,4,5,6};
• int b[2][3]={5,6,7,8,0,0};
• int i,j;
• for(i=0;i<2;i++){
• for(j=0;j<3;j++){

• }
• }
• for(i=0;i<2;i++){
• for(j=0;j<3;j++){
• printf("%d " ,(a[i][j])+(b[i][j]));
• }
• printf("\n");
• }

}
Pointer
• #include<stdio.h>
• int main (){
• int myage=20;
• int* ptr;
• ptr = &myage;
• printf("%d",myage);
• printf("\n%p",ptr);
• printf("\n%p",&myage);
• printf("\n%d",*ptr);
• printf("\n%p",*&ptr);

printf("\n%p",&*ptr);
• int n[5]={1,2,3,4,50};
• for(int i=0;i<5;i++){
• printf("%p\n",&n[i]);
• }
• int myNumbers[5]={1,2,34,5,5};
• // Get the memory address of the myNumbers array
• printf("%d\n",*myNumbers);

// Get the memory address of the first array element
• return 0;
• }
Relation b/w array and pointer
• #include <stdio.h>

• int main() {
• int myNumbers[4] = {25, 50, 75, 100};

// Get the value of the second element in myNumbers
• printf("%d\n", *(myNumbers + 1));

// Get the value of the third element in myNumbers
• printf("%d", *(myNumbers + 2));

• return 0;
• }
Basic Function
• #include<stdio.h>
• void phool(){
• printf("Phool Babu\n");
• return;
• }
• int main (){
• phool();
• phool();
• return 0;

• }
Prototype in Function
• #include<stdio.h>

int main (){
• void fun(); //this is prototype
• fun();
• return 0;
• }
• void fun (){
• printf("Hello World!");
• }
Factorial by using Function
• #include<stdio.h>
• int factorial(int x)
• {
• int fact=1;
• for(int i=1;i<=x;i++){
• fact=fact*i;
• }
• return fact;
• }
• int main ()
• {
• int n;
• printf("enter a number");
• scanf("%d",&n);
• int facto=factorial(n);
• printf("%d",facto);

return 0;
• }
Fibonacci Series by using Function
• #include<stdio.h>
• int fibonacci(int x){
• int t1=0;
• int t2=1,nextterm;
• if(x==1){
• printf("fibonacci series: %d",t1);

• }
• else if (x==2){
• printf("fibonacci series: %d %d ",t1,t2);
• }
• else {
• printf("fibonacci series: %d %d ",t1,t2);
• for(int i=3;i<=x;i++){
• nextterm=t1+t2;
• printf("%d ",nextterm);
• t1=t2;
• t2=nextterm;
• }
• printf("\n");
• }
• return nextterm;
• }
• int main (){
• int n;
• printf("enter the number of terms: ");
• scanf("%d",&n);

int series=fibonacci(n);

• return 0;
Odd-Even by using Function
• #include<stdio.h>
• int even(int x){
• return x%2==0;
• }
• int main (){
• int n;
• printf("enter a number");
• scanf("%d",&n);

if (even(n)) {
• printf("even number");
• }
• else {
• printf("odd number");
• }
• return 0;
• }
Sum of two numbers by using Function
• #include<stdio.h>
• int sum(int x, int y){
• return x+y;
• }
• int main (){
• int a,b;
• printf("enter a and b");
• scanf("%d %d",&a,&b);
• int add=sum(a,b);
• printf("%d",add);
• }
Sum of first n natural number by using Recursion
• #include<stdio.h>
• int sum(int x){
• int s=0;
• if(x==1)
• return x;
• s=x+sum(x-1);
• return s;
• }
• int main (){
• int n;
• printf("enter a number");
• scanf("%d",&n);
• printf("Sum of first %d number is %d",n,sum(n));


}
Factorial By Using Recursion
• #include<stdio.h>
• int factorial(int x){
• int product=1;
• if(x==0)
• return 1;
• else
• product=x*factorial(x-1);
• return product;
• }
• int main (){
• int n;
• printf("enter a number: ");
• scanf("%d",&n);
• printf("factorial of %d is %d\n",n,factorial(n));

• for(int i=1;i<=n;i++){
• printf("%d\n",factorial(i));
• }
• return 0;
• }
Fibonacci Series By using Recursion
• #include<stdio.h>
• int fibonacci(int x){
• int t1=0,t2=1,nextterm;
• if(x==0){
• printf("fibonacci series: %d",t1);
• }
• if(x==1){
• printf("fibbonacci series: %d %d",t1,t2);
• }
• printf("fibonacci series: %d %d ",t1,t2 );
• for(int i=2;i<x;i++){
• nextterm=t1+t2;
• printf("%d ",nextterm);
• t1=t2;
• t2=nextterm;
• }
• return nextterm;
• }
• int main (){
• int n;
• printf("enter the no of terms: ");
• scanf("%d",&n);
• fibonacci(n);
• }
Thank You
By:-Phool Babu

You might also like