C Programs With Examples and Assignment KCA102

C Programs With Examples


Note : Please refresh for new updates and please open in desktop mode in chrome or laptop for full codes ..                                                                                         

1. Write a program to print message on console window.
please open in desktop mode in chrome or laptop for full codes ..                                                                                 
#include <stdio.h>
#include <conio.h>
int main()
{
printf("Hello Mam");
return 0;
}

OUTPUT:
Hello Mam

2. write a program to perform arithmetic operations using to variables.
please open in desktop mode in chrome or laptop for full codes ..    

// C program to demonstrate syntax of binary arithmetic
// operators
#include <stdio.h>
#include <conio.h>

int main()
{
    int a = 10, b = 4, res;

    // printing a and b
    printf("a is %d and b is %d\n", a, b);

    res = a + b; // addition
    printf("sum is %d\n", res);

    res = a - b; // subtraction
    printf("sub is %d\n", res);

    res = a * b; // multiplication
    printf("mult is %d\n", res);

    res = a / b; // division
    printf("div is %d\n", res);

    res = a % b; // modulus
    printf("mod is %d\n", res);

    return 0;
}


OUTPUT:
a is 10 and b is 4
sum is 14
sub is 6
mult is 40
div is 2
mod is 2

3. Write a program to input 5 subjects marks,   calculate total or percentage of a student.
  please open in desktop mode in chrome or laptop for full codes ..    

// C Program To Find Total, Average, and Percentage of Five Subjects
#include <stdio.h>
#include <conio.h>
int main(){
    int eng, math, python, java, react;
    float total, average, percentage;
   
    // Asking for Input
    printf("Enter the marks of 5 sub: \n");
    scanf("%d %d %d %d %d",&eng,&math,&python,&java,&react);
   
    total = eng + math + python + java + react;
    average = total / 5;
    percentage = (total/500) * 100;
   
    printf("Total Marks of the Student: %f\n", total);
    printf("Average Marks of the Student: %f\n", average);
    printf("Percentage of the Student: %f\n", percentage);
    return 0;
}

OUTPUT:
Enter the marks of 5 sub:
5
5
5
5
5
Total Marks of the Student: 25.000000
Average Marks of the Student: 5.000000
Percentage of the Student: 5.000000

4. Write a program to calculate simple interest.
  please open in desktop mode in chrome or laptop for full codes ..    

#include<stdio.h>
#include<conio.h>
int main()  
    {  
        float P , R , T , SI ;  
       P =34000; R =30;  T = 5;  
        SI  = (P*R*T)/100;  
        printf("\n\n Simple Interest is : %f", SI);  
        return (0);  
    }  
OUTPUT:
 Simple Interest is : 51000.000000

5. Write a program find out the area of circle.
 please open in desktop mode in chrome or laptop for full codes ..    

#include <stdio.h>
#include<conio.h>
int main() {
   float pie = 3.14;
   int radius;
   printf("Enter The Radius of Circle:");
   scanf("%d",&radius);
   float area = (float)(pie* radius * radius);
   printf("The area of the given circle is %f", area);
   return 0;
}
OUTPUT:
Enter The Radius of Circle:34
The area of the given circle is 3629.840088

6. write a program to swap two numbers without using a third variable

#include<stdio.h>
#include<conio.h>
int main()  
{  
  int x, y;  
  printf("Enter the value of x and y:");  
  scanf("%d %d",&x,&y);  
  printf("before swapping numbers: %d   %d\n",x,y);  
 
/*swapping*/  
  x = x + y;  
  y = x - y;    
  x = x - y;  
  printf("After swapping: %d    %d",x,y);  
  return 0;  
}  
OUTPUT:
Enter the value of x and y:7 3
before swapping numbers: 7   3
After swapping: 3    7

7. write a program to swap two numbers using a third variable.

#include <stdio.h>
#include <conio.h>
int main()
{
    int x, y, temp;
    printf("Enter the value of x and y\n");
    scanf("%d%d", &x, &y);
    printf("Before Swapping\nx = %d\ny = %d\n", x, y);
  temp = x;
  x = y;
  y = temp;
    printf("After Swapping\nx = %d\ny = %d\n", x, y);
    return 0;
}
OUTPUT:
Enter the value of x and y
6 1
Before Swapping
x = 6
y = 1
After Swapping
x = 1
y = 6

8. write a program to check whether number is positive using if condition.
#include <stdio.h>
#include <conio.h>
int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    if (num > 0) {
printf("You entered a positive number.");
    }
    return 0;
}

OUTPUT:
Enter a number: 4
You entered a positive number.

9. write a program to check whether number is positive or negative using if else condition.
#include <stdio.h>
#include <conio.h>
int main() {

    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    if (num > 0) {
          printf("You entered a positive number.");    
    }
    else
        printf("You entered a negative number.");

    return 0;
}
OUTPUT:
Enter a number: -33
You entered a negative number.

10. write a program to check whether number is odd or even using if else condition.
#include <stdio.h>
#include <conio.h>
void main()
{
    int x, rem;
 
    printf("Enter an integer no: ");
    scanf("%d", &x);
    rem = x % 2;
    if (rem == 0)
        printf("%d is an even integer\n", x);
    else
        printf("%d is an odd integer\n", x);
}
OUTPUT:
Enter an integer : 33
33 is an odd integer

11 . Write a program to solve using this equation b²-4ac.
// Write a program to solve using this equation b²-4ac
#include <stdio.h>
#include <conio.h>
void main()
{
    int a,b,c;
 
    printf("Enter the values a,b and c ");
    scanf("%d %d %d", &a, &b, &c);
   int res = b*b-4*a*c;
   printf("Result of this equation b²-4ac:  %d",res);
   getch();
}
OUTPUT:
Enter the values a,b and c 2 9 2
Result of this equation b²-4ac:  65

12.Write a program to check greatest  number of two numbers using if else.
#include <stdio.h>
#include <conio.h>
int main ()
{
  int num1, num2;
  num1=5,num2=7;

  if (num1 > num2)
    printf("%d num1 is greater", num1);
  else
    printf("%d num2 is greater", num2);
    getch();
  return 0;
}
OUTPUT:
7 num2 is greater

13.Write a program to check greatest of three numbers using if else if.
#include <stdio.h>
#include <conio.h>
int main() {
  int n1, n2, n3;
  printf("Enter three different numbers: ");
  scanf("%d %d %d", &n1, &n2, &n3);
  // if n1 is greater than both n2 and n3, n1 is the largest
  if (n1 >= n2 && n1 >= n3)
    printf("%d n1 is the largest number.", n1);
  // if n2 is greater than both n1 and n3, n2 is the largest
  if (n2 >= n1 && n2 >= n3)
    printf("%d n2 is the largest number.", n2);
  // if n3 is greater than both n1 and n2, n3 is the largest
  else
    printf("%d n3 is the largest number.", n3);
getch();
  return 0;
}
OUTPUT:
Enter three different numbers: 5 3 7
7 n3 is the largest number.

14. Write a program to find the  grade base of student total marks percentage
(i) 75%+ Distinction"
(ii) 60%-74% first division
(iii) 50% - 59% second division
(iv) 40% -49%- Third division
(v) 40% → Fail
/* Find grade according to the percentage */
   #include<stdio.h>
   #include<conio.h>
   int main() {
    float per ;
    printf("Enter the percentage of student:");
    scanf("%f",&per);
    if(per >= 75)
    {
        printf("Distinction");
    }
    else if(per >= 60 && per <75)
    {
        printf("First Division");
    }
    else if(per >= 50 && per <60)
    {
        printf("Second Division");
    }
    else if(per >= 40 && per <50)
    {
        printf("Third Division");
    }
    else
    {
        printf("Fail");
    }
getch();
    return 0;
}
OUTPUT:
Enter the percentage of student:78
Distinction

15. Write a program to check given year is a leap year or not. 

// write a program to check given year is leap year or not

#include<stdio.h>
#include<conio.h>

int main() {
    int year;
printf("Enter the year:");
scanf("%d", &year);
if(((year%4 == 0) && ((year %400 == 0) || (year%100!=0))))
{
    printf("Year %d is a leap year",year);
}
else {
    printf("Year %d is not a leap year",year);
}
}

OUTPUT:
Enter the year:2024
Year 2024 is a leap year

16. Write a program to given value is positive or negative using nested if.

// writer a program to check given value is positive or negative or zero
#include<stdio.h>
#include<conio.h>

int main() {
int num;
printf("Enter the number:");
scanf("%d", &num);
if(num==0){
    printf("%d is zero",num);
}
else if(num>0){
    printf("%d is a positive number",num);
}
else if(num<0){
    printf("%d is a negative number",num);
}
return 0;
}

OUTPUT:
Enter the number:15
15 is a positive number

17. Write a program to show student’s performance based on grades using switch case.

#include <stdio.h>

int main() {
    char grade;

    // Prompt user to enter the grade
    printf("Enter your grade: ");
    scanf(" %c", &grade);

    // Check grade using switch-case
    switch (grade) {
        case 'A':
            printf("You got first position in the class.\n");
            break;
        case 'B':
            printf("You got second position in the class.\n");
            break;
             case 'C':
            printf("You got third position in the class.\n");
            break;
        case 'D':
            printf("You got last position in the class.\n");
            break;
            case 'a':
            printf("You got first position in the class.\n");
            break;
        case 'b':
            printf("You got second position in the class.\n");
            break;
             case 'c':
            printf("You got third position in the class.\n");
            break;
        case 'd':
            printf("You got last position in the class.\n");
            break;
        default:
            printf("Invalid grade.\n");
            break;
    }

    return 0;
}

OUTPUT :

Enter your grade: a

You got first position in the class.

18. Write a program to check vowel or consonant using switch case.

// wap th check vowel or consonant using switch case statements.
#include<stdio.h>
#include<conio.h>

void main() {
char check;
printf("Enter the charater :");
scanf("%c", &check);
switch (check)
{
case 'a':
printf("Vowel");
    break;
case 'e':
printf("Vowel");
    break;
case 'i':
printf("Vowel");
    break;
case 'o':
printf("Vowel");
    break;
case 'u':
printf("Vowel");
    break;
case 'A':
printf("Vowel");
    break;
case 'E':
printf("Vowel");
    break;
case 'I':
printf("Vowel");
    break;
case 'O':
printf("Vowel");
    break;
case 'U':
printf("Vowel");
    break;
default:
printf("Consonant");
    break;
}
}

OUTPUT :

Enter the charater :A

Vowel

19. Write a program to print “Hello World” 10 times on console using while loop.

// wap to print Hello World 10 times using while loop.
#include<stdio.h>
#include<conio.h>

int main() {
    int a=0;
while (a<10)
{
   printf("Hello World: %d\n",a);
   a++;
}
return 0;
}

OUTPUT :

Hello World: 0

Hello World: 1

Hello World: 2

Hello World: 3

Hello World: 4

Hello World: 5

Hello World: 6

Hello World: 7

Hello World: 8

Hello World: 9

20. Write a program to 1 to 10 numbers using while loop.

// wap to print count 1 to 10 using while loop.
#include<stdio.h>
#include<conio.h>

int main() {
    int a=1;
while (a<=10)
{
   printf(" %d\n",a);
   a++;
}
return 0;
}

OUTPUT :

1

 2

 3

 4

 5

 6

 7

 8

 9

 10

21. Write a program to print numbers 1 to 10 using for loop.

// wap to print count 1 to 10  using for loop.
#include<stdio.h>
#include<conio.h>

int main() {
for(int i=1; i<=10; i++){
    printf("%d\n",i);
}
return 0;
}

OUTPUT :

1

 2

 3

 4

 5

 6

 7

 8

 9

 10

22. Write a program to print numbers 10 to 1 reverse using for loop.

// wap to print count 10 to 1 reverse  using for loop.
#include<stdio.h>
#include<conio.h>

int main() {
for(int i=10; i>=1; i--){
    printf("%d\n",i);
}
return 0;
}

OUTPUT :

10

9

8

7

6

5

4

3

2

1

23. Write a program to 10 to 1 numbers reverse using while loop.

// wap to print count 10 to 1 reverse  using while loop.
#include<stdio.h>
#include<conio.h>

int main() {
    int num=10;
while (num>=1)
{
 printf("%d\n",num);
 num--;
}
return 0;
}

OUTPUT :

10

9

8

7

6

5

4

3

2

1

24. Write a program to find all odd or even numbers till 50 using for loop.

// wap to find out all odd and even numbers till 50 using for loop

#include<stdio.h>
#include<conio.h>

int main() {
    // printing even number till 50
for(int i=1; i<=50; i++){
  if(i%2==0){
  printf("Even num is:: %d\n",i);
  }
}
// printing odd number till 50
for(int i=1; i<=50; i++){
  if(i%2!=0){
  printf("Odd num is:: %d\n",i);
  }
}
return 0;
}

OUTPUT :

Even num is:: 2

Even num is:: 4

Even num is:: 6

Even num is:: 8

Even num is:: 10

Even num is:: 12

Even num is:: 14

Even num is:: 16

Even num is:: 18

Even num is:: 20

Even num is:: 22

Even num is:: 24

Even num is:: 26

Even num is:: 28

Even num is:: 30

Even num is:: 32

Even num is:: 34

Even num is:: 36

Even num is:: 38

Even num is:: 40

Even num is:: 42

Even num is:: 44

Even num is:: 46

Even num is:: 48

Even num is:: 50

Odd num is:: 1

Odd num is:: 3

Odd num is:: 5

Odd num is:: 7

Odd num is:: 9

Odd num is:: 11

Odd num is:: 13

Odd num is:: 15

Odd num is:: 17

Odd num is:: 19

Odd num is:: 21

Odd num is:: 23

Odd num is:: 25

Odd num is:: 27

Odd num is:: 29

Odd num is:: 31

Odd num is:: 33

Odd num is:: 35

Odd num is:: 37

Odd num is:: 39

Odd num is:: 41

Odd num is:: 43

Odd num is:: 45

Odd num is:: 47

Odd num is:: 49

25. Write a program to break a loop.

#include<stdio.h>  
int main(){  
int i=1;//initializing a local variable      
//starting a loop from 1 to 10    
for(i=1;i<=10;i++){
    printf("%d \n",i);    
if(i==4)//if value of i is equal to 5, it will continue the loop    
break;    
   
}//end of for loop    
return 0;  
}  

OUTPUT:

3

4

26. Write a program to use skip a value of loop by using continue data type.

#include<stdio.h>  
int main(){  
int i=1;//initializing a local variable      
//starting a loop from 1 to 10    
for(i=1;i<=10;i++){      
if(i==5){//if value of i is equal to 5, it will continue the loop    
continue;    
}    
printf("%d \n",i);    
}//end of for loop    
return 0;  
}  

OUTPUT :

3

4

6

7

8

9

10

27. Write a program to print the first value of 1d array.

// wap to create a 1D array and print the array first indexing [0] value
#include<stdio.h>
#include<conio.h>
int main() {
    int arr[3]={9,3,5};
  printf("Array first indexing value: %d\n",arr[0]);
  return 0;
}

OUTPUT :

Array first indexing value: 9

28. Write a program to print the 1d array’s values on console.

// wap to create a 1D array and print the all indexing values
#include<stdio.h>
#include<conio.h>
int main() {
    int arr[3]={9,3,5};
    for (int i = 0; i<3; i++)
    {
         printf("Array arr[%d] values: %d\n",i,arr[i]);

    }
   
  return 0;
}

OUTPUT :

Array arr[0] values: 9

Array arr[1] values: 3

Array arr[2] values: 5

29. Write a program to create 2d array and print all the array’s values on console.

// wap to create a 2D array and print the all indexing values
#include<stdio.h>
#include<conio.h>
int main() {
    int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
    for (int i = 0; i<4; i++){
    for (int j = 0; j<3; j++){
   printf("Array arr[%d][%d] values: %d\n",i,j,arr[i][j]);
    }
    }
  return 0;
}

OUTPUT :

Array arr[0][0] values: 1

Array arr[0][1] values: 2

Array arr[0][2] values: 3

Array arr[1][0] values: 2

Array arr[1][1] values: 3

Array arr[1][2] values: 4

Array arr[2][0] values: 3

Array arr[2][1] values: 4

Array arr[2][2] values: 5

Array arr[3][0] values: 4

Array arr[3][1] values: 5

Array arr[3][2] values: 6

30. Write program to print any value of 2d array on console.

// wap to create a 2D array and print any index of value
#include<stdio.h>
#include<conio.h>
int main() {
    int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
 
         printf("Array arr[1][2] values: %d\n",arr[1][2]);
  return 0;
}

OUTPUT :

Array arr[1][2] values: 4

31. Write a program to add two numbers using functions.

// wap to add two numbers using function
#include<stdio.h>
#include<conio.h>

// function declaration
int addTwoNum(int num1, int num2);

// function definition
int addTwoNum(int num1, int num2){
// int sum = num1 + num2;
return num1 + num2;
}
int main() {
    int a,b,c;
printf("Enter the two numbers for sum: ");
scanf("%d %d",&a,&b);

// function calls
c=addTwoNum(a,b);
printf("Sum of two numbers: %d ",c);
return 0;
}

OUTPUT :

Enter the two numbers for sum: 15 32 

Sum of two numbers: 47 

32. Write a program to find factorial of a number by making a function.

// wap to find factorial of any number using function
#include<stdio.h>
#include<conio.h>

// function declaration
int factorial(int num);

// function definition
int factorial(int num){
int factorial = 1;
for (int i=1; i<=num; i++)
{
   factorial= factorial*i;
}
return factorial;
}
int main() {
    int a,b;
printf("Enter the numbers which you want factorial: ");
scanf("%d",&a);

// function calls
b=factorial(a);
printf("factorial of %d number : %d ",a,b);
return 0;
}

OUTPUT :

Enter the numbers which you want factorial: 5

factorial of 5 number : 120 

33. Write a program to swap two numbers by making a function. 

#include <stdio.h>

void swap (int *n1, int *n2);

void swap (int *n1, int *n2)
{
  // int temp;
  // temp = *n2;
  // *n2 = *n1;
  // *n1 = temp;
  *n1= *n1+*n2;
  *n2 =*n1-*n2;
  *n1 =*n1-*n2;
}

void main ()
{
  /* Declaration of variables used within this program. */
  int num1, num2;

  /* Take input from the user and save it to the first variable. */
  printf("Enter first number:\n");
  scanf ("%d", &num1);

  /* Take input from the user and save it to the second variable. */
  printf("Enter second number:\n");
  scanf ("%d", &num2);

  /* Printing the use input before swapping. */
  printf("\nBefore Swapping\n");
  printf("First number: %d\n", num1);
  printf("Second number: %d\n\n", num2);

  /* Swapping the variables. */
  swap (&num1, &num2);

  /* Printing the variables after the swapping. */
  printf("\nAfter Swapping\n");
  printf("First number: %d\n", num1);
  printf("Second number: %d\n\n", num2);
}

OUTPUT :

Enter first number:

12

Enter second number:

32

Before Swapping

First number: 12

Second number: 32

After Swapping

First number: 32

Second number: 12

34. Write a program to calculate area of a square using function. 

// wap to calculate the area of square using function.
#include<stdio.h>

int area_of_square(int);

int area_of_square(int s) {
int as=s*s;
return as;
}

void main(){
    // as is area of square and s is side
    int s;
    printf("Enter the side to find the area of square:");
    scanf("%d",&s);
    printf("%d",area_of_square(s));
}

OUTPUT:
Enter the side to find the area of square:5
25

35. Write a program to calculate the average of five numbers using function without return value.

// wap to calculate the average of five num using function with retun
// value and argument.
#include<stdio.h>
void  average(int a,int b,int c,int d,int e);

void average(int a,int b,int c,int d,int e){
    int sum = a+b+c+d+e;
    float avg=(float)sum/5;
  printf("average of the 5 no:%f",avg);
}

void main(){
    int a,b,c,d,e;
    printf("Enter the five no to find the average:");
    scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
    average(a,b,c,d,e);

}

OUTPUT:

Enter the five no to find the average:8 4 9 4 2

average of the 5 no:5.400000

36. Write a program to check whether a number is even or odd using function with return value and argument.

//  wap to check whether a number is even or odd using function
// with return value and argument .
#include<stdio.h>
char* check_odd_even(int num);
char* check_odd_even(int num){
if(num%2==0){
return "even";
}
else{
    return "odd";
}
}

void main(){
    int num;
    printf("Enter the num:");
    scanf("%d",&num);
printf("num is : %s",check_odd_even(num));
}

OUTPUT:

Enter the num:37

num is : odd

37. Write a program to adding value in variable using function and call by value.

#include<stdio.h>  
void change(int num) {    
printf("Before adding value inside function num=%d \n",num);    
    num=num+100;    
printf("After adding value inside function num=%d \n", num);    
}    
int main() {    
    int x=100;    
    printf("Before function call x=%d \n", x);    
    change(x);//passing value in function    
    printf("After function call x=%d \n", x);    
return 0;  
}  

OUTPUT:

Before function call x=100 

Before adding value inside function num=100 

After adding value inside function num=200 

After function call x=100 

38. Write a program to swap two numbers using function and call by value.

#include <stdio.h>  
void swap(int , int); //prototype of the function  
int main()  
{  
    int a = 10;  
    int b = 20;  
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
// printing the value of a and b in main  
    swap(a,b);  
// The value of actual parameters do not change by changing
// the formal parameters in call by value, a = 10, b = 20
printf("After swapping values in main a = %d, b = %d\n",a,b);  
}  
void swap (int a, int b)  
{  
    int temp;  
    temp = a;  
    a=b;  
    b=temp;  
// Formal parameters, a = 20, b = 10  
printf("After swapping values in function a = %d, b = %d\n",a,b);
}  

OUTPUT:

Before swapping the values in main a = 10, b = 20

After swapping values in function a = 20, b = 10

After swapping values in main a = 10, b = 20

39. Write a program to adding value in variable using function and call by reference.

#include<stdio.h>  
void change(int *num) {    
printf("Before adding value inside function num=%d \n",*num);    
    (*num) += 100;    
printf("After adding value inside function num=%d \n", *num);    
}      
int main() {    
    int x=100;    
    printf("Before function call x=%d \n", x);    
    change(&x);//passing reference in function    
    printf("After function call x=%d \n", x);    
return 0;  
}    

OUTPUT :

Before function call x=100
Before adding value inside function num=100
After adding value inside function num=200
After function call x=200

40. Write a program to swap two numbers using function and call by reference.

#include <stdio.h>  
void swap(int *, int *); //prototype of the function  
int main()  
{  
    int a = 10;  
    int b = 20;
// printing the value of a and b in main    
    printf("Before swapping the values in main a = %d, b = %d\n",a,b);
    swap(&a,&b);  
// The values of actual parameters do change in call by reference, a = 10, b = 20  
    printf("After swapping values in main a = %d, b = %d\n",a,b);
}  
void swap (int *a, int *b)  
{  
    int temp;  
    temp = *a;  
    *a=*b;  
    *b=temp;  
// Formal parameters, a = 20, b = 10  
    printf("After swapping values in function a = %d, b = %d\n",*a,*b);
}  

OUTPUT :

Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 20, b = 10 

41. Write a program to find maximum number between two numbers using function.

/**
 * C program to find maximum between two numbers
 */

#include <stdio.h>

int main()
{
    int num1, num2;

    /* Input two numbers from user */
    printf("Enter two numbers: ");
    scanf("%d%d", &num1, &num2);

    /* If num1 is maximum */
    if(num1 > num2)
    {
        printf("%d is maximum", num1);        
    }

    /* If num2 is maximum */
    if(num2 > num1)
    {
        printf("%d is maximum", num2);
    }

    /* Additional condition check for equality */
    if(num1 == num2)
    {
        printf("Both are equal");
    }

    return 0;
}

OUTPUT :

Enter two numbers: 14

14

Both are equal

42. Write a program to find given number’s factorial using function recursion.

#include<stdio.h>
int rec(int num){
    if(num >= 1){
        return num*rec(num-1);
    }
    else{
    return 1;
    }
}
void main(){
int num;
printf("Enter the number:");
scanf("%d",&num);
int rc=rec(num);
printf("%d",rc);
}

OUTPUT:

Enter the number:7

5040

43. Write a program to print the address and value of a variable using pointer.

#include <stdio.h>

int main() {
  int a;
  int *pt;

  printf("Pointer Example Program : Print Pointer Address\n");
  a = 10;
  pt = &a;

  printf("\n[a  ]:Value of A = %d", a);
  printf("\n[*pt]:Value of A = %d", *pt);
  printf("\n[&a ]:Address of A = %p", &a);
  printf("\n[pt ]:Address of A = %p", pt);
  printf("\n[&pt]:Address of pt = %p", &pt);
  printf("\n[pt ]:Value of pt = %p", pt);
 
  return 0;
}

OUTPUT :

Pointer Example Program : Print Pointer Address


[a  ]:Value of A = 10

[*pt]:Value of A = 10

[&a ]:Address of A = 0061FF1C

[pt ]:Address of A = 0061FF1C

[&pt]:Address of pt = 0061FF18

[pt ]:Value of pt = 0061FF1C

44. Pointer Program to swap two numbers without using the 3rd variable.

#include<stdio.h>
void main(){
    int a=5,b=4;
    int* c=&a;
     int* d=&b;
    printf("Before swap a:%d,b:%d",a,b);
    int tem=*d;
    *d=*c;
    *c=tem;
     printf("\nAfter swap a:%d,b:%d",a,b);
}

OUTPUT :

Before swap a:5,b:4

After swap a:4,b:5

45. Write a program to print address and value of two variables using double pointers.

// C program to demonstrate pointer to pointer
#include <stdio.h>

int main()
{
    int var = 789;

    // pointer for var
    int* ptr2;

    // double pointer for ptr2
    int** ptr1;

    // storing address of var in ptr2
    ptr2 = &var;

    // Storing address of ptr2 in ptr1
    ptr1 = &ptr2;

    // Displaying value of var using
    // both single and double pointers
    printf("Value of var = %d\n", var);
    printf("Value of var using single pointer = %d\n", *ptr2);
    printf("Value of var using double pointer = %d\n", **ptr1);

    return 0;
}

OUTPUT :

Value of var = 789

Value of var using single pointer = 789

Value of var using double pointer = 789

46. Write a program to make string array and print the array’s values.

#include<stdio.h>  
int main()  
{  
    char str[8];  
    printf("Enter a String: ");  
    scanf("%s", &str);  
    printf("%s", str);  
}  

OUTPUT :

Enter a String: CODEWITHRAVIKANT

CODEWITHRAVIKANT

47. Write a program to copy to string, add two string and find the length of string using string’s functions.

#include<stdio.h>
#include<string.h>
void main(){
    char str1[100]="hello",str2[100],result[200]="";
    // strcpy(str1,"hello");
    strcpy(str2,str1);
        printf("add two string in result:%s",str2);
    strcat(result,str1);
    strcat(result,str2);
    printf("\nadd two string in result: %s",result);
    printf("\nprint length of result string:%d",strlen(result));
}

OUTPUT:

add two string in result:hello

add two string in result: hellohello

print length of result string:10

48. Write a program to make two book structures book 1 and book 2, where structures have title, author, subject and id just copy values of these to book 2 structure and print them.

#include<stdio.h>
#include<string.h>
struct Book
{
int id;
char subject[100];
char author[100];
char tittle[100];
};

void main(){
    struct Book book1,book2;

    book1.id=123;
    strcpy(book1.tittle,"Coding");
    strcpy(book1.subject,"C-Programming");
    strcpy(book1.author,"Dennis Ritchi e");
    // copy structure
    book2=book1;

    printf("Id:%d",book2.id);
    printf("\nTittle:%s",book2.tittle);
    printf("\nSubject:%s",book2.subject);
    printf("\nAuthor:%s",book2.author);
   
}

Output:

Id:123

Tittle:Coding

Subject:C-Programming

Author:Dennis Ritchie

49. Write a program to make a union.

// C Program to demonstrate how to use union
#include <stdio.h>

// union template or declaration
union un {
    int member1;
    char member2;
    float member3;
};

// driver code
int main()
{

    // defining a union variable
    union un var1;

    // initializing the union member
    var1.member1 = 15;

    printf("The value stored in member1 = %d",
        var1.member1);

    return 0;
}

OUTPUT:

The value stored in member1 = 15

50. Write a program to create&read a file.

#include<stdio.h>
#include<stdlib.h>
void main(){
    FILE* fptr;

    fptr = fopen("coding.c","r");
    char mystring[100];

    if (fptr != NULL)
    {
        printf("This file is open now\n");
            while(fgets(mystring,100,fptr)){
            printf("%s",mystring);
            }
         exit(0);
    }
    else{
                printf("this file is not open now");
               
    }
   
}

OUTPUT:

This file is open now

Hello world

51. Write a program to write to a file.

#include<stdio.h>
#include<stdlib.h>
void main(){
    FILE* fptr;

    fptr = fopen("coding.txt","w");

    if (fptr != NULL)
    {
        printf("This file is open now\n");
          fprintf(fptr,"HELLO WORLD");
         exit(0);
    }
    else{
                printf("this file is not open now");
               
    }
   
}

OUTPUT:

This file is open now

52. Write a program check a number if Armstrong number.

#include<stdio.h>  
 int main()    
{    
int n,r,sum=0,temp;    
printf("enter the number=");    
scanf("%d",&n);    
temp=n;    
while(n>0)    
{    
r=n%10;    
sum=sum+(r*r*r);    
n=n/10;    
}    
if(temp==sum)    
printf("armstrong  number ");    
else    
printf("not armstrong number");    
return 0;  
}  

OUTPUT:

enter the number=153

armstrong  number 

53. Write a program to make a Fibonacci series.

#include<stdio.h>    
int main()    
{    
 int n1=0,n2=1,n3,i,number;    
 printf("Enter the number of elements:");    
 scanf("%d",&number);    
 printf("\n%d %d",n1,n2);//printing 0 and 1    
 for(i=2;i<number;++i)//loop starts from 2 because 0 and 1 are already printed    
 {    
  n3=n1+n2;    
  printf(" %d",n3);    
  n1=n2;    
  n2=n3;    
 }  
  return 0;  
 }    

OUTPUT:

Enter the number of elements:15

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

54. Write a program to draw basic graphics construction like line, circle, arc, ellipse and rectangle. 

#include<graphics.h>  
#include<stdio.h>
#include<conio.h>  
void main()  
{  
    intgd=DETECT,gm;  
    initgraph (&gd,&gm,"c:\\tc\\bgi");  
    setbkcolor(GREEN);  
    printf("\t\t\t\n\nLINE");  
    line(50,40,190,40);  
    printf("\t\t\n\n\n\nRECTANGLE");  
    rectangle(125,115,215,165);  
    printf("\t\t\t\n\n\n\n\n\n\nARC");  
    arc(120,200,180,0,30);  
    printf("\t\n\n\n\nCIRCLE");  
    circle(120,270,30);  
    printf("\t\n\n\n\nECLIPSE");  
    ellipse(120,350,0,360,30,20);  
    getch();  
}  

OUTPUT :
Computer Graphics Programs
wait for more.......

Post a Comment

Previous Post Next Post