Wayne. C File
Wayne. C File
Wayne. C File
Of “C”
Programing
Department of computer science and application
Submitted To:- Submitted By:- Wayne D. Amewu
5 To write a C Program,
Using Switch to
Implement Simple
Calculator (ADD, MIN,
DIV, MUL)
6 To write a C program to illustrate Call by
Value and Call by Reference
7 To write a C Program to
Find Factorial of a
Number using
Recursion
#include<stdio.h>
main()
{
int num;
printf ("Enter an integer");
scanf ("%d",&num); if (num%2 == 0)
printf ("%d is an even number", num) ;
else
printf ("%d is an odd number", num); return 0;
}
Output 1
1
Wayne/10 Bsc IT 1st
Program 2
Write a C Program to reverse a number and check whether it is
palindrome or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int num, temp, remainder, reverse = 0;
printf("Enter an integer \n");
scanf("%d", &num);
temp = num;
while (num > 0)
{
remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
printf("Given number is = %d\n", temp);
printf("Its reverse is = %d\n",reverse);
if (temp == reverse)
printf("Number is a palindrome \n");
else
printf("Number is not a palindrome \n"); getch(); }
Output:- 2
2
Wayne/10 Bsc IT 1st
Program 3
Write a C Program to compute Sum of Digit in a given Number
#include <stdio.h>
main()
{
int n, t, sum = 0, remainder;
printf("Enter an integer\n");
scanf("%d", &n);
t = n;
while (t != 0)
{
remainder = t % 10;
sum = sum + remainder;
t = t / 10;
}
Output:- 3
3
Wayne/10 Bsc IT 1st
Program 4
Write a C Program to find whether a given number is Prime or Not.
#include <stdio.h>
main()
{
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
if (n == 1)
{
printf("1 is neither a prime nor a composite number.");
}
else
{
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}
}
Output:- 4
4
Wayne/10 Bsc IT 1st
Program 5
Write a C Program, Using Switch to Implement Simple Calculator
(ADD, MIN, DIV,MUL)
#include <stdio.h>
main()
{
char operator;
double first number, second number;
switch(operator)
{
case '+':
printf ("%.1lf + %.1lf = %.1lf",first number, second number, first number +
second number);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf",firstNumber, second number, first number – second
number);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf",firstNumber, second number, first number * second
number);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf",firstNumber, second number, first number / second
number);
break;
default:
printf("Error! operator is not correct");
}
}
5
Wayne/10 Bsc IT 1st
Output:- 5
6
Wayne/10 Bsc IT 1st
Program 6
Write a C program to illustrate Call by Value and Call by Reference
#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);
printf("After function call x=%d \n", x);
}
Output:- 6
7
Wayne/10 Bsc IT 1st
Program 7
Write a C Program to Find Factorial of a Number using Recursion
#include<stdio.h>
int find_factorial(int);
int main()
{
int num, fact;
printf("\nEnter any integer number:");
scanf("%d",&num);
fact =find_factorial(num);
if(n==0)
return(1);
return(n*find_factorial(n-1));
}
Output:- 7
8
Wayne/10 Bsc IT 1st
Program 8
Write a C program to check whether a given string is palindrome or
not
#include <stdio.h>
#include <string.h>
int main()
{
char string1[20];
int i, length;
int flag = 0;
printf("Enter a string:");
scanf("%s", string1);
length = strlen(string1);
9
Wayne/10 Bsc IT 1st
Program 9
Write a C program to read two strings and concatenate the Strings
#include <stdio.h>
#include <string.h>
int main()
{
char a[1000], b[1000];
printf("Enter the first string\n");
gets(a);
printf("Enter the second string\n");
gets(b);
strcat(a, b);
printf("String obtained on concatenation: %s\n", a);
return 0;
}
Output:- 9
10
Wayne/10 Bsc IT 1st
Program 10
Write a C Program to implement the following Pointer Concept:
a) Pointer to Pointer
#include <stdio.h>
int main () {
int var;
int *ptr;
int **pptr;
var = 8006;
return 0;
}
Output:-
b) Pointer to Structure
#include <stdio.h>
struct my_structure
{
char name[20];
int number;
int rank;
};
11
Wayne/10 Bsc IT 1st
int main()
{
struct my_structure variable = {"Shivam", 06, 1};
struct my_structure *ptr;
ptr = &variable;
return 0;
}
Output:-
c) Pointer to Function
#include <stdio.h>
void fun
(int a)
{
printf("Value of a is %d\n", a);
}
int main()
{
void (*fun_ptr)
(int) = &fun;
(*fun_ptr) (06);
return 0;
}
Output:-
12
Wayne/10 Bsc IT 1st
Program 11
Using Array, write a C Program to Implement the transpose of a
Matrix
#include <stdio.h>
void main()
{ int array[10][10];
int i, j, m, n;
printf("Enter the order of the matrix \n");
scanf("%d %d", &m, &n);
printf("Enter the coefiicients of the matrix\n");
13
Wayne/10 Bsc IT 1st
Output 11
14
Wayne/10 Bsc IT 1st
Program 12
Using Array, write a C Program to Implement the Multiplication of a Matrix
#include <stdio.h>
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
if (n != p)
printf("The matrices can't be multiplied with each other.\n");
else
{
printf("Enter elements of second matrix\n");
15
Wayne/10 Bsc IT 1st
Output:-
16
Wayne/10 Bsc IT 1st
Program 13
Using Structure in C, write a Program to create the record of 10 students
consisting of (Name, Age, Address & their marks In Percentage)
#include <stdio.h>
struct student {
char name[50];
int roll;
int age;
char address;
float marks; }
s[2];
int main(){
int i;
printf("Enter information of students:\n");
for(i=0; i<2; ++i){
s[i].roll = i+1;
printf("\nFor roll number%d,\n",s[i].roll);
printf("Enter name: ");
scanf("%s",s[i].name);
printf("Enter age: ");
scanf("%d",&s[i].age);
printf("\n");
printf("Enter address: ");
scanf("%s",&s[i].address);
printf("\n");
printf("Enter marks: ");
scanf("%f",&s[i].marks);
printf("\n");
}
printf("Displaying Information:\n\n");
for(i=0; i<2; ++i)
{
printf("\nRoll number: %d\n",i+1);
printf("Name: ");
puts(s[i].name);
printf("age: %.d",s[i].age);
printf("address: %.s",s[i].address);
printf("Marks: %.1f",s[i].marks);
printf("\n");
}
17
Wayne/10 Bsc IT 1st
return 0; }
Output:-
18
Wayne/10 Bsc IT 1st
Program 14
Write a C program to Create a file and store the Information
#include <stdio.h>
#include <stdlib.h>
#define DATA_SIZE 1000
int main(){
char data[DATA_SIZE];
FILE * fPtr;
fPtr = fopen("C:/file1.txt", "w");
if(fPtr == NULL) {
printf("Unable to create file.\n");
exit(EXIT_FAILURE);
}
printf("Enter contents to store in file : \n");
fgets(data, DATA_SIZE, stdin);
fputs(data, fPtr);
fclose(fPtr);
printf("File created and saved successfully. \n"); return 0; }
Output:-
19
Wayne/10 Bsc IT 1st
Program 15
Write a C program to illustrate reading of Data from a File
#include <stdio.h>
#include <stdlib.h>
int main(){
char c[1000];
FILE *fptr;
if ((fptr = fopen("program.txt", "r")) == NULL)
{
printf("Error! opening file");
exit(1); }
fscanf(fptr,"%[^\n]", c);
printf("Data from the file:\n%s", c);
fclose(fptr);
return 0; }
Output:-
20
Wayne/10 Bsc IT 1st