Pps Amit
Pps Amit
DAHOD(018)
PRACTICALS OF
PROGRAMMING FOR PROBLEM
SOLVING
(3110003)
#include <stdio.h>
#include<string.h>
#include <time.h>
int main()
{char str[]="NAME-AMIT YADAV,ENROLLMENT NO-210180107001";
struct tm* ptr;
time_t t;
t = time(NULL);
ptr = localtime(&t);
printf("%s", asctime(ptr));
printf("\n%s",str);
int h,b;
float area;
printf("\nenter the base and height:");
scanf("%d %d",&b,&h);
area=0.5*b*h;
printf("\narea of triangle is:%f",area);
return 0;
}
Q3.)Write a C program to interchange two numbers.
#include <stdio.h>
#include<string.h>
#include <time.h>
int main()
{char str[]="NAME-AMIT YADAV,ENROLLMENT NO-210180107001";
struct tm* ptr;
time_t t;
t = time(NULL);
ptr = localtime(&t);
printf("%s", asctime(ptr));
printf("\n%s",str);
int x,y;
printf("\n enter number 1:");
scanf("%d",&x);
printf("\n enter number 2:");
scanf("%d",&y);
x=x+y;
y=x-y;
x=x-y;
printf("\n new number 1:%d",x);
printf("\n new number 2:%d",y);
return 0;
}
Q4.) Write a C program to find that the accepted number is Negative or Positive or Zero.
#include <stdio.h>
#include<string.h>
#include <time.h>
int main()
{char str[]="NAME-AMIT YADAV,ENROLLMENT NO-210180107001";
struct tm* ptr;
time_t t;
t = time(NULL);
ptr = localtime(&t);
printf("%s", asctime(ptr));
printf("\n%s",str);
int n;
printf("\nenter the number:");
scanf("%d",&n);
if(n>0)
printf("\npositive");
else if(n<0)
printf("\nnegative");
else
printf("\nzero");
return 0;
}
Q6.)Write a program to read marks of a student from keyboard whether the student is pass
or fail( using if else).
#include <stdio.h>
#include<string.h>
#include <time.h>
int main()
{char str[]="NAME-AMIT YADAV,ENROLLMENT NO-210180107001";
struct tm* ptr;
time_t t;
t = time(NULL);
ptr = localtime(&t);
printf("%s", asctime(ptr));
printf("\n%s",str);
int marks;
printf("\nenter marks:");
scanf("%d",&marks);
if(marks>33)
printf("\npass");
else
printf("\nfail");
return 0;
}
Q7.) Write a program to read three numbers from the keyboard and find out the
maximum out of these three. (Nested if else).
#include <stdio.h>
#include<string.h>
#include <time.h>
int main()
{char str[]="NAME-AMIT YADAV,ENROLLMENT NO-210180107001";
struct tm* ptr;
time_t t;
t = time(NULL);
ptr = localtime(&t);
printf("%s", asctime(ptr));
printf("\n%s",str);
int a,b,c;
printf("\nenter numbers:");
scanf("%d %d %d",&a,&b,&c);
if(a>b)
{
if(a>c)
printf("\n%d is maximum",a);
else
printf("\n%d is maximum",c);
}
else
{
if(b>c)
printf("\n%d is maximum",b);
else
printf("\n%d is maximum",c);
}
return 0;
}
Q8.) Write a C program to read no. 1 to 7 and print relatively day Sunday to Saturday
#include <stdio.h>
#include<string.h>
#include <time.h>
int main()
{char str[]="NAME-AMIT YADAV,ENROLLMENT NO-210180107001";
struct tm* ptr;
time_t t;
t = time(NULL);
ptr = localtime(&t);
printf("%s", asctime(ptr));
printf("\n%s",str);
int r;
printf("\n enter no from 1-7:");
scanf("%d",&r);
switch(r)
{
case 1:printf("\n SUNDAY");break;
case 2:printf("\n MONDAY");break;
case 3:printf("\n TUESDAY");break;
case 4:printf("\n WEDNESDAY");break;
case 5:printf("\n THURSDAY");break;
case 6:printf("\n FRIDAY");break;
case 7:printf("\n SATURDAY");break;
}
return 0;
}
Q9.) Write a C program to input an integer number and check if the last digit of the number is
even or odd
#include <stdio.h>
#include<string.h>
#include <time.h>
int main()
{char str[]="NAME-AMIT YADAV,ENROLLMENT NO-210180107001";
struct tm* ptr;
time_t t;
t = time(NULL);
ptr = localtime(&t);
printf("%s", asctime(ptr));
printf("\n%s",str);
int n;
printf("\nenter the number:");
scanf("%d",&n);
int rem=n%10;
if(rem%2==0)
printf("\nlast digit is even");
else
printf("\nlast digit is odd");
return 0;
}
Q10.) Write a C program to find the factorial of a given number
#include <stdio.h>
#include<string.h>
#include <time.h>
int main()
{char str[]="NAME-AMIT YADAV,ENROLLMENT NO-210180107001";
struct tm* ptr;
time_t t;
t = time(NULL);
ptr = localtime(&t);
printf("%s", asctime(ptr));
printf("\n%s",str);
int n;int fac=1;
printf("\nenter the number:");
scanf("%d",&n);
while(n>1)
{fac=fac*n;
n--;
}
printf("\nfactorial of given number is:%d",fac);
return 0;
}
Q11.) Write a program to reverse a number.
#include <stdio.h>
#include<string.h>
#include <time.h>
int main()
{char str[]="NAME-AMIT YADAV,ENROLLMENT NO-210180107001";
struct tm* ptr;
time_t t;
t = time(NULL);
ptr = localtime(&t);
printf("%s", asctime(ptr));
printf("\n%s",str);
int n;
printf("\nenter the number:");
scanf("%d",&n);
printf("\nthe reversed number is:");
while(n!=0)
{printf("%d",n%10);
n=n/10;
}
return 0;
}
Q12.) Write a program to generate the first n number of Fibonacci series
#include <stdio.h>
#include<string.h>
#include <time.h>
int main()
{char str[]="NAME-AMIT YADAV,ENROLLMENT NO-210180107001";
struct tm* ptr;
time_t t;
t = time(NULL);
ptr = localtime(&t);
printf("%s", asctime(ptr));
printf("\n%s",str);
int n;int a[100];
a[0]=0;a[1]=1;
printf("\nenter the number n of fibonacci series:");
scanf("%d",&n);
for(int i=2;i<n;i++)
{
a[i]=a[i-1]+a[i-2];
}
for(int j=0;j<n;j++)
{printf("%d ",a[j]);
}
return 0;
}
Q13.) Write a program to calculate average and total of 5 students for 3 subjects (use
nested for loops).
#include <stdio.h>
#include<string.h>
#include <time.h>
int main()
{char str[]="NAME-AMIT YADAV,ENROLLMENT NO-210180107001";
struct tm* ptr;
time_t t;
t = time(NULL);
ptr = localtime(&t);
printf("%s", asctime(ptr));
printf("\n%s",str);
int marks,total,avg;
for(int i=1;i<=5;i++)
{avg=0;total=0;
for(int j=1;j<=3;j++)
{
printf("\nenter student %d subject %d marks:",i,j);
scanf("%d",&marks);
total+=marks;
}
avg=total/3;
printf("\ntotal of student %d is:%d",i,total);
printf("\naverage of student %d is:%d",i,avg);
}
return 0;
}
Q14.) Write a program to check whether the given number is prime or not.
#include <stdio.h>
#include<string.h>
#include <time.h>
int main()
{char str[]="NAME-AMIT YADAV,ENROLLMENT NO-210180107001";
struct tm* ptr;
time_t t;
t = time(NULL);
ptr = localtime(&t);
printf("%s", asctime(ptr));
printf("\n%s",str);
int n;
printf("\nenter the number:");
scanf("%d",&n);int m=2;int flag=0;
if((n!=1)&&(n!=0))
{while(m<n)
{
if(n%m==0)
{flag=1;break;
}
m++;
}
}
else
flag=1;
if(flag==0)
printf("\nprime");
else
printf("\nnot prime");
return 0;
}
Q15.) Write a C program to find 1+1/2+1/3+1/4+....+1/n.
#include <stdio.h>
#include<string.h>
#include <time.h>
int main()
{char str[]="NAME-AMIT YADAV,ENROLLMENT NO-210180107001";
struct tm* ptr;
time_t t;
t = time(NULL);
ptr = localtime(&t);
printf("%s", asctime(ptr));
printf("\n%s",str);
int n;float ans,f;
ans=0.0;
printf("\nenter the nth term:");
scanf("%d",&n);
for(float i=1;i<=n;i++)
{f=1/i;
ans=ans+f;
}
printf("\nthe sum of the series is:%f",ans);
return 0;
}
Q16.)
(i)
#include <stdio.h>
#include<string.h>
#include <time.h>
int main()
{char str[]="NAME-AMIT YADAV,ENROLLMENT NO-210180107001";
struct tm* ptr;
time_t t;
t = time(NULL);
ptr = localtime(&t);
printf("%s", asctime(ptr));
printf("\n%s\n",str);
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{printf("*");
}
printf("\n");
}
return 0;
}
(ii)
#include <stdio.h>
#include<string.h>
#include <time.h>
int main()
{char str[]="NAME-AMIT YADAV,ENROLLMENT NO-210180107001";
struct tm* ptr;
time_t t;
t = time(NULL);
ptr = localtime(&t);
printf("%s", asctime(ptr));
printf("\n%s\n",str);
for(int i=1;i<=5;i++)
{int p=1;
for(int j=1;j<=i;j++)
{printf("%d",p);
p++;
}
printf("\n");
}
return 0;
}
(iii)
#include <stdio.h>
#include<string.h>
#include <time.h>
int main()
{char str[]="NAME-AMIT YADAV,ENROLLMENT NO-210180107001";
struct tm* ptr;
time_t t;
t = time(NULL);
ptr = localtime(&t);
printf("%s", asctime(ptr));
printf("\n%s\n",str);
for(int i=5;i>=1;i--)
{int p=1;
for(int j=1;j<=i;j++)
{printf("%d",p);
p++;
}
printf("\n");
}
return 0;
}
Q17.)
#include <stdio.h>
#include<string.h>
#include <time.h>
int main()
{char str[]="NAME-AMIT YADAV,ENROLLMENT NO-210180107001";
struct tm* ptr;
time_t t;
t = time(NULL);
ptr = localtime(&t);
printf("%s", asctime(ptr));
printf("\n%s",str);
int a[]={12,13,18,24,79,33,19,34,59,98};
printf("\nThe number in the list are-");
for(int i=0;i<10;i++)
{
printf("%d ",a[i]);
}
printf("\nEven-");
for(int i=0;i<10;i++)
{if(a[i]%2==0)
printf("%d ",a[i]);
}
printf("\nOdd-");
for(int i=0;i<10;i++)
{if(a[i]%2!=0)
printf("%d ",a[i]);
}
return 0;
}
Q18.)
#include <stdio.h>
#include<string.h>
#include <time.h>
int main()
{char str[]="NAME-AMIT YADAV,ENROLLMENT NO-210180107001";
struct tm* ptr;
time_t t;
t = time(NULL);
ptr = localtime(&t);
printf("%s", asctime(ptr));
printf("\n%s",str);
char string[]="@123$&!e";
char in;int pos,flag;
printf("\nthe given string is:%s",string);
printf("\nenter the character to be found:");
scanf("%c",&in);
for(int i=0;string[i]!='\0';i++)
{
if(string[i]==in)
{flag=1;
pos=i+1;
break;
}
}
if(flag==1)
{printf("\ncharacter is found at %d position of string",pos);
}
else
{printf("\ncharacter not found!!");
}
return 0;
}
Q19.) Write a program to reverse string.
#include <stdio.h>
#include<string.h>
#include <time.h>
int main()
{char str[]="NAME-AMIT YADAV,ENROLLMENT NO-210180107001";
struct tm* ptr;
time_t t;
t = time(NULL);
ptr = localtime(&t);
printf("%s", asctime(ptr));
printf("\n%s",str);
char rev[100];char rev1[100];
printf("\nenter the string to be reversed:");
scanf("%s",rev);
int n=strlen(rev);
for(int i=0;rev[i]!='\0';i++)
{
rev1[(n-1)-i]=rev[i];
}
strcpy(rev,rev1);
printf("\nthe reversed string is:%s",rev);
return 0;
}
Q20.) Write a program to convert string into upper case.
#include <stdio.h>
#include<string.h>
#include <time.h>
int main()
{char str[]="NAME-AMIT YADAV,ENROLLMENT NO-210180107001";
struct tm* ptr;
time_t t;
t = time(NULL);
ptr = localtime(&t);
printf("%s", asctime(ptr));
printf("\n%s",str);
char rev[100];char rev1[100];
printf("\nenter the string to be upper cased:");
scanf("%s",rev);
strcpy(rev1,strupr(rev));
strcpy(rev,rev1);
printf("\nthe upper cased string is:%s",rev);
return 0;
}
Q21.) Write a function Exchange to interchange the values of two variables, say x and
y. illustrate the use of this function in a calling function.
#include <stdio.h>
#include<string.h>
#include <time.h>
int Exchange(int *a,int *b);
int main()
{char str[]="NAME-AMIT YADAV,ENROLLMENT NO-210180107001";
struct tm* ptr;
time_t t;
t = time(NULL);
ptr = localtime(&t);
printf("%s", asctime(ptr));
printf("\n%s",str);
int x,y;
printf("\nenter the values of x and y:");
scanf("%d %d",&x,&y);
Exchange(&x,&y);
printf("\nnew value of x is %d and y is %d",x,y);
return 0;
}
int Exchange(int *a,int *b)
{
int temp=*a;
*a=*b;*b=temp;
return 0;
}
Q22.) Write a program to find the factorial of a number using recursion.
#include <stdio.h>
#include<string.h>
#include <time.h>
int fac(int n);
int main()
{char str[]="NAME-AMIT YADAV,ENROLLMENT NO-210180107001";
struct tm* ptr;
time_t t;
t = time(NULL);
ptr = localtime(&t);
printf("%s", asctime(ptr));
printf("\n%s",str);
int n;
printf("\nenter the number:");
scanf("%d",&n);
printf("\nfactorial of number is:%d",fac(n));
return 0;
}
int fac(int n)
{if(n==1)
{
return 1;
}
int FAC=fac(n-1)*n;
return FAC;
}
Q26.) Write a program to print the address of a variable using a pointer.
#include <stdio.h>
#include<string.h>
#include <time.h>
int main()
{char str[]="NAME-AMIT YADAV,ENROLLMENT NO-210180107001";
struct tm* ptr;
time_t t;
t = time(NULL);
ptr = localtime(&t);
printf("%s", asctime(ptr));
printf("\n%s",str);
int n=5;
int *addr=&n;
printf("\naddres of the variable n is:%p",addr);
return 0;
}
Q27.) Write a C program to swap the two values using pointers
#include <stdio.h>
#include<string.h>
#include <time.h>
int main()
{char str[]="NAME-AMIT YADAV,ENROLLMENT NO-210180107001";
struct tm* ptr;
time_t t;
t = time(NULL);
ptr = localtime(&t);
printf("%s", asctime(ptr));
printf("\n%s",str);
int a,b;
int *A=&a;
int *B=&b;
printf("\nenter the values of a and b:");
scanf("%d %d",A,B);
int temp=*A;
*A=*B;
*B=temp;
printf("\nnew value of a is %d and b is %d",a,b);
return 0;
}
Q28.) Write a program to access elements using a pointer.
#include <stdio.h>
#include<string.h>
#include <time.h>
int main()
{char str[]="NAME-AMIT YADAV,ENROLLMENT NO-210180107001";
struct tm* ptr;
time_t t;
t = time(NULL);
ptr = localtime(&t);
printf("%s", asctime(ptr));
printf("\n%s",str);
int arr[]={1,2,3,4,5};
int *ptr1=arr;
printf("\nthe elements in the array are-");
for(int i=0;i<5;i++)
{
printf("%d ",*(ptr1+i));
}
return 0;
}
Q29.) Write a program to write a string in file.
#include <stdio.h>
#include<string.h>
#include <time.h>
int main()
{char str[]="NAME-AMIT YADAV,ENROLLMENT NO-210180107001";
struct tm* ptr;
time_t t;
t = time(NULL);
ptr = localtime(&t);
printf("%s", asctime(ptr));
printf("\n%s",str);
FILE *fptr;
fptr=fopen("pps.txt","w");
char string[]="AMIT";
fprintf(fptr,"%s",string);
fclose(fptr);
printf("\n\nThe string written in file is-");
char ch;
fptr = fopen("pps.txt", "r");
ch = fgetc(fptr);
while(ch != EOF) {
printf("%c", ch);
ch = fgetc(fptr);
}
fclose(fptr);
return 0;
}
A file named data contains a series of integer numbers. Write a c program to
Q30.)
read all numbers from a file and then write all odd numbers into a file named
“odd” and write all even numbers into a file named “even”. Display all the
contents of these files on screen
#include <stdio.h>
#include<string.h>
#include <time.h>
int main()
{char str[]="NAME-AMIT YADAV,ENROLLMENT NO-210180107001";
struct tm* ptr;
time_t t;
t = time(NULL);
ptr = localtime(&t);
printf("%s", asctime(ptr));
printf("\n%s",str);
FILE *fptr1, *fptr2, *fptr3;
int n, i, num;
return 0;
}
Q25.) Define a structure data type called time_struct containing three member’s integer
hour, integer minute and integer second. Develop a program that would assign
values to the individual number and display the time in the following format: 16:
40:51.
#include <stdio.h>
#include<string.h>
#include <time.h>
typedef struct time_struct{
int h,m,s;
}pps;
int main()
{char str[]="NAME-AMIT YADAV,ENROLLMENT NO-210180107001";
struct tm* ptr;
time_t t;
t = time(NULL);
ptr = localtime(&t);
printf("%s", asctime(ptr));
printf("\n%s",str);
pps t1;
printf("\nenter hours value:");
scanf("%d",&t1.h);
printf("\nenter minute value:");
scanf("%d",&t1.m);
printf("\nenter seconds value:");
scanf("%d",&t1.s);
printf("\n\n entered time is-:%d:%d:%d",t1.h,t1.m,t1.s);
return 0;
}
Q23.) Write a C program using global variables, static variables
#include <stdio.h>
#include<string.h>
#include <time.h>
int X=2;
static int Y=4;
int main()
{char str[]="NAME-AMIT YADAV,ENROLLMENT NO-210180107001";
struct tm* ptr;
time_t t;
t = time(NULL);
ptr = localtime(&t);
printf("%s", asctime(ptr));
printf("\n%s",str);
return 0;
}
Q24.) Write a program to read structure elements from the keyboard.
#include <stdio.h>
#include<string.h>
#include <time.h>
struct student{
int roll;
float cgpa;
};
int main()
{char str[]="NAME-AMIT YADAV,ENROLLMENT NO-210180107001";
struct tm* ptr;
time_t t;
t = time(NULL);
ptr = localtime(&t);
printf("%s", asctime(ptr));
printf("\n%s",str);
struct student s1;
printf("\nenter student roll number:");
scanf("%d",&s1.roll);
printf("\nenter student sem 1 cgpa:");
scanf("%f",&s1.cgpa);
printf("\n\nthe entered details of the student are:");
printf("\nroll-%d",s1.roll);
printf("\ncgpa-%f",s1.cgpa);
return 0;
}