AKUL Cprogramming Practical
AKUL Cprogramming Practical
AKUL Cprogramming Practical
UNIVERSITY
INSTITUTE OF INNOVATION
IN TECHNOLOGY &
MANAGEMENT
INDEX
S. No. Problem Statement Date Signature
getch();
}
Output:
Program 2
Ques: Write a program to find the greatest number among 3
numbers given by the user.
Code:
#include<stdio.h>
int main ()
{
int num1, num2, num3;
printf ("Enter first number:");
scanf ("%d”, &num1);
printf ("\nEnter second number:");
scanf ("%d”, &num2);
printf ("\nEnter third number:");
scanf ("%d”, &num3);
if(num1>num2 && num1>num3)
{
printf ("\nFirst number is greater than other numbers");
}
else if(num2>num1 && num2>num3)
{
printf ("\nSecond number is greater than other numbers");
}
else
{
printf ("\nThird number is greater than other numbers");
}
Return 0;
}
Output:
Program 3
Ques: Write a program to check if a given number is a prime
number or not.
Code:
#include <stdio.h>
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
if (n == 0 || n == 1){
flag = 1;
}
for (i = 2; i <= n / 2; ++i) {
if (n % i == 0) {
flag = 1;
break;
}
}
if (flag == 0){
printf("%d is a prime number.", n);
}
else{
printf("%d is not a prime number.", n);
}
return 0;
}
Output:
Program 4
Ques: Write a program to check if a given number is odd or even.
Code:
#include<stdio.h>
int main ()
{
int n;
printf ("Enter a number:");
scanf ("%d”, &n);
if(n%2==0)
{
printf ("It is an even number.");
}
else
{
printf ("It is an odd number.");
}
Return 0;
}
Output:
Program 5
Ques: Write a program to find the area of (i) Circle (ii) Rectangle.
Code:
(i)
#include<stdio.h>
int main ()
{
int radius;
float area;
printf ("Enter radius:");
scanf ("%d”, &radius);
area = 3.14*radius*radius;
printf ("Area of circle is %f”, area);
return 0;
}
Output:
(ii)
#include<stdio.h>
int main()
{
int length, breadth;
float area;
printf ("Enter length:");
scanf ("%d”, &length);
printf ("Enter breadth:");
scanf ("%d”, &breadth);
area = length*breadth;
printf ("Area of rectangle is %f", area);
return 0;
}
Output:
Program 6
Ques: Write a program to swap the value of two variable with the
help of third variable.
Code:
#include<stdio.h>
int main ()
{
int a, b, c;
printf ("Enter the value of a:");
scanf ("%d”, &a);
printf ("Enter the value of b:");
scanf ("%d”, &b);
printf ("\nNumbers before swapping are %d %d “, a, b);
c=a;
a=b;
b=c;
printf ("\nNumbers after swapping are %d %d “, a, b);
return 0;
}
Output:
Program 7
Ques: Write a program to display the following pattern upto n
rows, taking the value of n from the user.
1
23
456
7 8 9 10
Code:
#include <stdio.h>
int main() {
int rows, i, j, number = 1;
clrscr();
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; ++j) {
printf("%d ", number);
++number;
}
printf("\n");
}
return 0;
}
Output:
Program 8
Ques: Write a menu driven program to construct a calculator for
following Arithmetic Operation: addition, subtraction,
multiplication, division, average and percentage.
Code:
#include <stdio.h>
int main ()
{
float num1;
float num2;
char op;
float result;
switch(op)
{
case '-':
result = num1 - num2;
printf ("%f”, result);
break;
case '+':
result = num1 + num2;
printf ("%f”, result);
break;
case '*':
result = num1 * num2;
printf ("%f”, result);
break;
case '/':
result = num1 / num2;
printf ("%f”, result);
break;
case 'a':
result = (num1 + num2)/2;
printf ("%f”, result);
break;
case 'p':
result = ((num1 + num2)/200) *100;
printf ("%f”, result);
break;
default:
printf ("the operator is not valid:");
}
Return 0;
}
Output:
Program 9
Ques: Write a program to calculate factorial of a number and
display Fibonacci series upto N terms using recursive functions.
Code:
#include<stdio.h>
void printFibonacci (int n) {
static int n1=0, n2=1, n3;
if(n>0) {
n3 = n1 + n2;
n1 = n2;
n2 = n3;
printf ("%d “, n3);
printFibonacci(n-1);
}
}
int main () {
int n;
printf ("Enter the number of elements: ");
scanf ("%d”, &n);
printf ("Fibonacci Series: ");
printf ("%d %d ",0,1);
printFibonacci(n-2) ;//n-2 because 2 numbers are already printed
return 0;
}
Output:
Program 10
Ques: Write a program to input marks of 50 students using an
array and display the average marks of the class.
Code:
#include<stdio.h>
int main ()
{
int marks [50], i, n, sum =0;
double average;
sum += marks[i];
}
average = (double) sum / n;
printf ("Average = %.2lf", average);
return 0;
}
Output:
Program 11
Ques: Write a program to search for a number entered by the
user in a given array and display the array in ascending order.
Code:
#include <stdio.h>
void main (){
int num[20]={3,4,1,5,2,6};
int i, j, a, n , no,flag=0;
printf("enter the number you want to search it");
scanf("%d",&no);
int main () {
char string1[20];
int i, length;
int flag = 0;
printf ("Enter a string: ");
scanf ("%s", string1);
length = strlen(string1);
for (i = 0; i < length / 2; i++) {
if (string1[i]! = string1[length - i - 1]) {
flag = 1;
break;
}
}
if (flag) {
printf ("%s is not a palindrome\n", string1);
} else {
printf ("%s is a palindrome\n", string1);
}
return 0;
}
Output:
Program 13
Ques: write a menu driven program to perform the following
operations: (i) Print armstrong numbers upto N, (ii) Display
prime numbers between 1 to N, (iii) Reverse of an integer.
#include<stdio.h>
void main() {
int choice;
clrscr();
while(1) {
printf("1.print armstrong number : \n");
printf("2. Pirnt Prime number : \n");
printf("3.Print reverse number : \n");
printf("4.Exit\n");
printf("\nYour choice :? ");
scanf("%d",&choice);
switch(choice){
case 1:{
int n,sum,i,t,a;
printf("The Armstrong numbers in between 1 to N are : \n\n\n");
scanf("%d",&n);
for(i = 1;i<=n;i++)
{
t = i;
sum = 0;
while(t != 0){
a = t%10;
sum += a*a*a;
t = t/10;
}
if(sum == i){
printf("%d\n", i);
}
}
break;
}
case 2:{
int j,i,count,n;
printf("Enter max range: ");
scanf("%d",&n);
for(i=1;i<=n;i++){
count = 0;
for(j=2;j<=i/2;j++){
if(i%j==0){
count++;
break;
}
}
while (n != 0) {
remainder = n % 10;
reverse = reverse * 10 + remainder;
n /= 10;
}
printf("Reversed number = %d\n", reverse);
break;
}
case 4:{
exit(0);
}
default:
printf("you choose wrong choice");
}
}
Return 0;
}
Output:
Program 14
Ques: Write a program to perform (i)matrix addition, (ii) matrix
multiplication, and (iii) Matrix transpose) on 2D arrays.
Code:
(i) Matrices addtion
#include <stdio.h>
int main() {
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c)
return 0;
}
Output
Output:
Program 15
Write a menu driven program to implement the following string
operations: (i) Calculate length of a string (ii) Concatenate at the
end of a given (iii) Copy one string to another (iv) Compare
contents of two strings (v) Copy nth character string to another.
Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char str1[20],str2[20];
int ch,i,j;
do
{
printf("\tMENU");
printf("\n------------------------------\n");
printf("1:Find Length of String");
printf("\n2:Find Reverse of String");
printf("\n3:Concatenate Strings");
printf("\n5:Copy String ");
printf("\n5:Compare Strings");
printf("\n6:Exit");
printf("\n------------------------------\n");
printf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter String: ");
scanf("%s",str1);
i=strlen(str1);
printf("Length of String : %d\n\n",i);
break;
case 2:
printf("Enter String: ");
scanf("%s",str1);
//strrev(str1);
printf("Reverse string : %s\n\n",str1);
break;
case 3:
printf("\nEnter First String: ");
scanf("%s",str1);
printf("Enter Second string: ");
scanf("%s",str2);
strcat(str1,str2);
printf("String After Concatenation : %s\n\n",str1);
break;
case 4:
printf("Enter a String1: ");
scanf("%s",str1);
printf("Enter a String2: ");
scanf("%s",str2);
printf("\nString Before Copied:\
nString1=\"%s\",String2=\"%s\"\n",str1,str2);
strcpy(str2,str1);
printf("-----------------------------------------------\n");
printf("\"We are copying string String1 to String2\" \n");
printf("-----------------------------------------------\n");
printf("String After Copied:\nString1=\"%s\",
String2=\"%s\"\n\n",str1,str2);
break;
case 5:
printf("Enter First String: ");
scanf("%s",str1);
printf("Enter Second String: ");
scanf("%s",str2);
j=strcmp(str1,str2);
if(j==0)
{
printf("Strings are Same\n\n");
}
else
{
printf("Strings are Not Same\n\n");
}
break;
case 6:
exit(0);
break;
default:
printf("Invalid Input. Please Enter valid Input.\n\n ");
}
}while(ch!=6);
return 0;
}
Output
Output:
Program 16
Write a program to create a structure for employees containing
the following data members: Employee ID, Employee Name, Age,
Address, Department and Salary. Input data for 10 employees
and display the details of the employee from the employee ID
given by the user.
Code:
#include<stdio.h>
struct employee{
int employeeID;
char name[50];
int age;
char address[100];
char department[100];
float salary;
};
int main(){
struct employee emp[10];
int i,id;
clrscr();
for(i=1;i<=10;i++){
printf("input the details for\n %d\nEmployeeid:\
nname:\nAge:\nDepartment:\nsalary:\n",i);
scanf("%d",&emp[i].employeeID);
scanf("%s",emp[i].name);
scanf("%d",&emp[i].age);
scanf("%s",emp[i].address);
scanf("%s",emp[i].department);
scanf("%.2f",&emp[i].salary);
}
printf("enter id of employee you want to search");
scanf("%d",&id);
for(i=1;i<=10;i++){
if(id==emp[i].employeeID){
printf("%d employee");
printf("%d",emp[i].employeeID);
printf("%s",emp[i].name);
printf("%d",emp[i].age);
printf("%s",emp[i].address);
printf("%s",emp[i].department);
printf("%.2f",emp[i].salary);
}
}
return 0;
}
output:
Program 17
Write a program to add, subtract, multiply and divide two
numbers using pointers.
Code:
#include<stdio.h>
void main()
{
int num1,num2,sub,*p1,*p2,add,div,mul;
clrscr();
printf("Enter 1st number: ");
scanf("%d",&num1);
printf("Enter 2nd number: ");
scanf("%d",&num2);
p1=&num1;
p2=&num2;
add=*p1+*p2;
sub=*p1-*p2;
mul=*p1 * *p2;
div=*p1/*p2;
printf("\n\nDifference of %d and %d is %d",*p1,*p2,sub);
printf("\n\nAddition of %d and %d is %d",*p1,*p2,add);
printf("\n\n multiplication of %d and %d is %d",*p1,*p2,mul);
printf("\n\nDivision of %d and %d is %d",*p1,*p2,div);
getch();
}
Output: