0% found this document useful (0 votes)
152 views22 pages

C Program Report

The document contains 50 programs written in C language with solutions to common programming problems like finding area of shapes, relational and logical operators, if-else conditions, loops, functions, arrays etc. It covers basic to intermediate level C programming concepts.

Uploaded by

Officialwahid
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)
152 views22 pages

C Program Report

The document contains 50 programs written in C language with solutions to common programming problems like finding area of shapes, relational and logical operators, if-else conditions, loops, functions, arrays etc. It covers basic to intermediate level C programming concepts.

Uploaded by

Officialwahid
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

Practice Programs in C

1. Program Sample
2. Program to find cube of a number
3. Program to add two numbers
4. Program to Print an Integer
5. Program to find product of floating point numbers
6. Program to Print ASCII Value
7. Program to Compute Quotient and Remainder
8. Program to Find the Size of Variables
9. Swap Numbers Using Temporary Variable
[Link] to find area of rectangle and square
[Link] Decrement Operator
[Link] on Relational operator
[Link] to find whether a person can vote
14. Program to check greater of two number.
15. Program to check condition with &. And |.
16. Program on Static Example
[Link] on extern Storage Class
[Link] on Register variable
[Link] to check Even Odd
[Link] to check largest among three integers
21. Program using switch to display week days .
[Link] using switch
[Link] to print table of a number
[Link] to print sum of 10 numbers.
[Link] to print factorial of a numbers..
[Link] loop
[Link] to check for prime
[Link] using do_while to display sum of n natural number
[Link] to find LCM
[Link] binary to decimal
[Link] jump statement
[Link] to check continue statement.
[Link] to store value in array and traversing
[Link] to input two-dimensional array
[Link] sort.
[Link] to input String .
[Link] to illustrate String functions
[Link] to find reverse of string.
[Link] example with argument /without argument
[Link] by Value
[Link] by Reference
[Link] for calculating factorial.
[Link] Example /double pointer
[Link] to display Array using Pointers
[Link] to allocate memory dynamically malloc function
[Link] using calloc function
[Link] on structure
[Link] to illustrate pionter object to structure
[Link] is a nested structure ?
[Link] structure to function

Program solutions
1. Program Sample
#include <stdio.h>
#include<conio.h>
void main()
{ printf(“C Programming”);
getch();
}

2. Program to find cube of a number


#include<stdio.h>
int main()
{
int number;
printf("enter a number:");
scanf("%d",&number);
printf("cube of number is:%d ",number*number*number);
return 0;
}

3. Program to add two numbers


#include<stdio.h>
int main() {
int x=0,y=0,result=0;
printf("enter first number:");
scanf("%d“ ,&x);
printf("enter second number:");
scanf("%d", &y);
result= x+y;
printf("sum of 2 numbers:%d ",result);
return 0;
}

4. Program to Print an Integer


#include <stdio.h>
int main()
{ int number;
printf("Enter an integer: ");
scanf("%d", &number); // read input
printf("You entered: %d", number); // display
return 0;
}

5. Program to find product of floating point numbers


#include <stdio.h>
int main() {
double a, b, product;
printf("Enter two numbers: ");
scanf("%lf %lf", &a, &b);
// Calculating product
product = a * b;
// %.2lf displays number up to 2 decimal point
printf("Product = %.2lf", product);
return 0;}
}

6. Program to Print ASCII Value


#include <stdio.h>
int main() {
char c;
printf("Enter a character: ");
scanf("%c", &c);

// %d displays the integer value of a character


// %c displays the actual character
printf("ASCII value of %c = %d", c, c);

return 0;
}

7. Program to Compute Quotient and Remainder


#include <stdio.h>
int main() {
int dividend, divisor, quotient, remainder;
printf("Enter dividend: ");
scanf("%d", &dividend);
printf("Enter divisor: ");
scanf("%d", &divisor)
// Computes quotient
quotient = dividend / divisor;
// Computes remainder
remainder = dividend % divisor;
printf("Quotient = %d\n", quotient);
printf("Remainder = %d", remainder);
return 0;
}

8. Program to Find the Size of Variables


#include<stdio.h>
int main() {
int intType;
float floatType;
double doubleType;
char charType;
// sizeof evaluates the size of a variable
printf("Size of int: %zu bytes\n", sizeof(intType));
printf("Size of float: %zu bytes\n", sizeof(floatType));
printf("Size of double: %zu bytes\n", sizeof(doubleType));
printf("Size of char: %zu byte\n", sizeof(charType));
return 0; }

9. Swap Numbers Using Temporary Variable


#include<stdio.h>
int main() {
int first, second, temp;
printf("Enter first number: ");
scanf("%d", &first);
printf("Enter second number: ");
scanf("%d", &second);
temp = first;
first = second;
second = temp;
printf("\nAfter swapping, first number = %d\n", first);
printf("After swapping, second number = %d", second);
return 0;
}
[Link] to find area of rectangle and square
#include <stdio.h>
int main() {
int L,B,S;
printf("Enter Length and Breadth: ");
scanf("%d%d", &L,&B);
printf(“Area : %d ", L*B);
printf("Enter Side : ");
scanf("%d", &S);
printf(“Area of Square: %d ", S*S);
return 0;
}

[Link] Decrement Operator


#include <stdio.h>
int main()
{ int var1 = 5, var2 = 5;
printf("%d\n", var1++);
printf("%d\n", ++var2);
return 0; }

[Link] on Relational operator


#include <stdio.h>
int main()
{ int a = 5, b = 5, c = 10, result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) is %d \n", result);
result = (a == b) && (c < b);
printf("(a == b) && (c < b) is %d \n", result);
result = (a == b) || (c < b);
printf("(a == b) || (c < b) is %d \n", result);
return 0;
}
13. Program to find whether a person can vote
#include <stdio.h>
int main()
{
int age; // variable declaration
printf ( "Enter your age");
scanf("%d",&age); // taking user input for age variable
(age>=18)? (printf("eligible for voting")) : (printf("not eligible for voting"));
return 0;
}

[Link] to check greater of two number.


#include <stdio.h>
int main()
{
int a,b; // variable declaration
printf ( "Enter your a and b");
scanf("%d%d",&a,&b); // taking user input for age variable
(a>b)? (printf(“a is greater")) : (printf(“b is greater"));
return 0;
}

15. Program to check condition with &. And |.


#include <stdio.h>
int main()
{
int a=26, b=14; // variable declarations
printf("The output of the Bitwise AND operator a&b is %d",a&b);
printf("The output of the Bitwise OR operator a|b is %d",a|b);

return 0;
}
16,17,18 Program will be given later on

19. Program Even /Odd


#include<stdio.h>
int main()
{int num;
printf(“Enter number”);
scanf(“%d”,&num);
if (num%2==0)
printf(“Even”);
else
printf(“Odd”);
return 0;}

20. Program to check largest among three integers


#include <stdio.h>
int main()
{ int a, b, c;
printf("Enter three numbers?");
scanf("%d %d %d", &a, &b, &c);
if(a>b && a>c)
{ printf("%d is largest ",a); }
if( b>a && b > c)
{ printf("%d is largest",b); }
if(c>a && c>b)
{ printf("%d is largest",c); }
if(a == b && a == c)
{ printf("All are equal"); } }
21.
22. #include <stdio.h>
int main(){
int num1, num2 ,result;
char c;
printf("Enter opt ");
scanf("%c",&c);
printf("Enter num1,num2"); scanf("%d%d",&num1,&num2);
switch(c)
{case '+': printf("%d %c %d = %d",num1,c,num2,num1+num2);
break;
case '-': printf("%d %c %d = %d",num1,c,num2,num1-num2); }
return 0;}
[Link] to print table of a number.
#include<stdio.h>
#include<conio.h>
int main()
{ int i ,num=7;
for (i=0;i<=5;i++)
{ printf(num *i);}
return 0;
}

[Link] to print sum of 10 numbers..


#include<stdio.h>
#include<conio.h>
int main()
{ int i ,num,sum=0;
printf(“Enter 10 numbers”);
for (i=1;i<=10;i++)
{ printf(“Enter num %d”,i);
scanf(“%d”,&num);
sum+=num;}
printf(“Sum of 10 numbers is =%d ”,sum);
return 0;
}
[Link] to print factorial of a numbers..
#include<stdio.h>
#include<conio.h>
int main()
{ int i ,num,fact=1;
printf(“Enter numbers”);
scanf(“%d”,&num);
for ( i=1;i<=num;i++)
{ fact=fact*i;
}
printf(“Factorial of %d is: %d ”,num,fact);
return 0;
}

26. While loop


27. Program to check for prime
#include<stdio.h>
#include<conio.h>
int main()
{ int i ,num,flag=1;
printf(“Enter numbers”);
scanf(“%d”,&num);
for ( i=2;i<=num/2;i++)
{ if (num%i==0)
flag=0;
break; }
if(flag==0)
printf(“number is not prime);
else
printf(“number is prime);
return 0;
}
28. Program using do_while to display sum of n natural number
#include <stdio.h>
int main()
{ int i, num, sum=0;
printf("Enter num");
scanf("%d",&num);
i=0;
do { sum=sum+i;
i++;
}while(i<=num);
printf("Sum:%d",sum);
return 0;
}
[Link] to find LCM
#include <stdio.h>
int main() {
int n1, n2, max;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
// maximum number between n1 and n2 is stored in max
max = (n1 > n2) ? n1 : n2;
while (1) {
if (max % n1 == 0 && max % n2 == 0) {
printf("The LCM of %d and %d is %d.", n1, n2, max);
break;
}
++max;
}
return 0;
}
30. convert binary to decimal
#include <stdio.h>
#include <math.h>
int convert(long long);
int main() {
long long n;
printf("Enter a binary number: ");
scanf("%lld", &n);
printf("%lld in binary = %d in decimal", n, convert(n));
return 0;
}
int convert(long long n) {
int dec = 0, i = 0, rem;
while (n!=0) {
rem = n % 10;
n /= 10;
dec += rem * pow(2, i);
++i;
}
return dec;
}

31. Goto jump statement


#include <stdio.h>
int main()
{int num , i ,sum=0;
printf("Enter number");
scanf("%d",&num);
i=1;
while(i<=num)
{sum+=i;
i++;
if (sum>=30)
goto label; }
printf("%d",sum);
label :
printf("sorry limit exceeded");
return 0;}

32. Program to check continue statement.


#include<stdio.h>
void main ()
{
int i = 0;
while(i!=10)
{
printf("%d", i);
continue;
i++;
}
}

[Link] to store value in array and traversing


#include<stdio.h>
int main(){
int i=0;
int marks[5]; //declaration of array
marks[0]=80; //initialization of array
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;
//traversal of array
for(i=0;i<5;i++){
printf("%d \n",marks[i]);
} //end of for loop
return 0;
}
[Link] two-dimensional array can be defined as an array of arrays
int a[2][3]={{11,12,13},{21,22,23}};
Program:
#include <stdio.h>
int main()
{ printf ("Hello World\n");
int a[2][3]={{11,12,13},{21,22,23}};
for (int i=0;i<2;i++)
{ for(int j=0;j<3;j++)
{printf("%d\t",a[i][j]);}
printf("\n"); }
return 0;}

35. Bubble sort


#include <stdio.h>
int main(){
printf("Hello World\n");
int a[8]={4,7,8,1,22,32,11,9};
int tmp,k,n=8;
for (int i=0;i<n;i++)
{ for(int j=0;j<n-1-i;j++)
{ if(a[j]>a[j+1])
{ tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
} }
}
for ( k=0;k<8;k++)
{printf("%d\t",a[k]);}
return 0;}

36. Input of a string


#include<stdio.h>
void main ()
{
char s[20];
printf("Enter the string?");
scanf("%s",&s);
printf("You entered %s",&s);
}
37. String functions
#include <stdio.h>
#include <string.h>
int main()
{char str1[10],str2[10];
printf("Enter string1");
gets(str1);
printf("Enter string2");
strcpy(str2,"Hello");
printf("\nLength of string 1:%d",strlen(str1)); printf("\nLength of string
2:%d",strlen(str2));
if (strcmp(str1,str2)==0)
printf("\nThey are same");
else
printf("\nNot same\n"); printf(strcat(str1,str2));
return 0;
}

[Link] to find reverse of string


#include<stdio.h>
#include <string.h>
int main(){
char str[20];
printf("Enter string: ");
gets(str);//reads string from console
printf("String is: %s",str);
printf("\nReverse String is: %s",strrev(str));
return 0;
}

39. Function example with argument /without argument


#include <stdio.h>
#include <string.h>
void add();
int sub(int ,int );
int main()
{ int x,y,r;
printf("Sum Of two values is");
add();
printf("Enter values for substraction");
scanf("%d%d“,&x,&y);
r=sub(x,y);
printf("Difference :%d",r);
return 0;}
void add()
{ int a=20,b=40;
printf("%d",a+b);}
int sub(int x,int y)
{ return x-y; }

[Link] 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;
}
41. 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;
}
42. Recursion
#include <stdio.h>
int fact (int);
int main()
{
int n,f;
printf("Enter the number ");
scanf("%d",&n);
f = fact(n);
printf("factorial = %d",f);
}
int fact(int n)
{
if (n==0)
{ return 0; }
else if ( n == 1)
{ return 1; }
else
{ return n*fact(n-1); }
}
[Link] example
#include<stdio.h>
int main(){
int number=50;
int *p;
p=&number;
printf("Address of p variable is %x \n",p);
printf("Value of p variable is %d \n",*p);
return 0;
}

[Link] and Pointers


#include <stdio.h>
const int MAX = 4;
int main ()
{ char *names[] = { "Zara Ali",
"Hina Ali",
"Nuha Ali",
"Sara Ali" };
int i = 0;
for ( i = 0; i < MAX; i++)
{ printf("Value of names[%d] = %s\n", i, names[i] ); }
return 0;
}
[Link] to allocate memory dynamically
#include <stdio.h>
#include <stdlib.h>
int main()
{ int* ptr;
int n, i;
printf("Enter number of elements:");
scanf("%d",&n);
printf("Entered number of elements: %d\n", n);
ptr = (int*)malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.\n");
exit(0); }
else { printf("Memory successfully allocated using malloc.\n");
for (i = 0; i < n; ++i) {
ptr[i] = i + 1;}
printf("The elements of the array are: ");
for (i = 0; i < n; ++i) {
printf("%d, ", ptr[i]); } }
return 0; }
[Link] using calloc
#include <stdio.h>
#include <stdlib.h>
int main()
{ int* ptr;
int n, i;
n = 5;
printf("Enter number of elements: %d\n", n);
ptr = (int*)calloc(n, sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.\n");
exit(0); }
else { printf("Memory successfully allocated using calloc.\n");
for (i = 0; i < n; ++i) {
ptr[i] = i + 1;
}
printf("The elements of the array are: ");
for (i = 0; i < n; ++i) {
printf("%d, ", ptr[i]); } }
return 0; }

[Link] on structure
struct Point
{ int x, y;
};
int main()
{ struct Point p1 = {0, 1};
p1.x = 20;
printf ("x = %d, y = %d", p1.x, p1.y) ;
return 0;
}
48. Program to illustrate pionter object to structure
struct Point
{ int x, y;
};
int main()
{ struct Point p1 = {0, 1};
p1.x = 20;
printf ("x = %d, y = %d", p1.x, p1.y) ;
// p2 is a pointer to structure p1
struct Point *p2 = &p1;
// Accessing structure members using structure pointer
printf("%d %d", p2->x, p2->y);
return 0;
}

[Link] is a nested structure ?


#include<stdio.h>
struct address
{ char city[20];
int pin;
char phone[14];
};
struct employee
{ char name[20];
struct address add;
};
void main ()
{ struct employee emp;
printf("Enter employee information?\n");
scanf("%s %s %d %s",[Link],[Link], &[Link], [Link]);

printf("Printing the employee information....\n");


printf("name: %s\nCity: %s\nPincode: %d\nPhone: %s",[Link],[Link]
,[Link],[Link]);
}
[Link] structure to function

#include<stdio.h>
struct address
{ char city[20], phone[14];
int pin;
};
struct employee
{ char name[20];
struct address add;
};
void display(struct employee);
void main ()
{ struct employee emp;
printf("Enter employee information?\n");
scanf("%s %s %d %s",[Link],[Link], &[Link], [Link]);

display(emp); }
void display(struct employee emp)
{ printf("Printing the details....\n");
printf("%s %s %d%s",[Link],[Link],[Link],[Link]);}

You might also like