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

structure of C program

Uploaded by

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

structure of C program

Uploaded by

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

1.

A program to display our name

#include <stdio.h>

int main()
{

printf("Hi, my name is mohammed affan");

return 0;
}

2.A program to enter a value


/*
author: Mohammed Affan
purpose: This program prints out my name
Date: 04\07\2024

*/
int main()
{
char Str[100];
int i;

printf("enter a value:");

scanf("%d", &i);

printf("\nYou entered: %\dn", i);

return 0;
}
result:You entered: 67

3.A program to display name with value

/*
author: Mohammed Affan
purpose: This program prints out my name
Date: 04\07\2024

*/

#include <stdio.h>

int main()
{
char Str[100];
int i;

printf("enter a value:");

scanf("%d %s", &i, Str);

printf("\nYou entered: %d:::::%s\n", i, Str);


return 0;
}
result: 56:::::affan

4.A program to display double value

#include <stdio.h>

int main()
{
double x;

scanf("%lf", &x);

return 0;
}

5.calculator code

#include <stdio.h>

int main() {

char op;
double first, second;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &op);
printf("Enter two operands: ");
scanf("%lf %lf", &first, &second);

switch (op) {
case '+':
printf("%.1lf + %.1lf = %.1lf", first, second, first + second);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
break;
// operator doesn't match any case constant
default:
printf("Error! operator is not correct");
}

return 0;
}

6.A program which displays output

/*
author: Mohammed Affan
purpose: This program prints out my name
Date: 04\07\2024

*/
#include <stdio.h>

int main()
{
int integervar = 100;
float floatingvar = 331.79;
double doublevar = 8.44e+11;
char charvar = 'w';
_Bool boolvar = 0;

printf ("integervar = %i\n", integervar);


printf ("floatingvar = %f\n", floatingvar);
printf ("doublevar =%e\n", doublevar);
printf ("doublevar =%g\n", doublevar);
printf ("charvar =%c\n", charvar);
printf ("boolvar =%i\n", boolvar);

return 0;
}

result :integervar = 100


floatingvar = 331.790009
doublevar =8.440000e+11
doublevar =8.44e+11
charvar =w
boolvar =0

7.a programme of command line argument

/* Author: Mohammed affan


Purpose: This program prints out my name to the screen
date :04/07/2024

*/

#include <stdio.h>

int main(int argc, char *argv[])


{
int numberofarguments = argc;
char *argument1 = argv[0];
char *argument2 = argv[1];

printf ("number of arguments: %d\n", numberofarguments);


printf ("Argument 1 is the program name: %s\n", argument1);
printf ("Argument 2 is the command line argument: %s\n", argument2);

return 0;
}
8.A program to know the final number

#include <stdio.h>

int main()

int i = 10, j = 2;

printf("%d, %d\n", i, j);

result:10,2

A program for increment

/*
author: Mohammed Affan
purpose: This program prints out my name
Date: 04\07\2024

*/

#include <stdio.h>

int main()
{
int a = 33;
int b = 15;
int result = 0;

printf ("c is %d\n", ++a);

return 0;
}

logical operators
and operator (&)
1.a program for true logical operation/*
author: Mohammed Affan
purpose: This program prints out my name
Date: 04\07\2024

*/

#include <stdio.h>

int main()
{
_Bool a = 1;
_Bool b = 1;
_Bool result;

result = a && b;
printf ("%d" , result);

return 0;
}

2.A program for false logical operator

/*
author: Mohammed Affan
purpose: This program prints out my name
Date: 04\07\2024

*/

#include <stdio.h>

int main()
{
_Bool a = 1;
_Bool b = 0;
_Bool result;

result = a && b;

printf ("%d" , result);

return 0;
}

bar operator (||)

1. A program for true logical operator

/*
author: Mohammed Affan
purpose: This program prints out my name
Date: 04\07\2024

*/

#include <stdio.h>

int main()
{
_Bool a = 1;
_Bool b = 0;
_Bool result;

result = a || b;

printf ("%d" , result);

return 0;
}
2.A program for false logical operator
/*
author: Mohammed Affan
purpose: This program prints out my name
Date: 04\07\2024

*/

#include <stdio.h>

int main()
{
_Bool a = 0;
_Bool b = 0;
_Bool result;

result = a || b;

printf ("%d" , result);

return 0;
}
not operators (!a or !a&&b or !b)

1.A program fortrue logical operator


/*
author: Mohammed Affan
purpose: This program prints out my name
Date: 04\07\2024

*/

#include <stdio.h>

int main()
{
_Bool a = 0;
_Bool b = 0;
_Bool result;

result = !a ; or !b;

printf ("%d" , result);

return 0;
}

2. A program for false logical operator

/*
author: Mohammed Affan
purpose: This program prints out my name
Date: 04\07\2024

*/

#include <stdio.h>

int main()
{
_Bool a = 0;
_Bool b = 0;
_Bool result;

result = !a && b;

printf ("%d" , result);

return 0;
}

bitewise operaters

1.and operater (&)

/*
author: Mohammed Affan
purpose: This program prints out my name
Date: 04\07\2024

*/

#include <stdio.h>

int main()
{
unsigned int a = 60; // 00111100
unsigned int b = 13; // 00001101
int result = 0;

result = a & b;
//00001100
printf ("result is %d", result);

return 0;
}

result : 12

2.bar operater or division operater (|)

/*
author: Mohammed Affan
purpose: This program prints out my name
Date: 04\07\2024

*/

#include <stdio.h>

int main()
{
unsigned int a = 60; // 00111100
unsigned int b = 13; // 00001101
int result = 0;

result = a | b;
//0011 1101
printf ("result is %d", result);

return 0;
}

result : 61

3. left shift operater

/*
author: Mohammed Affan
purpose: This program prints out my name
Date: 04\07\2024

*/

#include <stdio.h>

int main()
{
unsigned int a = 60; // 00111100
unsigned int b = 13; // 00001101
int result = 0;

result = a<<4 ;

//0011 1101
printf ("result is %d", result);

return 0;
}

result : 960

4. right shift operater

/*
author: Mohammed Affan
purpose: This program prints out my name
Date: 04\07\2024

*/

#include <stdio.h>

int main()
{
unsigned int a = 60; // 00111100
unsigned int b = 13; // 00001101
int result = 0;

result = a>>4 ;

//0011 1101
printf ("result is %d", result);

return 0;
}
result : 3

5. complement operater

/*
author: Mohammed Affan
purpose: This program prints out my name
Date: 04\07\2024

*/

#include <stdio.h>

int main()
{
unsigned int a = 1; // 00111100
unsigned int b = 13; // 00001101
int result = 0;

result = ~b ;

//0011 1101
printf ("result is %d", result);

return 0;
}

result : -14

switch statement

/*
author: Mohammed Affan
purpose: This program prints out my name
Date: 04\07\2024

*/
int main()
{

enum weekly {Monday,Tuesday,Wednesday,Thuraday,Friday,Saturday,Sunday};


enum weekly today = Sunday;

switch (today)
{
case sunday:
printf ("Today is sunday");
break;
case Monday:
printf ("Today is monaday");
break;
case Tuesday:
printf ("Today is tuesday");
break;
default:
printf ("whatever");
break;
}

return 0;
}

A program of loop function ic language

/*
author: Mohammed Affan
purpose: This program prints out my name
Date: 04\07\2024

*/

#include<stdio.h>

int main()
{
unsigned long long sum = 0; // stores the sum of the integers
unsigned int count = 0; //The number of integers to be summed

//Read the number of integers to be summed


printf("\nEnter the number of integers you want to sum:");
scanf("%u", &count);

//sum integers from 1 to count


unsigned int i;
for( i = 1; i<=count ; ++i)
{
printf("inside loop");
sum += i;

printf("\nTotal of the first %u numbers is %llu\n", count, sum);

return 0;
}

Array program
A program which displays average 10 grades

/*
author: Mohammed Affan
purpose: This program prints out my name
Date: 04\07\2024

*/

#include <stdio.h>
int main()
{
int grades[10]; //Array storing 10 values
int count = 10; //Number of values to be read
long sum = 0; //sum of the numbers
float average = 0.0f; //Average of the numbers

printf("\nEnter the 10 grades :\n"); //prompt for input

//Read the 10 number to br averaged


int i;
for(i =0; i< count; ++i)
{
printf("%2u> ", i + 1);
scanf("%d", &grades [i]); //Read a grade
sum += grades [i]; //Add it to sum
}

average = (float)sum/count; //Average

printf("\nAverage of the ten grades entered is : %.2f\n", average);

return 0;
}

Result:Average of the ten grades entered is : 184.80

string

a program which displays lenght of a string

#include <stdio.h>
#include <string.h>

int main()
{
char mystring[] = "My name is Affan";

printf("the lenght is: %d" , strlen(mystring));


return 0;
}

result : the lenght is 16

A program which displays name of the string

#include <stdio.h>
#include <string.h>
int main()
{
char mystring[] = "My name is Affan";

char temp[50];

strncpy(temp, mystring, sizeof(temp) - 1);

printf("the string is: %s" , temp);


return 0;
}

result : the string is My name is Affan

A program which displays final string

#include <stdio.h>
#include <string.h>

int main()
{
char src[50], dest[50];

strcpy(src, "this is the source");


strcpy(dest, "this is the destinatiom");

strncat (dest, src, 15);

printf("Final destination string: |%s|" , dest);


return 0;
}

result : Final destination string |this is the destinatiomthis is the sou|

A program which displays numbers in a string

#include <stdio.h>
#include <string.h>

int main()
{
printf("strcmp(\"A\", \"A\") is ");
printf("%d\n", strcmp("A", "A"));

printf("strcmp(\"A\", \"B\") is ");


printf("%d\n", strcmp("A", "B"));

printf("strcmp(\"B\", \"A\") is ");


printf("%d\n", strcmp("B", "A"));

printf("strcmp(\"C\", \"A\") is ");


printf("%d\n", strcmp("C", "A"));
printf("strcmp(\"Z\", \"a\") is ");
printf("%d\n", strcmp("Z", "a"));

printf("strcmp(\"apples\", \"apple\") is ");


printf("%d\n", strcmp("apples", "apple"));
return 0;

result :strcmp("A", "A") is 0


strcmp("A", "B") is -1
strcmp("B", "A") is 1
strcmp("C", "A") is 1
strcmp("Z", "a") is -1
strcmp("apples", "apple") is 1

You might also like