SIMPLE C PROGRAM

Write a programe to print hellow world.

Method 1: Printing Hello World in the main() Function

/*

 * C program to print “Hello World”

 */

 

#include <stdio.h>

 

//main function

int main()

{

    //print Hello World

    printf("Hello World");

 

    return 0;

}

Hello World

Method 2: Printing Hello World in C using Function

/*

 * C program to print “Hello World” using function.

 */

 

#include <stdio.h>

 

//function print() to print Hello World

void print()

{

    printf("Hello World");

}

 

//main function

void main()

{

    print(); // calling the function print()

}

Program Output

Hello World

 

WAP to print given number is even or odd.

Method 1: (Using if-else and Modulus Operator)

/*

 * C Program to check whether a number is even or odd

 * using if-else statement and modulus operator

 */

 

#include <stdio.h>

void main()

{

    int n;

    printf("Enter a number: ");

    scanf("%d", &n);

 

    if(n % 2 == 0)

        printf("%d is even number.", n);

    else

        printf("%d is odd number.", n);

}

Runtime Test Cases

Testcase 1: Here is the runtime output of the C program to check whether a number is even or odd when the user enters number = “22”.

Enter a number: 22

22 is even number.

Testcase 2: Here is the runtime output of the C program to check whether a number is even or odd when the user enters number = “15”.

Enter a number: 15

15 is odd number.

Method 2: (Using Ternary Operator)

/*

 * C Program to check whether a number is even or odd using ternary operator

 */

 

#include <stdio.h>

void main()

{

    int n;

    printf("Enter an integer: ");

    scanf("%d", &n);

 

    (n % 2 == 0) ? printf("%d is even number.", n) : printf("%d is odd number.", n);

}

Runtime Test Cases

Testcase 1: Here is the runtime output of the C program to check whether a number is even or odd when the user enters number = “37”.

Enter a number: 37

37 is odd number.

Testcase 2: Here is the runtime output of the C program to check whether a number is even or odd when the user enters number = “18”.

Enter a number: 18

18 is even number.

Method 3: (Using Bitwise Operator)

/*

 * C Program to check whether a number is even or odd using bitwise operator

 */

 

#include <stdio.h>

void main()

{

    int n;

    printf("Enter a number: ");

    scanf("%d", &n);

 

    if(n & 1==1)

        printf("%d is odd.", n);

    else

        printf("%d is even.", n);

}

Runtime Test Cases

Testcase 1: Here is the runtime output of the C program to check whether a number is even or odd when the user enters number = “7”.

Enter a number: 7

7 is odd number.

Testcase 2: Here is the runtime output of the C program to check whether a number is even or odd when the user enters number = “22”.

Enter a number: 22

22 is even number.

C Program to Find the Sum of Even and Odd Numbers

#include <stdio.h>

 

void main()

{

    int i, num, odd_sum = 0, even_sum = 0;

 

    printf("Enter the value of num\n");

    scanf("%d", &num);

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

    {

        if (i % 2 == 0)

            even_sum = even_sum + i;

        else

            odd_sum = odd_sum + i;

    }

    printf("Sum of all odd numbers  = %d\n", odd_sum);

    printf("Sum of all even numbers = %d\n", even_sum);

}

Runtime Test Cases

Case 1:

Enter the value of num

10

Sum of all odd numbers  = 25

Sum of all even numbers = 30

 

Case 2:

Enter the value of num

100

Sum of all odd numbers  = 2500

Sum of all even numbers = 2550

C program to Check Whether a Number is Positive or Negative

#include <stdio.h>

 

void main()

{

    int number;

 

    printf("Enter a number \n");

    scanf("%d", &number);

    if (number >= 0)

        printf("%d is a positive number \n", number);

    else

        printf("%d is a negative number \n", number);

}

Runtime Test Cases

Case:1

Enter a number

-10

-10 is a negative number

 

Case:2

Enter a number

45

45 is a positive number

C Program to Find the Largest Number Among Three Numbers

Method 1: (Using If Statement)

/*

 * C program to find the largest  of three numbers

 */

 

#include <stdio.h>

 

int main()

{

    int a, b, c;

    printf("Enter three numbers: \na: ");

    scanf("%d", &a);

    printf("b: ");

    scanf("%d", &b);

    printf("c: ");

    scanf("%d", &c);

    if (a > b && a > c)

        printf("Biggest number is %d", a);

    if (b > a && b > c)

        printf("Biggest number is %d", b);

    if (c > a && c > b)

        printf("Biggest number is %d", c);

    return 0;

}

Runtime Test Cases

Testcase 1: In this case, we enter the values ​​”6, 8 and 10” as input to find the largest of the three given numbers.

Enter three numbers:

a: 6

b: 8

c: 10

Biggest number is 10

Testcase 2: In this case, we enter the values ​​”10, 99 and 87” as input to find the largest of the three given numbers.

Enter three numbers:

a: 10

b: 99

c: 87

Biggest number is 99

Method 2: (Using If-else Statement)

/*

 * C program to find the biggest of three numbers using if else statement

 */

 

#include <stdio.h>

 

void main()

{

    int num1, num2, num3;

 

    printf("Enter the values of num1, num2 and num3\n");

    scanf("%d %d %d", &num1, &num2, &num3);

    printf("num1 = %d\tnum2 = %d\tnum3 = %d\n", num1, num2, num3);

    if (num1 > num2)

    {

        if (num1 > num3)

        {

            printf("%d is the largest number.", num1);

        }

        else

        {

            printf("%d is the largest number.", num3);

        }

    }

    else if (num2 > num3)

        printf("%d is the largest number.", num2);

    else

        printf("%d is the largest number.", num3);

}

Runtime Test Cases

Testcase 1: In this case, we enter the values ​​”65“, “34” and “25” as input to find the largest of the three given numbers.

Enter the values of num1, num2 and num3

65

34

25

num1 = 65  num2 = 34  num3 = 25

65 is the largest number.

Testcase 2: In this case, we enter the values ​​”8“, “4“, and “19” as input to find the largest of the three given numbers.

Enter the values of num1, num2 and num3

8

4

19

num1 = 8  num2 = 4  num3 = 19

19 is the largest number.

Method 3: (Using Ternary Operator)

/*

 * C program to find the biggest of three numbers using ternary operator

 */

 

#include <stdio.h>

 

int main(void)

{

    int a, b, c;

    printf("Enter three numbers: \na: ");

    scanf("%d", &a);

    printf("b: ");

    scanf("%d", &b);

    printf("c: ");

    scanf("%d", &c);

    printf("Largest of three numbers is %d", a > b ? (a > c ? a : c) : (b > c ? b : c));

    return 0;

}

Runtime Test Cases

Testcase 1: In this case, we enter the values ​​”1“, “9” and “37” as input to find the largest of the three given numbers.

Enter three numbers:

a: 1

b: 9

c: 37

Largest of three numbers is 37

Testcase 2: In this case, we enter the values ​​”22“, “14“, and “51” as input to find the largest of the three given numbers.

Enter three numbers:

a: 22

b: 14

c: 51

Largest of three numbers is 51

Method 4: (Using Function)

/*

 * C program to find the biggest of three numbers using function

 */

 

#include <stdio.h>

 

int biggest(int a, int b)

{

    if (a > b)

        return a;

    return b;

}

 

int main(void)

{

    int a, b, c;

    printf("Enter three numbers: \na: ");

    scanf("%d", &a);

    printf("b: ");

    scanf("%d", &b);

    printf("c: ");

    scanf("%d", &c);

    printf("%d is the biggest of all three numbers.\n", biggest(biggest(a, b), c));

    return 0;

}

Runtime Test Cases

Testcase 1: In this case, we enter the values ​​”99“, “132” and “88” as input to find the largest of the three given numbers.

Enter three numbers:

a: 99

b: 132

c: 88

132 is the biggest of all three numbers.

Testcase 2: In this case, we enter the values ​​”29320“, “41332“, and “42393” as input to find the largest of the three given numbers.

Enter three numbers:

a: 29320

b: 41332

c: 42393

42393 is the biggest of all three numbers.

C Program to Swap Two Numbers

Method 1: Swap Two Numbers using Naive Approach

/*

* C program to read two integers M and N and to swap their values.

* Use a user-defined function for swapping by accepting the addresses of the two variables.

* Output the values of M and N before and after swapping.

*/

 

#include <stdio.h>

 

/*  Function swap - to interchanges the contents of two items */

 

void swap(float *ptr1, float *ptr2)

{

    // Step 4. Create a temporary variable for storing the values

    float temp;

    temp = *ptr1;

    *ptr1 = *ptr2;

    *ptr2 = temp;

}

 

int main(void)

{

    float m, n;

    // Step 1. Take user input

    printf("Enter the value of M (accepted decimal values): ");

    scanf("%f", &m);

    printf("Enter the value of N (accepted decimal values): ");

    scanf("%f", &n);

    // Step 2. Show the values before passing their addresses to the function

    printf("Before swapping : M = %5.2f\tN = %5.2f\n", m, n);

    // Step 3. Pass the addresses to the function

    swap(&m, &n);

    // Step 5. Print the values after the swap function has executed

    printf("After swapping : M  = %5.2f\tN = %5.2f\n", m, n);

}

Run Time Testcases

Testcase 1: Here, we are entering the integers 13 and 12 as input.

 

Enter the value of M (accepted decimal values): 13

Enter the value of N (accepted decimal values): 12

Before swapping : M = 13.00    N = 12.00

After swapping : M  = 12.00    N = 13.00

Testcase 2: Here, we are entering decimal values as input.

Enter the value of M (accepted decimal values): 4324.14311

Enter the value of N (accepted decimal values): 32331.5342

Before swapping : M = 4324.14    N = 32331.54

After swapping : M  = 32331.54    N = 4324.14

Method 2: Swap Two Numbers without using any Temporary Variable

}/*

 * C Program to Swap two Integers without using Temporary Variables

 * and Bitwise Operations

 */

#include <stdio.h>

 

// function to swap the two numbers

void swap(float *ptr1, float *ptr2)

{

    // Step 4. As ptr1 gets the sum of both, in the next step obviously ptr2

    // gets the difference of the sum and ptr2 which is ptr1

    *ptr1 = *ptr1 + *ptr2;

    *ptr2 = *ptr1 - *ptr2;

    *ptr1 = *ptr1 - *ptr2;

}

 

int main(void)

{

    float m, n;

    // Step 1. Take user input

    printf("Enter the value of M (accepted decimal values): ");

    scanf("%f", &m);

    printf("Enter the value of N (accepted decimal values): ");

    scanf("%f", &n);

    // Step 2. Show the values before passing their addresses to the function

    printf("Before swapping : \t M = %5.2f\tN = %5.2f\n", m, n);

    // Step 3. Pass the addresses to the function

    swap(&m, &n);

    // Step 5. Print the values after the swap function has executed

    printf("After swapping : \t M  = %5.2f\tN = %5.2f\n", m, n);

}

Run Time Testcases

Testcase 1: Here, we are entering 436.687 and 76.98 as input.

Enter the value of M (accepted decimal values): 436.687

Enter the value of N (accepted decimal values): 76.98

Before swapping :     M = 436.69    N = 76.98

After swapping :     M  = 76.98    N = 436.69

Testcase 2: Here, we are entering 34.4523 and 213.0004 as input.

Enter the value of M (accepted decimal values): 34.4523

Enter the value of N (accepted decimal values): 213.0004

Before swapping :     M = 34.45    N = 213.00

After swapping :     M  = 213.00    N = 34.45

Method 3: Swap Two Numbers using Bitwise XOR

/*

* C program to read two integers M and N and to swap their values.

* Use a user-defined function for swapping by accepting the addresses of the two variables.

* Output the values of M and N before and after swapping.

*/

 

#include <stdio.h>

 

/*  Function swap - to interchanges the contents of two items */

 

void swap(long *ptr1, long *ptr2)

{

    // Step 4. Performing XOR operation on the values

    *ptr1 = *ptr1 ^ *ptr2;

    *ptr2 = *ptr1 ^ *ptr2;

    *ptr1 = *ptr1 ^ *ptr2;

}

 

int main(void)

{

    long m, n;

    // Step 1. Take user input

    printf("Enter the value of M (accepted decimal values): ");

    scanf("%ld", &m);

    printf("Enter the value of N (accepted decimal values): ");

    scanf("%ld", &n);

    // Step 2. Show the values before passing their addresses to the function

    printf("Before swapping : M = %5ld\tN = %5ld\n", m, n);

    // Step 3. Pass the addresses to the function

    swap(&m, &n);

    // Step 5. Print the values after the swap function has executed

    printf("After swapping : M  = %5ld\tN = %5ld\n", m, n);

}

Run Time Testcases

Enter the value of M (accepted decimal values): 4234145445

Enter the value of N (accepted decimal values): 3241234534

Before swapping : M = 4234145445    N = 3241234534

After swapping : M  = 3241234534    N = 4234145445

Method 4: Swap Two Numbers without using Temporary Variables or Arithmetic Operator

/*

 * C program to swap the contents of two numbers using bitwise XOR operation.

 * Don't use either the temporary variable or arithmetic operators.

 */

#include <stdio.h>

 

void main()

{

    long i, k;

 

    printf("Enter two integers \n");

    scanf("%ld %ld", &i, &k);

    printf("\n Before swapping i= %ld and k = %ld", i, k);

    i = i ^ k;

    k = i ^ k;

    i = i ^ k;

    printf("\n After swapping i= %ld and k = %ld", i, k);

}

Runtime Test Cases

 

Enter two integers

45

89

 

Before swapping i= 45 and k = 89

After swapping i= 89 and k = 45

C Program that Takes Input as 2323 and Gives Output as 2332

/*
 * C program that takes input as 2323 and gives output as 2332. 
 * ie.the new number should be greater than the previous number
 * but should have the same digits
 */
#include <stdio.h>
#include <math.h>
 
int evaluate(int [], int);
int find(int);
 
int main()
{
    int num, result;
 
    printf("Enter a number: ");
    scanf("%d", &num);
    result = find(num);
    if (result)
    {
        printf("The number greater than %d and made of same digits is %d.\n", num, result);
    }
    else
    {
        printf("No higher value possible. Either all numbers are same or the digits of the numbers entered are in decreasing order.\n");
    }
 
    return 0;
}
 
int find(int num)
{
    int digit[20];
    int i = 0, len = 0, n, temp;
 
    n = num;
    while (n != 0)
    {
        digit[i] = n % 10;
        n = n / 10;
        i++;
    }
    len = i;
    for (i = 0; i < len - 1; i++)
    {
        if (digit[i] > digit[i + 1])
        {
            temp = digit[i];
            digit[i] = digit[i + 1];
            digit[i + 1] = temp;
 
            return (evaluate(digit, len));
        }
    }
 
    return 0;
}
 
int evaluate(int digit[], int len)
{
    int i, num = 0;
 
    for (i = 0; i < len; i++)
    {
        num += digit[i] * pow(10, i);
    }
 
    return num;
}

 

Runtime Test Cases

Enter a number: 56732  

The number greater than 56732 and made of same digits is 57632.

C Program to Input 3 Arguments and Operate Appropriately on the Numbers

/* 
 * C Program to Input 3 Arguments and Operate Appropriately on the 
 * Numbers
 */
#include <stdio.h>
 
void main(int argc, char * argv[])
{
    int a, b, result;
    char ch;
 
    printf("arguments entered: \n");
    a = atoi(argv[1]);
    b = atoi(argv[2]);
    ch  = *argv[3];
    printf("%d %d %c", a, b, ch);
    switch (ch)
    {
    case '+':
        result = a + b;
        break;
    case '-':
        result = a - b;
        break;
    case 'x':
        result = a * b;
        break;
    case '/':
        result = a / b;
        break;
    default:
        printf("Enter a valid choice");
    }
    printf("\nThe result of the operation is %d", result);
    printf("\n");    
}

 

Runtime Test Cases

arguments entered:

5 4 +

The result of the operation is 9

 

arguments entered:

8 7 -

The result of the operation is 1

 

arguments entered:

9 6 x

The result of the operation is 54

 

arguments entered:

100 10 /

The result of the operation is 10

C Program to Accept the Height of a Person & Categorize as Taller, Dwarf & Average

/*
 * C program to accept the height of a person in centimeter and
 * categorize the person based on height as taller, dwarf and
 * average height person
 */
 
#include <stdio.h>
void main()
{
    float height;
 
    printf("Enter  the Height (in centimetres) \n");
    scanf("%f", &height);
    if (height < 150.0)
        printf("Dwarf \n");
    else if ((height >= 150.0) && (height <= 165.0))
        printf(" Average Height \n");
    else if ((height > 165.0) && (height <= 195.0))
        printf("Taller \n");
    else
        printf("Abnormal height \n");
}

 

Runtime Test Cases

Enter  the Height (in centimetres)

165

Average Height

 

Enter  the Height (in centimetres)

140

Dwarf

 

Enter  the Height (in centimetres)

190

Taller

C Program to Print Environment Variables

*
 * C Program to Print Environment variables
 */
#include <stdio.h>
 
void main(int argc, char *argv[], char * envp[])
{
    int i;
 
    for (i = 0; envp[i] != NULL; i++)
    {    
        printf("\n%s", envp[i]);
    }
}
$ cc arg7.c
$ a.out
 
HOSTNAME=localhost.localdomain
SELINUX_ROLE_REQUESTED=
SHELL=/bin/bash
TERM=xterm
HISTSIZE=1000
SSH_CLIENT=192.168.7.43 49162 22
SELINUX_USE_CURRENT_RANGE=
QTDIR=/usr/lib64/qt-3.3
QTINC=/usr/lib64/qt-3.3/include
SSH_TTY=/dev/pts/8
USER=harika
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.tbz=01;31:*.tbz2=01;31:*.bz=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:
PATH=/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/harika/bin:.
MAIL=/var/spool/mail/harika
PWD=/home/harika
KDE_IS_PRELINKED=1
LANG=en_US.UTF-8
KDEDIRS=/usr
SELINUX_LEVEL_REQUESTED=
HISTCONTROL=ignoredups
SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
HOME=/home/harika
SHLVL=2
LOGNAME=harika
CVS_RSH=ssh
QTLIB=/usr/lib64/qt-3.3/lib
SSH_CONNECTION=192.168.7.43 49162 192.168.7.140 22
LESSOPEN=|/usr/bin/lesspipe.sh %s
G_BROKEN_FILENAMES=1
_=./a.out
OLDPWD=/home