SlideShare a Scribd company logo
1
Assignment #4
Subject: Programming Fundamentals
Semester: 1st
Submitted To: Sir. Junaid
Submitted By: Zohaib Zeeshan
Roll No: BSSE-F17-57
Date: 22/01/2018
Department of CS&IT
(BSSE)
University Of Sargodha Mandi Bahauddin Campus
2
Programming Fundamentals
(1). Printing text on screen:
a). Write a simple C program.
Code:
#include<stdio.h>
int main(void)
{
printf(“Welcome to C!”);
return 0;
}
Output:
b). Write a program to find sum of two integers.
Code:
#include<stdio.h>
int main()
{
int a=5;
int b=7;
int sum;
sum = a+b;
printf("The sum is = %d", sum);
}
Output:
(2). Write two code examples of if-else.
a). Find maximum between two numbers.
Code:
#include<stdio.h>
int main()
{
int num1, num2;
printf("Enter two integers to find which is maximumn");
scanf("%d%d", &num1, &num2);
if(num1 > num2){
printf("First numbers is maximumn");
}
else
{
printf("Second is maximumn");
}
return 0;
}
3
Output:
b). Write a program to check an integer is evenor odd.
Code:
#include<stdio.h>
int main()
{
int num;
printf("Enter an integer to check even or oddn");
scanf("%d", &num);
if(num % 2 == 0)
{
printf("Evenn");
}
else
{
printf("Oddn");
}
}
Output:
(3). Write two code examples of switch statement.
a). Write a code to check an alphabet is vowel or consonant.
Code:
#include<stdio.h>
int main ()
{
char ch;
printf("Enter an alphabetn");
scanf("%c", &ch);
switch (ch)
{
case'a':
printf("a is voweln");
break;
case 'e':
printf("e is voweln");
break;
4
case 'i':
printf("i is voweln");
break;
case 'o':
printf("o is voweln");
break;
case 'u':
printf("u is woweln");
break;
case 'A':
printf("A is voweln");
break;
case 'E':
printf("E is voweln");
break;
case 'I':
printf("I is voweln");
break;
case 'O':
printf("O is voweln");
break;
case 'U':
printf("U is voweln");
break;
default:
printf("is consonant");
}
return 0;
}
Output:
b). Write a code to check number is evenor odd.
Code:
#include<stdio.h>
int main()
{
int num;
printf("Enter a number to check even or oddn");
scanf("%d", &num);
switch(num % 2){
5
case 0:
printf("Number is Evenn");
break;
case 1:
printf("Number is Oddn");
break;
}
}
Output:
(4). Write two code examples of For Loop.
a). C program to find power ofa number using for loop.
Code:
#include<stdio.h>
int main(){
int base,exponent;
int power = 1;
int i;
printf("Enter base: n");
scanf("%d", &base);
printf("Enter exponenet: n");
scanf("%d", &exponent);
for(i=1; i<=exponent; i++){
power=power*base;
}
printf("%d ^ %d = %d", base, exponent, power);
return 0;
}
Output:
b). C program to print all even numbers from 1 to n.
Code:
#include<stdio.h>
int main(){
int i,n;
printf("Print all even numbers: n");
scanf("%d", &n);
printf("Even numbers from 1 to %d are n", n);
6
for(i=1; i<=n; i++){
if(i%2 == 0){
printf("%d ", i);
}
}
return 0;
}
Output:
(5). Write two code examples using while loop.
a). C program to print multiplication table ofa number using while loop.
Code:
#include <stdio.h>
int main()
{
int i, num;
printf("Enter number to print table: ");
scanf("%d", &num);
while(i <=10)
{
printf("%d * %d = %dn", num, i, (num*i));
i++;
}
return 0;
}
Output:
b). Write a program to genrate star pattern as shown below using while loop.
Code:
#include<stdio.h>
int main()
{
int i,j;
7
i=1;
while(i<=5){
printf("");
j=1;
while(j<=i)
{
printf("*");
j++;
}
printf("n");
i++;
}
return 0;
}
Output:
(6).Write two code examples using do while loop.
a). Value of a using do while loop.
Code:
#include <stdio.h>
int main(){
int a = 0;
// do loop execution
do {
printf("value of a: %dn", a);
a++;
}
while( a <= 5 );
return 0;
}
Output:
b). C program to print the table of 5 from 1 to 10.
Code:
#include<stdio.h>
int main()
{
int i=1;
do
{
printf("5 * %d = %dn",i,5*i);
8
i++;
}
while(i<=10);
return 0;
}
Output:
(7). Write two code examples of Functions.
a). C program to find cube ofa number using function.
Code:
#include <stdio.h>
/* Function declaration */
int cube(int num);
int main(){
int num;
int c;
printf("Enter any number: ");
scanf("%d", &num);
c = cube(num);
printf("Cube of %d is %d", num, c);
return 0;
}
int cube(int num)
{
return (num * num * num);
}
Output:
b). Find factorial of a number using function.
Code:
#include<stdio.h>
int factorial(int);
int main(){
int fact;
int numbr;
printf("Enter a number: ");
scanf("%d",&numbr);
9
fact= factorial(numbr);
printf("Factorial of %d is: %d",numbr,fact);
return 0;
}
int factorial(int n){
int i;
int factorial;
factorial =1;
for(i=1;i<=n;i++)
factorial=factorial*i;
return(factorial);
}
Output:
(8). Write two code examples of Array.
a). Write a program to find repeated elements using array.
Code:
#include<stdio.h>
int main(){
int i,arr[20],j,num;
printf("Enter size of array: ");
scanf("%d",&num);
printf("Enter any %d elements in array: ",num);
for(i=0;i<num;i++)
{
scanf("%d",&arr[i]);
}
printf("Repeated elements are: n");
for(i=0; i<num; i++)
{
for(j=i+1;j<num;j++)
{
if(arr[i]==arr[j])
{
printf("%dn",arr[i]);
}
}
}
return 0;
}
Output:
10
b). Find largest element using array.
Code:
#include <stdio.h>
int main()
{
int array[50], size, i, largest;
printf("Enter the size of the array: n");
scanf("%d", &size);
printf("Enter %d elements of the array: n", size);
for(i=0; i<size; i++){
scanf("%d", &array[i]);}
largest = array[0];
for (i = 1; i < size; i++)
{
if (largest < array[i])
largest = array[i];
}
printf("The largest element is : %dn", largest);
return 0;
}
Output:
Ad

More Related Content

What's hot (20)

Practical no 6
Practical no 6Practical no 6
Practical no 6
Kshitija Dalvi
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
Leandro Schenone
 
3. user input and some basic problem
3. user input and some basic problem3. user input and some basic problem
3. user input and some basic problem
Alamgir Hossain
 
Lab. Programs in C
Lab. Programs in CLab. Programs in C
Lab. Programs in C
Saket Pathak
 
Programming egs
Programming egs Programming egs
Programming egs
Dr.Subha Krishna
 
2. introduction of a c program
2. introduction of a c program2. introduction of a c program
2. introduction of a c program
Alamgir Hossain
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in c
Saranya saran
 
C important questions
C important questionsC important questions
C important questions
JYOTI RANJAN PAL
 
C Programming
C ProgrammingC Programming
C Programming
Sumant Diwakar
 
Programming with c language practical manual
Programming with c language practical manualProgramming with c language practical manual
Programming with c language practical manual
Anil Bishnoi
 
Testing lecture after lec 4
Testing lecture after lec 4Testing lecture after lec 4
Testing lecture after lec 4
emailharmeet
 
Chapter 5 exercises Balagurusamy Programming ANSI in c
Chapter 5 exercises Balagurusamy Programming ANSI  in cChapter 5 exercises Balagurusamy Programming ANSI  in c
Chapter 5 exercises Balagurusamy Programming ANSI in c
BUBT
 
B.Com 1year Lab programs
B.Com 1year Lab programsB.Com 1year Lab programs
B.Com 1year Lab programs
Prasadu Peddi
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
Saranya saran
 
Chapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in CChapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in C
BUBT
 
C programs
C programsC programs
C programs
Minu S
 
Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7
alish sha
 
C language questions_answers_explanation
C language questions_answers_explanationC language questions_answers_explanation
C language questions_answers_explanation
srinath v
 
CP Handout#7
CP Handout#7CP Handout#7
CP Handout#7
trupti1976
 
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
vikram mahendra
 
3. user input and some basic problem
3. user input and some basic problem3. user input and some basic problem
3. user input and some basic problem
Alamgir Hossain
 
Lab. Programs in C
Lab. Programs in CLab. Programs in C
Lab. Programs in C
Saket Pathak
 
2. introduction of a c program
2. introduction of a c program2. introduction of a c program
2. introduction of a c program
Alamgir Hossain
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in c
Saranya saran
 
Programming with c language practical manual
Programming with c language practical manualProgramming with c language practical manual
Programming with c language practical manual
Anil Bishnoi
 
Testing lecture after lec 4
Testing lecture after lec 4Testing lecture after lec 4
Testing lecture after lec 4
emailharmeet
 
Chapter 5 exercises Balagurusamy Programming ANSI in c
Chapter 5 exercises Balagurusamy Programming ANSI  in cChapter 5 exercises Balagurusamy Programming ANSI  in c
Chapter 5 exercises Balagurusamy Programming ANSI in c
BUBT
 
B.Com 1year Lab programs
B.Com 1year Lab programsB.Com 1year Lab programs
B.Com 1year Lab programs
Prasadu Peddi
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
Saranya saran
 
Chapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in CChapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in C
BUBT
 
C programs
C programsC programs
C programs
Minu S
 
Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7
alish sha
 
C language questions_answers_explanation
C language questions_answers_explanationC language questions_answers_explanation
C language questions_answers_explanation
srinath v
 
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
vikram mahendra
 

Similar to Programming fundamentals (20)

C lab
C labC lab
C lab
rajni kaushal
 
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
PRATHAMESH DESHPANDE
 
C Programming
C ProgrammingC Programming
C Programming
Sumant Diwakar
 
Subject:Programming in C - Lab Programmes
Subject:Programming in C - Lab ProgrammesSubject:Programming in C - Lab Programmes
Subject:Programming in C - Lab Programmes
vasukir11
 
Common problems solving using c
Common problems solving using cCommon problems solving using c
Common problems solving using c
ArghodeepPaul
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
Mainak Sasmal
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
Mainak Sasmal
 
Itp practical file_1-year
Itp practical file_1-yearItp practical file_1-year
Itp practical file_1-year
AMIT SINGH
 
Computer P-Lab-Manual acc to syllabus.pdf
Computer P-Lab-Manual acc to syllabus.pdfComputer P-Lab-Manual acc to syllabus.pdf
Computer P-Lab-Manual acc to syllabus.pdf
sujathachoudaryn29
 
Best C Programming Solution
Best C Programming SolutionBest C Programming Solution
Best C Programming Solution
yogini sharma
 
C file
C fileC file
C file
simarsimmygrewal
 
Progr3
Progr3Progr3
Progr3
SANTOSH RATH
 
C
CC
C
Mukund Trivedi
 
Linux_C_LabBasics.ppt
Linux_C_LabBasics.pptLinux_C_LabBasics.ppt
Linux_C_LabBasics.ppt
CharuJain396881
 
In C Programming create a program that converts a number from decimal.docx
In C Programming create a program that converts a number from decimal.docxIn C Programming create a program that converts a number from decimal.docx
In C Programming create a program that converts a number from decimal.docx
tristans3
 
C lab programs
C lab programsC lab programs
C lab programs
Dr. Prashant Vats
 
C lab programs
C lab programsC lab programs
C lab programs
Dr. Prashant Vats
 
C Language Programs
C Language Programs C Language Programs
C Language Programs
Mansi Tyagi
 
Unit-IV.pptx
Unit-IV.pptxUnit-IV.pptx
Unit-IV.pptx
Mehul Desai
 
comp2
comp2comp2
comp2
franzneri
 
Subject:Programming in C - Lab Programmes
Subject:Programming in C - Lab ProgrammesSubject:Programming in C - Lab Programmes
Subject:Programming in C - Lab Programmes
vasukir11
 
Common problems solving using c
Common problems solving using cCommon problems solving using c
Common problems solving using c
ArghodeepPaul
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
Mainak Sasmal
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
Mainak Sasmal
 
Itp practical file_1-year
Itp practical file_1-yearItp practical file_1-year
Itp practical file_1-year
AMIT SINGH
 
Computer P-Lab-Manual acc to syllabus.pdf
Computer P-Lab-Manual acc to syllabus.pdfComputer P-Lab-Manual acc to syllabus.pdf
Computer P-Lab-Manual acc to syllabus.pdf
sujathachoudaryn29
 
Best C Programming Solution
Best C Programming SolutionBest C Programming Solution
Best C Programming Solution
yogini sharma
 
In C Programming create a program that converts a number from decimal.docx
In C Programming create a program that converts a number from decimal.docxIn C Programming create a program that converts a number from decimal.docx
In C Programming create a program that converts a number from decimal.docx
tristans3
 
C Language Programs
C Language Programs C Language Programs
C Language Programs
Mansi Tyagi
 
Ad

More from Zaibi Gondal (8)

Modal Verbs
Modal VerbsModal Verbs
Modal Verbs
Zaibi Gondal
 
Parts of speech1
Parts of speech1Parts of speech1
Parts of speech1
Zaibi Gondal
 
Wirless Security By Zohaib Zeeshan
Wirless Security By Zohaib ZeeshanWirless Security By Zohaib Zeeshan
Wirless Security By Zohaib Zeeshan
Zaibi Gondal
 
C project on a bookshop for saving of coustmer record
C project on a bookshop for saving of coustmer recordC project on a bookshop for saving of coustmer record
C project on a bookshop for saving of coustmer record
Zaibi Gondal
 
Backup data
Backup data Backup data
Backup data
Zaibi Gondal
 
Functional english
Functional englishFunctional english
Functional english
Zaibi Gondal
 
application of electronics in computer
application of electronics in computerapplication of electronics in computer
application of electronics in computer
Zaibi Gondal
 
Model Verbs
Model VerbsModel Verbs
Model Verbs
Zaibi Gondal
 
Wirless Security By Zohaib Zeeshan
Wirless Security By Zohaib ZeeshanWirless Security By Zohaib Zeeshan
Wirless Security By Zohaib Zeeshan
Zaibi Gondal
 
C project on a bookshop for saving of coustmer record
C project on a bookshop for saving of coustmer recordC project on a bookshop for saving of coustmer record
C project on a bookshop for saving of coustmer record
Zaibi Gondal
 
Functional english
Functional englishFunctional english
Functional english
Zaibi Gondal
 
application of electronics in computer
application of electronics in computerapplication of electronics in computer
application of electronics in computer
Zaibi Gondal
 
Ad

Recently uploaded (20)

Overview Well-Being and Creative Careers
Overview Well-Being and Creative CareersOverview Well-Being and Creative Careers
Overview Well-Being and Creative Careers
University of Amsterdam
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
Cultivation Practice of Turmeric in Nepal.pptx
Cultivation Practice of Turmeric in Nepal.pptxCultivation Practice of Turmeric in Nepal.pptx
Cultivation Practice of Turmeric in Nepal.pptx
UmeshTimilsina1
 
The History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptxThe History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
Rock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian HistoryRock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian History
Virag Sontakke
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
parmarjuli1412
 
Module 1: Foundations of Research
Module 1: Foundations of ResearchModule 1: Foundations of Research
Module 1: Foundations of Research
drroxannekemp
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
Overview Well-Being and Creative Careers
Overview Well-Being and Creative CareersOverview Well-Being and Creative Careers
Overview Well-Being and Creative Careers
University of Amsterdam
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
Cultivation Practice of Turmeric in Nepal.pptx
Cultivation Practice of Turmeric in Nepal.pptxCultivation Practice of Turmeric in Nepal.pptx
Cultivation Practice of Turmeric in Nepal.pptx
UmeshTimilsina1
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
Rock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian HistoryRock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian History
Virag Sontakke
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
parmarjuli1412
 
Module 1: Foundations of Research
Module 1: Foundations of ResearchModule 1: Foundations of Research
Module 1: Foundations of Research
drroxannekemp
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 

Programming fundamentals

  • 1. 1 Assignment #4 Subject: Programming Fundamentals Semester: 1st Submitted To: Sir. Junaid Submitted By: Zohaib Zeeshan Roll No: BSSE-F17-57 Date: 22/01/2018 Department of CS&IT (BSSE) University Of Sargodha Mandi Bahauddin Campus
  • 2. 2 Programming Fundamentals (1). Printing text on screen: a). Write a simple C program. Code: #include<stdio.h> int main(void) { printf(“Welcome to C!”); return 0; } Output: b). Write a program to find sum of two integers. Code: #include<stdio.h> int main() { int a=5; int b=7; int sum; sum = a+b; printf("The sum is = %d", sum); } Output: (2). Write two code examples of if-else. a). Find maximum between two numbers. Code: #include<stdio.h> int main() { int num1, num2; printf("Enter two integers to find which is maximumn"); scanf("%d%d", &num1, &num2); if(num1 > num2){ printf("First numbers is maximumn"); } else { printf("Second is maximumn"); } return 0; }
  • 3. 3 Output: b). Write a program to check an integer is evenor odd. Code: #include<stdio.h> int main() { int num; printf("Enter an integer to check even or oddn"); scanf("%d", &num); if(num % 2 == 0) { printf("Evenn"); } else { printf("Oddn"); } } Output: (3). Write two code examples of switch statement. a). Write a code to check an alphabet is vowel or consonant. Code: #include<stdio.h> int main () { char ch; printf("Enter an alphabetn"); scanf("%c", &ch); switch (ch) { case'a': printf("a is voweln"); break; case 'e': printf("e is voweln"); break;
  • 4. 4 case 'i': printf("i is voweln"); break; case 'o': printf("o is voweln"); break; case 'u': printf("u is woweln"); break; case 'A': printf("A is voweln"); break; case 'E': printf("E is voweln"); break; case 'I': printf("I is voweln"); break; case 'O': printf("O is voweln"); break; case 'U': printf("U is voweln"); break; default: printf("is consonant"); } return 0; } Output: b). Write a code to check number is evenor odd. Code: #include<stdio.h> int main() { int num; printf("Enter a number to check even or oddn"); scanf("%d", &num); switch(num % 2){
  • 5. 5 case 0: printf("Number is Evenn"); break; case 1: printf("Number is Oddn"); break; } } Output: (4). Write two code examples of For Loop. a). C program to find power ofa number using for loop. Code: #include<stdio.h> int main(){ int base,exponent; int power = 1; int i; printf("Enter base: n"); scanf("%d", &base); printf("Enter exponenet: n"); scanf("%d", &exponent); for(i=1; i<=exponent; i++){ power=power*base; } printf("%d ^ %d = %d", base, exponent, power); return 0; } Output: b). C program to print all even numbers from 1 to n. Code: #include<stdio.h> int main(){ int i,n; printf("Print all even numbers: n"); scanf("%d", &n); printf("Even numbers from 1 to %d are n", n);
  • 6. 6 for(i=1; i<=n; i++){ if(i%2 == 0){ printf("%d ", i); } } return 0; } Output: (5). Write two code examples using while loop. a). C program to print multiplication table ofa number using while loop. Code: #include <stdio.h> int main() { int i, num; printf("Enter number to print table: "); scanf("%d", &num); while(i <=10) { printf("%d * %d = %dn", num, i, (num*i)); i++; } return 0; } Output: b). Write a program to genrate star pattern as shown below using while loop. Code: #include<stdio.h> int main() { int i,j;
  • 7. 7 i=1; while(i<=5){ printf(""); j=1; while(j<=i) { printf("*"); j++; } printf("n"); i++; } return 0; } Output: (6).Write two code examples using do while loop. a). Value of a using do while loop. Code: #include <stdio.h> int main(){ int a = 0; // do loop execution do { printf("value of a: %dn", a); a++; } while( a <= 5 ); return 0; } Output: b). C program to print the table of 5 from 1 to 10. Code: #include<stdio.h> int main() { int i=1; do { printf("5 * %d = %dn",i,5*i);
  • 8. 8 i++; } while(i<=10); return 0; } Output: (7). Write two code examples of Functions. a). C program to find cube ofa number using function. Code: #include <stdio.h> /* Function declaration */ int cube(int num); int main(){ int num; int c; printf("Enter any number: "); scanf("%d", &num); c = cube(num); printf("Cube of %d is %d", num, c); return 0; } int cube(int num) { return (num * num * num); } Output: b). Find factorial of a number using function. Code: #include<stdio.h> int factorial(int); int main(){ int fact; int numbr; printf("Enter a number: "); scanf("%d",&numbr);
  • 9. 9 fact= factorial(numbr); printf("Factorial of %d is: %d",numbr,fact); return 0; } int factorial(int n){ int i; int factorial; factorial =1; for(i=1;i<=n;i++) factorial=factorial*i; return(factorial); } Output: (8). Write two code examples of Array. a). Write a program to find repeated elements using array. Code: #include<stdio.h> int main(){ int i,arr[20],j,num; printf("Enter size of array: "); scanf("%d",&num); printf("Enter any %d elements in array: ",num); for(i=0;i<num;i++) { scanf("%d",&arr[i]); } printf("Repeated elements are: n"); for(i=0; i<num; i++) { for(j=i+1;j<num;j++) { if(arr[i]==arr[j]) { printf("%dn",arr[i]); } } } return 0; } Output:
  • 10. 10 b). Find largest element using array. Code: #include <stdio.h> int main() { int array[50], size, i, largest; printf("Enter the size of the array: n"); scanf("%d", &size); printf("Enter %d elements of the array: n", size); for(i=0; i<size; i++){ scanf("%d", &array[i]);} largest = array[0]; for (i = 1; i < size; i++) { if (largest < array[i]) largest = array[i]; } printf("The largest element is : %dn", largest); return 0; } Output:
  翻译: